topical media & game development

talk show tell print

mobile-query-three-plugins-minecraft-tquery.minecraftcharcontrols.js / js



  tQuery.registerStatic('MinecraftCharControls', function(opts){
          // handle arguments polymorphism
          if( opts instanceof tQuery.MinecraftChar )        opts        = { object3D: opts.object3D() };
          if( opts instanceof THREE.Object3D )                opts        = { object3D: opts };
          if( opts instanceof tQuery.Object3D )                opts        = { object3D: opts.get(0) };
          if( opts.object3D instanceof tQuery.Object3D )        opts.object3D        = opts.object3D.get(0)
          // handle arguments default values
          this._opts        = opts        = tQuery.extend(opts, {
                  world                : tQuery.world,
                  lateralMove        : 'rotationY'
          });
          // arguments sanity check
          console.assert( opts.object3D instanceof THREE.Object3D )
          console.assert( ['strafe', 'rotationY'].indexOf(opts.lateralMove) !== -1 );
          
          // user control
          this._input        = {};
          this._callback        = opts.world.loop().hook(function(delta, now){
                  var model        = opts.object3D;
                  var prevPosition= model.position.clone();
                  var input        = this._input;
                  // keyboard handling                
                  if( opts.lateralMove === 'rotationY' ){
                          // lateral => rotation Y
                          if( input.left )        model.rotation.y += 0.3 * delta * Math.PI * 2;
                          if( input.right )        model.rotation.y -= 0.3 * delta * Math.PI * 2;                        
                  }else if( opts.lateralMove === 'strafe' ){
                          // lateral => strafe
                          var distance        = 0;
                          if( input.left )        distance        = +2 * delta;
                          if( input.right )        distance        = -2 * delta;
                          if( distance ){
                                  var speed        = new THREE.Vector3(distance, 0, 0);
                                  var matrix        = new THREE.Matrix4().makeRotationY(model.rotation.y);
                                  speed.applyMatrix4( matrix );
                                  model.position.add(speed);
                          }                        
                  }else        console.assert(false, 'opts.lateralMove invalid: '+opts.lateralMove);
  
                  var distance        = 0;
                  if( input.up )                distance        = +2 * delta;
                  if( input.down )        distance        = -2 * delta;
                  if( distance ){
                          var speed        = new THREE.Vector3(0, 0, distance);
                          var matrix        = new THREE.Matrix4().makeRotationY(model.rotation.y);
                          speed.applyMatrix4( matrix );
                          model.position.add(speed);
                  }
                  // notify an event of the update
                  this.dispatchEvent('postUpdate', model.position, prevPosition);
          }.bind(this));
  });
  
  // make it eventable
  tQuery.MicroeventMixin(tQuery.MinecraftCharControls.prototype);
  
  tQuery.MinecraftCharControls.prototype.destroy        = function(){
          this._opts.world.loop().unhook(this._callback);
  }
  
  tQuery.registerStatic('createMinecraftCharControls', function(opts){
          return new tQuery.MinecraftCharControls(opts)
  });
  
  
////////////////////////////////////////////////////////////////////////////

// comment //
////////////////////////////////////////////////////////////////////////////

tQuery.MinecraftCharControls.prototype.input = function(value){ if( value === undefined ) return this._input; this._input = value; return this; // for chained API };


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.