1 // TODO make it require.js compatible 2 3 /** 4 * Hook the keyboard 5 * 6 * @name hookKeyboard 7 * @memberOf tQuery.RatamahattaMD2Character 8 */ 9 tQuery.MinecraftChar.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', { 18 loopCb : loopCb, 19 rotation: tQuery.createVector3(), 20 position: tQuery.createVector3() 21 }, true); 22 // hook the callback 23 opts.loop.hook(loopCb); 24 // for chained API 25 return this; 26 }); 27 28 /** 29 * unhook the keyboard 30 * 31 * @name unhookKeyboard 32 * @memberOf tQuery.RatamahattaMD2Character 33 */ 34 tQuery.MinecraftChar.registerInstance('unhookKeyboard', function(opts){ 35 // handle parameters 36 opts = tQuery.extend(opts, { 37 loop : tQuery.world.loop() 38 }); 39 // fetch loopCb 40 var loopCb = tQuery.data(this, 'hookKeyboardLoopCb'); 41 // unstore loopCb 42 tQuery.removeData(this, 'hookKeyboardLoopCb'); 43 // unhook the callback 44 opts.loop.unhook(loopCb); 45 // for chained API 46 return this; 47 }); 48 49 /** 50 * callback for hook the keyboard 51 * 52 * @private 53 * @name hookKeyboardLoopCb 54 * @memberOf tQuery.RatamahattaMD2Character 55 */ 56 tQuery.MinecraftChar.registerInstance('hookKeyboardLoopCb', function(delta, now){ 57 var keyboard = tQuery.keyboard(); 58 var model = this.model; 59 // keyboard handling 60 if( keyboard.pressed("left") ) model.translateX(-4*delta); 61 if( keyboard.pressed("right") ) model.translateX(+4*delta); 62 if( keyboard.pressed("up") ) model.translateY(+4*delta); 63 if( keyboard.pressed("down") ) model.translateY(-4*delta); 64 });