Old stuff/old_sites/threejs/9_world/js/perso.js
(Deskargatu)
var Perso = function()
{
var self=this;
self.move_speed= 1.0;
self.is_moving=false;
this.load= function()
{
var self=this;
return new Promise(function(ok, reject)
{
var loader = new THREE.JSONLoader();
console.log('loading meshes perso.js');
loader.load( "blender/perso.json", function(geometry, mat)
{
self.perso_geo = geometry;
console.log('loaded perso.js', self.perso_geo);
self.perso_mat = mat;
ok();
});
});
};
this.build = function(options, game)
{
this.game = game;
console.log('creating perso', this.perso_geo);
this.options=options;
this.create();
};
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:true } );
var cube_geo = new THREE.CubeGeometry(6, 6, 6)
this.container_mesh = new THREE.Mesh(cube_geo, cube_material);
this.container.add(this.container_mesh);
material = new THREE.MeshLambertMaterial( { color: 0xffffff } );
material.skinning = true;
material.morphTargets = true;
this.mesh = new THREE.SkinnedMesh( self.perso_geo, material);
this.mesh.scale.x=30;
this.mesh.scale.y=30;
this.mesh.scale.z=30;
this.container.add(this.mesh);
this.mesh.castShadow=true;
this.mesh.receiveShadow=true;
this.mesh.position.x = this.options.x;
this.mesh.position.y = 0;
this.mesh.position.z = this.options.z;
this.mixer = new THREE.AnimationMixer( this.mesh );
console.log('perso geo ',self.perso_geo);
this.bounceClip = self.perso_geo.animations[0];
this.move_action = this.mixer.clipAction(this.bounceClip, null ).setDuration(0.5);
};
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.level.ground], false );
if ( intersects.length > 0 ) {
return intersects[0].point;
}
return null;
}
this.lookAtEvent= function(event)
{
var pos = this.getRayPos(event);
if(pos)
{
this.lookAt(pos);
}
}
this.lookAt= function(pos)
{
if(this.container)
{
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))
{
return;
}
this.is_moving=true;
// A: bottom right rectangle
// B: start
// C: destination
var distance_ab = Math.abs(current_pos.x - pos.x);
var distance_ac = Math.abs(current_pos.z - pos.z);
var total = distance_ab + distance_ac;
var ratio_x = (total-distance_ac) / total;
var ratio_z = (total-distance_ab) / total;
move_step_x = this.move_speed * ratio_x;
move_step_z = this.move_speed * ratio_z;
this.move_step_vector = new THREE.Vector2();
this.move_step_vector.x = current_pos.x>pos.x ? (- move_step_x) : move_step_x;
this.move_step_vector.z = current_pos.z>pos.z ? (- move_step_z) : move_step_z;
// Actually moving...
this.move_destination = pos;
this.move_action.play();
this.move_weight_destination = 1;
this.move_idx=0;
};
this.is_in_collision= function()
{
};
this.move_step= function()
{
if(this.is_moving)
{
this.move_idx++;
var moving=0;
var originPoint = this.container.position.clone();
if(Math.abs(this.container.position.x - this.move_destination.x) > this.move_speed || Math.abs(this.container.position.z - this.move_destination.z) > this.move_speed)
{
var originPoint = this.container.position.clone();
originPoint.add(this.move_step_vector);
var collision=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, directionVector.clone().normalize() );
var collisionResults = ray.intersectObjects( this.options.obstacles );
if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() )
collision=true;
}
if(!collision)
{
moving++;
this.container.position.add(this.move_step_vector);
}
}
else
{
this.container.position.setX(this.move_destination.x);
this.container.position.setZ(this.move_destination.z);
}
if(!moving)
{
this.move_weight_destination = 0;
this.is_moving=false;
this.move_destination=null;
}
}
else
{
this.move_action.stop();
}
};
this.move_weight = function()
{
if(this.move_weight_destination!==null)
{
if(!this.move_action)
{
return;
}
var c = this.move_action.getEffectiveWeight();
if(c>this.move_weight_destination)
{
this.move_action.setEffectiveWeight(c-0.1);
}
else if(c<this.move_weight_destination)
{
this.move_action.setEffectiveWeight(c+0.1);
}
else
{
this.move_weight_destination=null;
}
}
};
this.moveable = function()
{
var self=this;
this.trigger_move=false;
document.addEventListener( 'mousemove', function(e) { if(self.trigger_move) { self.moveToEvent(e); } }, false );
document.addEventListener( 'mouseup', function(e) { self.trigger_move=false; e.stopPropagation(); return false; }, false);
document.addEventListener( 'mousedown', function(e) { self.trigger_move=true;e.stopPropagation(); self.moveToEvent(e); return false; }, false);
};
this.update= function(delta)
{
this.mixer.update(delta);
this.move_step();
this.move_weight();
};
};