Old stuff/old_sites/threejs/13_fighting/js/perso.js
(Deskargatu)
var Perso = function(game, options)
{
var self=this;
self.move_speed= 0.8;
self.id='perso_'+Math.random();
self.move_action_weight=0;
self.is_moving=false;
self.in_cells=[];
this.build = function()
{
this.game = game;
this.options=options;
this.create();
if(options.moveable)
{
this.moveable();
}
};
this.create =function()
{
var self=this;
this.container = new THREE.Object3D();
this.game.scene.add(this.container);
// Cube simulating perso, for collision detection
var cube_material = new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe:game.opt.debug_level>1, transparent:true, opacity: game.opt.debug_level>1 ? 1 : 0 } );
var cube_geo = new THREE.BoxGeometry(6 , 6, 6);
this.container_mesh = new THREE.Mesh(cube_geo, cube_material);
this.container_mesh.rotation.x = Math.radians(90);
this.container_mesh.rotation.z = Math.radians(45);
this.container_mesh.position.y=1;
this.container_mesh.name='p';
this.container.add(this.container_mesh);
this.container.position.x = this.options.x;
this.container.position.y = 0;
this.container.position.z = this.options.z;
var materials = game.assets.perso_mat;
for ( var i = 0; i < materials.length; i ++ ) {
var m = materials[ i ];
m.skinning = true;
m.morphTargets = true;
}
this.mesh = new THREE.SkinnedMesh( game.assets.perso_geo, new THREE.MultiMaterial(materials));
this.mesh.castShadow=true;
this.mesh.scale.x=15;
this.mesh.scale.y=15;
this.mesh.scale.z=15;
this.container.add(this.mesh);
this.mesh.receiveShadow = true;
//this.mesh.castShadow = true;
this.mesh.position.x = 0;
this.mesh.position.y = 1;
this.mesh.position.z = 0;
this.mixer = new THREE.AnimationMixer( this.mesh );
this.walkingClip = game.assets.perso_geo.animations[2];
this.iddlingClip = game.assets.perso_geo.animations[1];
this.move_action = this.mixer.clipAction(this.walkingClip, null ).setDuration(0.45);
this.idle_action = this.mixer.clipAction(this.iddlingClip, null ).setDuration(5);
this.move_action.play();
this.idle_action.play();
this.move_action.setEffectiveWeight(0);
this.idle_action.setEffectiveWeight(1);
};
this.getRayPos= function(event)
{
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
mouse.x = ( event.clientX / this.game.renderer.domElement.width ) * 2 - 1;
mouse.y = - ( event.clientY / this.game.renderer.domElement.height ) * 2 + 1;
raycaster.setFromCamera( mouse, this.game.camera );
var intersects = raycaster.intersectObjects( [this.game.click_ground], false );
if ( intersects.length > 0 ) {
return intersects[0].point;
}
return null;
}
this.lookAt= function(pos)
{
if(this.container)
{
pos.y=0;
this.container.lookAt(pos);
}
}
this.moveToEvent= function(event)
{
var pos = this.getRayPos(event);
if(pos)
{
this.lookAt(pos);
this.moveTo(pos);
}
}
this.moveTo= function(pos)
{
var current_pos = this.container.position;
// Last part of moving
if(current_pos.equals(pos))
{
this.move_event=null;
return;
}
this.is_moving=true;
var distance = Math.sqrt(Math.pow(pos.x-current_pos.x,2)+Math.pow(pos.z-current_pos.z,2));
var directionX = (pos.x-current_pos.x) / distance;
var directionZ = (pos.z-current_pos.z) / distance;
move_step_x = this.move_speed * directionX;
move_step_z = this.move_speed * directionZ;
this.move_step_vector_x = new THREE.Vector2();
this.move_step_vector_z = new THREE.Vector2();
this.move_step_vector_x.x = move_step_x;
this.move_step_vector_x.z=0;
this.move_step_vector_z.x=0;
this.move_step_vector_z.z = move_step_z;
// Actually moving...
this.move_destination = pos;
if(this.move_action_weight!=1)
{
this.move_weight_destination = 1;
}
};
this.move_step= function()
{
if(this.is_moving)
{
game.assets.step_sound.play();
var moving=0;
// Collision callbacks
var originPoint = this.container.position.clone();
var obstacles = game.getObstacles();
var obstacles_with_callbacks = game.getCollisionCallbacks();
var objects=[];
for (var vertexIndex = 0; vertexIndex < this.container_mesh.geometry.vertices.length; vertexIndex++)
{
var localVertex = this.container_mesh.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4( this.container_mesh.matrix );
var directionVector = globalVertex.sub( this.container_mesh.position );
var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize(),0 , game.opt.door_size );
var collisionResults = ray.intersectObjects(obstacles_with_callbacks);
if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() )
{
objects.push(collisionResults[0].object);
}
}
game.callCollisionCallbacks(this,objects);
// Moving X restrictions
if(Math.abs(this.container.position.x - this.move_destination.x) > 1)
{
var originPoint_x = this.container.position.clone();
originPoint_x.add(this.move_step_vector_x);
var collision_x=false;
for (var vertexIndex = 0; vertexIndex < this.container_mesh.geometry.vertices.length; vertexIndex++)
{
var localVertex = this.container_mesh.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4( this.container_mesh.matrix );
var directionVector = globalVertex.sub( this.container_mesh.position );
var ray = new THREE.Raycaster( originPoint_x, directionVector.clone().normalize(),0 , game.opt.door_size );
var collisionResults = ray.intersectObjects(obstacles);
if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() )
{
collision_x=true;
}
}
if(!collision_x)
{
moving++;
this.container.position.add(this.move_step_vector_x);
}
}
// Moving Z restrictions
if(Math.abs(this.container.position.z - this.move_destination.z) > 1)
{
var originPoint_z = this.container.position.clone();
originPoint_z.add(this.move_step_vector_z);
var collision_z=false;
for (var vertexIndex = 0; vertexIndex < this.container_mesh.geometry.vertices.length; vertexIndex++)
{
var localVertex = this.container_mesh.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4( this.container_mesh.matrix );
var directionVector = globalVertex.sub( this.container_mesh.position );
var ray = new THREE.Raycaster( originPoint_z, directionVector.clone().normalize(),0 , game.opt.door_size );
var collisionResults = ray.intersectObjects(obstacles);
if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() )
{
collision_z=true;
}
}
if(!collision_z)
{
moving++;
this.container.position.add(this.move_step_vector_z);
}
}
if(!moving)
{
game.assets.step_sound.pause();
this.move_weight_destination = 0;
this.is_moving=false;
this.move_destination=null;
}
}
};
this.move_weight = function()
{
if(this.move_weight_destination!==null)
{
var c = this.move_action.getEffectiveWeight();
var dest;
if(c>this.move_weight_destination)
{
var dest = Math.max(0,c-0.3);
this.move_action.setEffectiveWeight(dest);
this.idle_action.setEffectiveWeight(1-dest);
this.move_action_weight=dest;
}
else if(c<this.move_weight_destination)
{
var dest = Math.min(1,c+0.3);
this.move_action.setEffectiveWeight(dest);
this.idle_action.setEffectiveWeight(1-dest);
this.move_action_weight=dest;
}
else
{
this.move_weight_destination=null;
}
this.move_action_weight=c;
}
};
this.moveable = function()
{
var self=this;
this.trigger_move=false;
this.move_event = null;
// Moveable perso has a light
document.addEventListener( 'mousemove', function(e) { if(self.trigger_move) { self.move_event=e; self.moveToEvent(e); } }, false );
document.addEventListener( 'mouseup', function(e) { self.move_event=null;self.trigger_move=false; e.stopPropagation(); return false; }, false);
document.addEventListener( 'mousedown', function(e) { self.move_event=e; self.trigger_move=true;e.stopPropagation(); self.moveToEvent(e); return false; }, false);
};
this.update= function(delta)
{
this.mixer.update(delta);
this.delta=delta;
if(this.move_event)
{
this.moveToEvent(this.move_event);
}
this.move_step();
this.move_weight();
};
this.build();
};