2017-11-15 13:42:29 +01:00
|
|
|
if (!this.entityLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(this.initialized)) {
|
|
|
|
|
|
|
|
this.initialized = true;
|
|
|
|
|
|
|
|
this.fog = this.entityLoaded.fog;
|
|
|
|
this.bull = this.entityLoaded.bull;
|
|
|
|
this.star = this.entityLoaded.star;
|
|
|
|
this.burger = this.entityLoaded.burger;
|
|
|
|
this.parcel = this.entityLoaded.parcel;
|
|
|
|
this.santa = this.entityLoaded.santa;
|
|
|
|
this.renderer = this.entityLoaded.renderer;
|
|
|
|
this.raycaster = this.entityLoaded.raycaster;
|
|
|
|
this.scene = this.entityLoaded.scene;
|
|
|
|
this.smokeParticleEngine = this.entityLoaded.smokeParticleEngine;
|
|
|
|
this.fireParticleEngine = this.entityLoaded.fireParticleEngine;
|
2017-11-15 19:35:28 +01:00
|
|
|
this.burningTreeParticleEngine = this.entityLoaded.burningTreeParticleEngine;
|
2017-11-15 13:42:29 +01:00
|
|
|
|
|
|
|
this.toBlack = new THREE.Color(0.05, 0.05, 0.05);
|
|
|
|
this.toRed = new THREE.Color(0.02, 0, 0);
|
|
|
|
|
|
|
|
this.bull.instance.visible = false;
|
|
|
|
this.star.instance.visible = false;
|
|
|
|
this.burger.instance.visible = false;
|
|
|
|
this.parcel.instance.visible = false;
|
|
|
|
this.santa.instance.visible = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This component is initializing - set the total time to 0
|
|
|
|
*/
|
|
|
|
this.totalTime = 0;
|
|
|
|
this.spawnTime = 0;
|
|
|
|
|
|
|
|
this.enemies = [];
|
|
|
|
|
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.GET_GRAPHICS_IMPLEMENTATION,
|
|
|
|
null,
|
|
|
|
function(graphics) {
|
|
|
|
this.graphics = graphics
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.getNextSpawnTime = function() {
|
|
|
|
return GameLib.Utils.GetRandomIntInclusive(
|
|
|
|
1,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.nextSpawnTime = this.getNextSpawnTime();
|
|
|
|
|
|
|
|
this.spawnEnemy = function() {
|
|
|
|
|
|
|
|
var enemyType = GameLib.Utils.GetRandomIntInclusive(1, 5);
|
|
|
|
var meshType = GameLib.Utils.GetRandomIntInclusive(1, 5);
|
|
|
|
|
|
|
|
var y = GameLib.Utils.GetRandomIntInclusive(2, 10);
|
|
|
|
|
|
|
|
var speed = 1;
|
|
|
|
|
|
|
|
var material = null;
|
|
|
|
|
|
|
|
var mesh = null;
|
|
|
|
if (meshType === 1) {
|
|
|
|
mesh = this.bull.clone();
|
|
|
|
material = this.bull.materials[0].clone();
|
|
|
|
speed = 0.2;
|
|
|
|
} else if (meshType === 2) {
|
|
|
|
mesh = this.star.clone();
|
|
|
|
material = this.star.materials[0].clone();
|
|
|
|
speed = 0.3;
|
|
|
|
} else if (meshType === 3) {
|
|
|
|
mesh = this.burger.clone();
|
|
|
|
material = this.burger.materials[0].clone();
|
|
|
|
speed = 0.4;
|
|
|
|
} else if (meshType === 4) {
|
|
|
|
mesh = this.parcel.clone();
|
|
|
|
material = this.parcel.materials[0].clone();
|
|
|
|
speed = 0.5;
|
|
|
|
} else if (meshType === 5) {
|
|
|
|
mesh = this.santa.clone();
|
|
|
|
material = this.santa.materials[0].clone();
|
|
|
|
speed = 0.5;
|
|
|
|
} else {
|
|
|
|
console.log('unknown mesh type : ' + meshType);
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh.materials = [material];
|
|
|
|
|
|
|
|
mesh.updateInstance();
|
|
|
|
|
|
|
|
mesh.instance.visible = true;
|
|
|
|
|
|
|
|
var velocity = null;
|
|
|
|
|
|
|
|
var axis = new GameLib.API.Vector3(
|
|
|
|
GameLib.Utils.GetRandomIntInclusive(1,10) * 0.1,
|
|
|
|
GameLib.Utils.GetRandomIntInclusive(1,10) * 0.1,
|
|
|
|
GameLib.Utils.GetRandomIntInclusive(1,10) * 0.1
|
|
|
|
);
|
|
|
|
|
|
|
|
axis = axis.normalize();
|
|
|
|
|
|
|
|
var angle = GameLib.Utils.GetRandomIntInclusive(1,100) * 0.001;
|
|
|
|
|
|
|
|
var position = {x : 0, y: y, z :0};
|
|
|
|
|
|
|
|
var rotation = {
|
|
|
|
axis : axis,
|
|
|
|
angle : angle
|
|
|
|
};
|
|
|
|
|
|
|
|
var enemy = {
|
|
|
|
mesh : mesh
|
|
|
|
};
|
|
|
|
|
|
|
|
if (enemyType === 1) {
|
|
|
|
position.x = -100;
|
|
|
|
velocity = {x:speed,y:0,z:0};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enemyType === 2) {
|
|
|
|
position.x = 100;
|
|
|
|
velocity = {x:-speed,y:0,z:0};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enemyType === 3) {
|
|
|
|
position.x = -100;
|
|
|
|
position.z = -100;
|
|
|
|
velocity = {x:speed,y:0,z:speed};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enemyType === 4) {
|
|
|
|
position.x = 100;
|
|
|
|
position.z = -100;
|
|
|
|
velocity = {x:-speed,y:0,z:speed};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enemyType === 5) {
|
|
|
|
position.x = 0;
|
|
|
|
position.z = -100;
|
|
|
|
velocity = {x:0,y:0,z:speed};
|
|
|
|
}
|
|
|
|
|
|
|
|
enemy.mesh.position.x = position.x;
|
|
|
|
enemy.mesh.position.y = position.y;
|
|
|
|
enemy.mesh.position.z = position.z;
|
|
|
|
enemy.mesh.updateInstancePosition();
|
|
|
|
|
|
|
|
enemy.mesh.useQuaternion = true;
|
|
|
|
enemy.mesh.quaternion.axis.x = rotation.axis.x;
|
|
|
|
enemy.mesh.quaternion.axis.y = rotation.axis.y;
|
|
|
|
enemy.mesh.quaternion.axis.z = rotation.axis.z;
|
|
|
|
enemy.mesh.quaternion.angle = rotation.angle;
|
|
|
|
enemy.mesh.updateInstanceRotation();
|
|
|
|
|
|
|
|
enemy.velocity = velocity;
|
|
|
|
enemy.rotation = rotation;
|
|
|
|
|
|
|
|
enemy.lifeTime = 0;
|
|
|
|
enemy.life = 15;
|
|
|
|
|
|
|
|
enemy.exploding = false;
|
|
|
|
enemy.burning = false;
|
|
|
|
enemy.burnLife = 3;
|
|
|
|
enemy.burnTime = 0;
|
|
|
|
enemy.explodingLife = 2;
|
|
|
|
|
|
|
|
this.enemies.push(enemy);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.explode = function(mesh) {
|
|
|
|
|
|
|
|
this.enemies.map(
|
|
|
|
function(enemy) {
|
|
|
|
if (enemy.mesh === mesh && !enemy.exploding) {
|
|
|
|
console.log('exploding enemy');
|
|
|
|
enemy.exploding = true;
|
|
|
|
// var particleEngine = this.smokeParticleEngine.clone();
|
|
|
|
// particleEngine.position = mesh.position.clone();
|
|
|
|
// particleEngine.updateInstance('position');
|
|
|
|
// particleEngine.enabled = true;
|
|
|
|
// enemy.smokeParticleEngine = particleEngine;
|
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.burn = function(mesh) {
|
|
|
|
|
|
|
|
this.enemies.map(
|
|
|
|
function(enemy) {
|
|
|
|
if (enemy.mesh === mesh && !enemy.burning) {
|
|
|
|
console.log('burning enemy ' + mesh.name);
|
|
|
|
enemy.burning = true;
|
|
|
|
var smokeParticleEngine = this.smokeParticleEngine.clone();
|
|
|
|
smokeParticleEngine.position = mesh.position.clone();
|
|
|
|
smokeParticleEngine.updateInstance('position');
|
|
|
|
smokeParticleEngine.enabled = true;
|
|
|
|
enemy.smokeParticleEngine = smokeParticleEngine;
|
|
|
|
|
|
|
|
var fireParticleEngine = this.fireParticleEngine.clone();
|
|
|
|
fireParticleEngine.position = mesh.position.clone();
|
|
|
|
fireParticleEngine.updateInstance('position');
|
|
|
|
fireParticleEngine.enabled = true;
|
|
|
|
enemy.fireParticleEngine = fireParticleEngine;
|
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2017-11-15 19:26:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.burnTree = function(mesh) {
|
|
|
|
|
2017-11-15 19:27:10 +01:00
|
|
|
if (mesh.burning) {
|
2017-11-15 19:26:19 +01:00
|
|
|
/**
|
|
|
|
* Do nothing, this tree is burning
|
|
|
|
*/
|
|
|
|
} else {
|
2017-11-15 19:27:10 +01:00
|
|
|
mesh.burning = true;
|
2017-11-15 19:26:19 +01:00
|
|
|
|
2017-11-15 21:40:59 +01:00
|
|
|
// var smokeParticleEngine = this.smokeParticleEngine.clone();
|
|
|
|
// smokeParticleEngine.position = mesh.position.clone();
|
|
|
|
// smokeParticleEngine.updateInstance('position');
|
|
|
|
// smokeParticleEngine.enabled = true;
|
2017-11-15 19:26:19 +01:00
|
|
|
|
2017-11-15 21:40:59 +01:00
|
|
|
// mesh.smokeParticleEngine = smokeParticleEngine;
|
2017-11-15 19:26:19 +01:00
|
|
|
|
2017-11-15 19:35:28 +01:00
|
|
|
var burningTreeParticleEngine = this.burningTreeParticleEngine.clone();
|
|
|
|
burningTreeParticleEngine.position = mesh.position.clone();
|
2017-11-15 21:17:19 +01:00
|
|
|
burningTreeParticleEngine.position.y += mesh.dimensions.y;
|
2017-11-15 19:35:28 +01:00
|
|
|
burningTreeParticleEngine.updateInstance('position');
|
|
|
|
burningTreeParticleEngine.enabled = true;
|
|
|
|
mesh.burningTreeParticleEngine = burningTreeParticleEngine;
|
2017-11-15 19:26:19 +01:00
|
|
|
}
|
2017-11-15 13:42:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.totalTime += data.delta;
|
|
|
|
this.spawnTime += data.delta;
|
|
|
|
|
|
|
|
this.scene.meshes.map(
|
|
|
|
function(mesh) {
|
|
|
|
if (mesh.instance.material instanceof Array) {
|
|
|
|
mesh.instance.material.map(
|
|
|
|
function(material) {
|
|
|
|
if (material.emissive) {
|
|
|
|
material.emissive.sub(this.toRed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (mesh.instance.material.emissive) {
|
|
|
|
mesh.instance.material.emissive.sub(this.toRed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.mouseIsDown) {
|
|
|
|
|
|
|
|
this.fog.color.r += 0.01;
|
|
|
|
if (this.fog.color.r > 0.5) {
|
|
|
|
this.fog.color.r = 0.5;
|
|
|
|
}
|
|
|
|
//this.fog.updateInstance('color');
|
|
|
|
|
|
|
|
this.renderer.clearColor.r += 0.01;
|
|
|
|
if (this.renderer.clearColor.r > 0.5) {
|
|
|
|
this.renderer.clearColor.r = 0.5;
|
|
|
|
}
|
|
|
|
this.renderer.updateInstance('clearColor');
|
|
|
|
|
|
|
|
var intersects = this.raycaster.getIntersectedObjects(this.scene.meshes);
|
|
|
|
intersects.map(
|
|
|
|
function(intersect) {
|
|
|
|
|
|
|
|
var mesh = intersect.mesh;
|
|
|
|
|
|
|
|
if (
|
2017-11-15 21:14:06 +01:00
|
|
|
mesh.name.indexOf('Tree - Medium') !== -1 || //tree medium
|
|
|
|
mesh.name.indexOf('Hamburger') !== -1 || //burger
|
|
|
|
mesh.name.indexOf('Star Cookie') !== -1 || //cookie
|
|
|
|
mesh.name.indexOf('Parcel') !== -1 || //parcel
|
|
|
|
mesh.name.indexOf('Bull') !== -1|| //bull
|
|
|
|
mesh.name.indexOf('Tree - Large') !== -1 || //tree large
|
|
|
|
mesh.name.indexOf('Santa') !== -1 || //santa
|
2017-11-15 21:14:44 +01:00
|
|
|
mesh.name.indexOf('Tree - Small') !== -1 //tree small
|
2017-11-15 13:42:29 +01:00
|
|
|
) {
|
|
|
|
|
|
|
|
if (mesh.materials[0].name.indexOf('Trees') === -1) {
|
|
|
|
|
|
|
|
mesh.instance.material.color.sub(this.toBlack);
|
|
|
|
|
|
|
|
mesh.materials[0].color.r = mesh.instance.material.color.r;
|
|
|
|
mesh.materials[0].color.g = mesh.instance.material.color.g;
|
|
|
|
mesh.materials[0].color.b = mesh.instance.material.color.b;
|
|
|
|
|
|
|
|
mesh.materials[0].color.instance = mesh.instance.material.color;
|
|
|
|
|
|
|
|
if (mesh.instance.material.color.r <= 0) {
|
|
|
|
|
|
|
|
this.burn(mesh);
|
|
|
|
|
|
|
|
if (mesh.instance.material.emissive) {
|
|
|
|
mesh.instance.material.emissive.add(this.toRed);
|
|
|
|
mesh.instance.material.emissive.add(this.toRed);
|
|
|
|
|
|
|
|
if (mesh.instance.material.emissive.r >= 0.8) {
|
|
|
|
this.explode(mesh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-15 19:26:19 +01:00
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* This happens to trees
|
|
|
|
*/
|
|
|
|
mesh.instance.material.color.sub(this.toBlack);
|
|
|
|
|
|
|
|
mesh.materials[0].color.r = mesh.instance.material.color.r;
|
|
|
|
mesh.materials[0].color.g = mesh.instance.material.color.g;
|
|
|
|
mesh.materials[0].color.b = mesh.instance.material.color.b;
|
|
|
|
|
|
|
|
// Don't do the color instance
|
|
|
|
mesh.materials[0].color.instance = mesh.instance.material.color;
|
|
|
|
|
|
|
|
if (mesh.instance.material.color.r <= 0) {
|
|
|
|
this.burnTree(mesh);
|
|
|
|
|
|
|
|
if (mesh.instance.material.emissive) {
|
|
|
|
mesh.instance.material.emissive.add(this.toRed);
|
|
|
|
mesh.instance.material.emissive.add(this.toRed);
|
|
|
|
|
|
|
|
//if (mesh.instance.material.emissive.r >= 0.8) {
|
|
|
|
// this.explode(mesh);
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-11-15 13:42:29 +01:00
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.renderer.clearColor.r -= 0.01;
|
|
|
|
if (this.renderer.clearColor.r < 0.32) {
|
|
|
|
this.renderer.clearColor.r = 0.32;
|
|
|
|
}
|
|
|
|
this.renderer.updateInstance('clearColor');
|
|
|
|
|
|
|
|
this.fog.color.r -= 0.01;
|
|
|
|
if (this.fog.color.r < 0.32) {
|
|
|
|
this.fog.color.r = 0.32;
|
|
|
|
}
|
|
|
|
// this.fog.updateInstance('color');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.spawnTime > this.nextSpawnTime) {
|
|
|
|
this.nextSpawnTime = this.getNextSpawnTime();
|
|
|
|
this.spawnTime = 0;
|
|
|
|
this.spawnEnemy();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.enemies = this.enemies.reduce(
|
|
|
|
function(result, enemy) {
|
|
|
|
enemy.mesh.position.add(enemy.velocity);
|
|
|
|
enemy.mesh.updateInstancePosition();
|
|
|
|
|
|
|
|
enemy.mesh.quaternion.angle += enemy.rotation.angle;
|
|
|
|
enemy.mesh.updateInstanceRotation();
|
|
|
|
|
|
|
|
enemy.lifeTime += data.delta;
|
|
|
|
|
|
|
|
if (enemy.lifeTime > enemy.life) {
|
|
|
|
/**
|
|
|
|
* We only remove the things we cloned
|
|
|
|
*/
|
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
|
|
{
|
|
|
|
component : enemy.mesh.materials[0]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.REMOVE_COMPONENT,
|
|
|
|
{
|
|
|
|
component : enemy.mesh
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (enemy.burning) {
|
|
|
|
enemy.smokeParticleEngine.remove();
|
|
|
|
enemy.fireParticleEngine.remove();
|
|
|
|
enemy.burning = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (enemy.burning) {
|
|
|
|
enemy.burnTime += data.delta;
|
|
|
|
if (enemy.burnTime > enemy.burnLife) {
|
|
|
|
enemy.smokeParticleEngine.remove();
|
|
|
|
enemy.fireParticleEngine.remove();
|
|
|
|
enemy.lifeTime = enemy.life + 1;
|
|
|
|
enemy.burning = false;
|
|
|
|
} else {
|
|
|
|
enemy.smokeParticleEngine.position = enemy.mesh.position.clone();
|
|
|
|
enemy.smokeParticleEngine.updateInstance('position');
|
|
|
|
enemy.fireParticleEngine.position = enemy.mesh.position.clone();
|
|
|
|
enemy.fireParticleEngine.updateInstance('position');
|
|
|
|
}
|
|
|
|
|
|
|
|
enemy.mesh.materials[0].opacity -= 0.005;
|
|
|
|
enemy.mesh.materials[0].updateInstance('opacity');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push(enemy);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
//# sourceURL=beforeRender.js
|