1 // TODO make it require.js compatible
  2 
  3 /**
  4  * Hook the keyboard
  5  *
  6  * @name	hookKeyboard
  7  * @memberOf	tQuery.RatamahattaMD2Character
  8 */
  9 tQuery.RatamahattaMD2Character.registerInstance('hookKeyboard', function(opts){
 10 	// handle parameters
 11 	opts	= tQuery.extend(opts, {
 12 		loop	: tQuery.world.loop()
 13 	});
 14 	// create the loop callback
 15 	var loopCb	= this.hookKeyboardLoopCb.bind(this);
 16 	// store the loopCb
 17 	tQuery.data(this, 'hookKeyboardLoopCb', loopCb, true);
 18 	// hook the callback
 19 	opts.loop.hook(loopCb);
 20 	// for chained API
 21 	return this;
 22 });
 23 
 24 /**
 25  * unhook the keyboard
 26  *
 27  * @name	unhookKeyboard
 28  * @memberOf	tQuery.RatamahattaMD2Character
 29 */
 30 tQuery.RatamahattaMD2Character.registerInstance('unhookKeyboard', function(opts){
 31 	// handle parameters
 32 	opts	= tQuery.extend(opts, {
 33 		loop	: tQuery.world.loop()
 34 	});
 35 	// fetch loopCb
 36 	var loopCb	= tQuery.data(this, 'hookKeyboardLoopCb');
 37 	// unstore loopCb
 38 	tQuery.removeData(this, 'hookKeyboardLoopCb');
 39 	// unhook the callback
 40 	opts.loop.unhook(loopCb);
 41 	// for chained API
 42 	return this;
 43 });
 44 
 45 /**
 46  * callback for hook the keyboard
 47  * 
 48  * @private
 49  * @name	hookKeyboardLoopCb
 50  * @memberOf	tQuery.RatamahattaMD2Character
 51 */
 52 tQuery.RatamahattaMD2Character.registerInstance('hookKeyboardLoopCb', function(){
 53 	var keyboard	= tQuery.keyboard();
 54 	var character	= this;
 55 	// keyboard handling
 56 	if( keyboard.pressed("left") )	character.turnLeft();
 57 	if( keyboard.pressed("right") )	character.turnRight();
 58 	if( keyboard.pressed("up") )	character.goForward();
 59 
 60 	// handle the animation
 61 	if( keyboard.pressed("up") ){
 62 		if( character.animation() !== 'run' ) character.animation('run')
 63 	}else if( character.animation() === 'run' ){
 64 		if( character.animation !== 'stand' ) character.animation('stand')
 65 	}
 66 });