Old stuff/old_sites/threejs/9_world/js/doors.js
(Deskargatu)
var Doors = function(game_mesh, options)
{
var self=this;
self.game_mesh = game_mesh;
this.mesh = [];
var material;
var depth;
var depth2;
var total_doors=0;
this.generated_doors = [];
this.data_doors = {};
this.load= function()
{
var self=this;
depth = (Math.sqrt(3)/2) * options.door_size*1.0;
depth2 = (Math.sqrt(3)/2) * options.door_size * Math.sqrt(3)/2 *1.3;
var texloader = new THREE.TextureLoader();
this.cubeTexture=texloader.load("textures/test.jpg");
return new Promise(function(ok, reject)
{
var loader = new THREE.JSONLoader();
loader.load( "js/meshes/door.js", function(geometry, mat)
{
self.door_geo = geometry;
self.door_mat = mat;
ok();
});
});
};
this.build = function()
{
var self=this;
self.data_doors=[];
var num_items_line = Math.floor(Math.sqrt(options.maze_num));
// Loop lines
for(var row=0; row<num_items_line; row++)
{
for(var line=0; line<num_items_line; line++)
{
self.create({ x: row, y: 1, z:line});
}
}
};
/* Memorize the doors created, to avoid creating doble contiguous doors */
this.register_door= function(x, z, i, cell)
{
if(x<0 || z<0)
{
return;
}
if(!this.data_doors[x+'.'+z])
{
this.data_doors[x+'.'+z]={};
}
this.data_doors[x+'.'+z][i] = 1;
var pair= x%2==0;
if(cell)
{
this.generated_doors.push({
x: cell.position.x,
y: cell.position.y,
z: cell.position.z
});
switch(i)
{
case 0: this.register_door(x, z+1, 3, false); break;
case 1: this.register_door(x+1, pair? z : z+1, 4, false); break;
case 2: this.register_door(x+1, pair ? z-1 : z, 5, false); break;
}
}
};
this.create = function(params)
{
var self=this;
var pivots=[];
this.fulldepth = options.door_size + options.door_size*2;
var pair = params.x%2 ? depth2 : 0;
var cell = new THREE.Object3D();
cell.position.x=params.x * depth *2;
cell.position.y=0;
cell.position.z=params.z * depth2 *2 + pair;
self.game_mesh.add(cell);
var total=this.mesh.length;
for(var i=0; i<6;i++)
{
if(!this.data_doors[params.x+'.'+params.z] || !this.data_doors[params.x+'.'+params.z][i])
{
this.register_door(params.x, params.z, i, cell);
total_doors++;
pivots[total] = new THREE.Object3D();
cell.add(pivots[total]);
var materials = [];
materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff }));
materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff }));
materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff }));
materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff }));
materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff }));
materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff }));
materials.push(new THREE.MeshLambertMaterial({ map: this.cubeTexture, color: 0xffffff }));
this.mesh[total] = new THREE.Mesh( self.door_geo, new THREE.MeshFaceMaterial(materials));
this.mesh[total].scale.x=options.door_size;
this.mesh[total].scale.y= options.door_size;
this.mesh[total].scale.z= options.door_size;
this.mesh[total].receiveShadow = true;
this.mesh[total].castShadow = true;
pivots[total].add( this.mesh[total] );
this.mesh[total].position.set(0, 0, options.door_size);
pivots[total].rotation.y= Math.radians(i*60);
total++;
}
}
self.pivots = pivots;
};
}