1 /**
  2  * implement Ratamahatta character.
  3  * TODO seems to have issue with multilple characters. it may be about material 
  4  * caching. seems typically a microcache issue.
  5  *
  6  * @name	tQuery.RatamahattaMD2Character
  7  * @augments	tQuery.MD2Character
  8  * @constructor
  9 */
 10 tQuery.registerStatic('RatamahattaMD2Character', function(opts){
 11 	// call parent ctor
 12 	var parent	= tQuery.RatamahattaMD2Character.parent;
 13 	parent.constructor.call(this)
 14 	// handle parameters
 15 	opts	= tQuery.extend(opts, {
 16 		baseUrl		: tQuery.RatamahattaMD2Character.baseUrl+'/models/ratamahatta/'
 17 	});
 18 
 19 	// handle the attachedWorld
 20 	this._attachedWorld	= null;
 21 	this._$loopCb		= this._loopCb.bind(this);
 22 
 23 	// change the scale
 24 	this.scale(0.03);
 25 
 26 	// load the data
 27 	this.load({
 28 		baseUrl	: opts.baseUrl,
 29 		body	: "ratamahatta.js",
 30 		//skins	: [ "ratamahatta.png"],
 31 		skins	: [ "ratamahatta.png", "ctf_b.png", "ctf_r.png", "dead.png", "gearwhore.png" ],
 32 		weapons	: [
 33 			[ "weapon.js", "weapon.png" ],
 34 			[ "w_bfg.js", "w_bfg.png" ],
 35 			[ "w_blaster.js", "w_blaster.png" ],
 36 			[ "w_chaingun.js", "w_chaingun.png" ],
 37 			[ "w_glauncher.js", "w_glauncher.png" ],
 38 			[ "w_hyperblaster.js", "w_hyperblaster.png" ],
 39 			[ "w_machinegun.js", "w_machinegun.png" ],
 40 			[ "w_railgun.js", "w_railgun.png" ],
 41 			[ "w_rlauncher.js", "w_rlauncher.png" ],
 42 			[ "w_shotgun.js", "w_shotgun.png" ],
 43 			[ "w_sshotgun.js", "w_sshotgun.png" ]
 44 		]
 45 	});
 46 });
 47 
 48 tQuery.RatamahattaMD2Character.baseUrl	= "../../../plugins/md2character/";
 49 
 50 /**
 51  * inherit from tQuery.MD2Character
 52 */
 53 tQuery.inherit(tQuery.RatamahattaMD2Character, tQuery.MD2Character);
 54 
 55 // make it pluginable
 56 tQuery.pluginsInstanceOn(tQuery.RatamahattaMD2Character);
 57 
 58 tQuery.RatamahattaMD2Character.prototype.destroy	= function(){
 59 	// call parent ctor
 60 	var parent	= tQuery.RatamahattaMD2Character.parent;
 61 	parent.destroy.call(this)
 62 }
 63 
 64 //////////////////////////////////////////////////////////////////////////////////
 65 //										//
 66 //////////////////////////////////////////////////////////////////////////////////
 67 
 68 tQuery.RatamahattaMD2Character.prototype.attach	= function(world){
 69 	if( this._attachedWorld )	this.detacheFrom(this.attachedWorld);
 70 	world	= world	|| tQuery.world;
 71 	this._attachedWorld	= world;
 72 	world.add( this.container() )
 73 	world.loop().hook(this._$loopCb);	
 74 	return this;	// for chained API
 75 };
 76 
 77 tQuery.RatamahattaMD2Character.prototype.detach	= function(){
 78 	world	= world || this._attachedWorld;
 79 	world.remove(this.container())
 80 	world.loop().unhook(this._$loopCb);
 81 	this._attachedWorld	= null;
 82 	return this;	// for chained API
 83 };
 84 
 85 tQuery.RatamahattaMD2Character.prototype._loopCb	= function(delta, now){
 86 	this.update( delta, now);
 87 }
 88 
 89 //////////////////////////////////////////////////////////////////////////////////
 90 //		.turn()								//
 91 //////////////////////////////////////////////////////////////////////////////////
 92 
 93 /**
 94  * Turn left
 95  * @param {Number} angle angle to turn to in radian 
 96 */
 97 tQuery.RatamahattaMD2Character.prototype.turnLeft	= function(angle){
 98 	if( angle === undefined )	angle = 0.1;
 99 	return this.turn(+angle)
100 };
101 
102 /**
103  * Turn right
104  * @param {Number} angle angle to turn to in radian 
105 */
106 tQuery.RatamahattaMD2Character.prototype.turnRight	= function(angle){
107 	if( angle === undefined )	angle = 0.1;
108 	return this.turn(-angle)
109 };
110 
111 /**
112  * Turn to an angle
113  * 
114  * @param {Number} angle angle to turn to in radian 
115 */
116 tQuery.RatamahattaMD2Character.prototype.turn	= function(angle){
117 	var character	= this;
118 	if( angle === undefined, "angle MUST be defined" );
119 	if( this.isLoaded() === false )	return this;
120 	var container	= character.container();
121 	container.rotation.y	+= angle;
122 	return this;	// for chained API
123 };
124 
125 //////////////////////////////////////////////////////////////////////////////////
126 //		.go()								//
127 //////////////////////////////////////////////////////////////////////////////////
128 
129 /**
130  * go forward to a distance
131  * 
132  * @param {Number} angle angle to turn to in radian 
133 */
134 tQuery.RatamahattaMD2Character.prototype.goForward	= function(distance){
135 	if( this.isLoaded() === false )	return this;
136 	distance	= distance !== undefined ? distance : 0.05;
137 	var container	= this.container();
138 	var angle	= container.rotation.y;
139 	
140 	var speed	= new THREE.Vector3(0, 0, distance);
141 	var matrix	= new THREE.Matrix4().makeRotationY(angle);
142 	matrix.multiplyVector3(speed);
143 	container.position.addSelf(speed);
144 	return this;	// for chained API
145 };
146