r3-custom-code/dywmtohrda.js

263 lines
5.8 KiB
JavaScript

if (R3.Utils.UndefinedOrNull(this.entityLoaded)) {
/**
* This component is not ready to run
*/
return;
}
if (
R3.Utils.UndefinedOrNull(this.totalTime) ||
R3.Utils.UndefinedOrNull(this.spawnTime) ||
R3.Utils.UndefinedOrNull(this.spawnBulletFrequency) ||
R3.Utils.UndefinedOrNull(this.camera) ||
R3.Utils.UndefinedOrNull(this.scene) ||
R3.Utils.UndefinedOrNull(this.sceneCrosshair)
//|| true
) {
/**
* This component is initializing - set the total time to 0
*/
this.totalTime = 0;
this.spawnTime = 0;
this.spawnBulletFrequency = 0.1;
this.enemies = [];
R3.Event.Emit(
R3.Event.GET_GRAPHICS_IMPLEMENTATION,
null,
function(graphics) {
this.graphics = graphics
}.bind(this)
);
this.getNextSpawnTime = function() {
return R3.Utils.GetRandomIntInclusive(
R3.D3.CustomCode.MIN_SPAWN_TIME,
R3.D3.CustomCode.MAX_SPAWN_TIME
);
}
this.boxMaterial = R3.EntityManager.Instance.findComponentById('dsaua5t19a');
this.tree = R3.EntityManager.Instance.findComponentById('fcj62ukx5s');
this.camera = R3.EntityManager.Instance.findComponentById('yv62w8sx9r');
this.scene = R3.EntityManager.Instance.findComponentById('fd2be8y1c8');
this.sceneCrosshair = R3.EntityManager.Instance.findComponentById('7xkcp6x9pf');
this.meshCrosshair = R3.EntityManager.Instance.findComponentById('oxpg88pgzv');
this.meshFireBall = R3.EntityManager.Instance.findComponentById('zmcwqlym5y');
this.flames = [];
for (var i = 0; i < 100; i++) {
var tree = this.tree.clone();
tree.position.x = Math.floor(Math.random() * 1000) - 500;
tree.position.y = Math.floor(Math.random() * 60) - 30;
tree.position.z = Math.floor(Math.random() * 1000) - 500;
tree.updateInstance();
}
this.tree.position.x = Math.floor(Math.random() * 1000) - 500;
this.tree.position.y = Math.floor(Math.random() * 60) - 30;
this.tree.position.z = Math.floor(Math.random() * 1000) - 500;
this.tree.updateInstance();
this.nextSpawnTime = this.getNextSpawnTime();
this.spawnEnemy = function() {
var enemyType = Math.floor((Math.random() * 5) + 1);
var y = Math.floor((Math.random() * 40) + 2);
var apiBox = new R3.D3.API.Mesh();
apiBox.materials = [this.boxMaterial];
var box = new R3.D3.Mesh.Box(this.graphics, apiBox, 4,4,4);
var velocity = null;
var position = {x : 0, y: y, z :0};
var enemy = {
mesh : box
};
if (enemyType === 1) {
position.x = -500;
velocity = {x:1,y:0,z:0};
}
if (enemyType === 2) {
position.x = 500;
velocity = {x:-1,y:0,z:0};
}
if (enemyType === 3) {
position.x = -500;
position.z = -500;
velocity = {x:1,y:0,z:1};
}
if (enemyType === 4) {
position.x = 500;
position.z = -500;
velocity = {x:-1,y:0,z:1};
}
if (enemyType === 5) {
position.x = 0;
position.z = -500;
velocity = {x:0,y:0,z:1};
}
enemy.mesh.position.x = position.x;
enemy.mesh.position.y = position.y;
enemy.mesh.position.z = position.z;
enemy.mesh.updateInstancePosition();
enemy.velocity = velocity;
enemy.lifeTime = 0;
enemy.life = 50;
this.enemies.push(enemy);
}
}
this.totalTime += data.delta;
this.spawnTime += data.delta;
if (this.spawnTime > this.nextSpawnTime) {
this.nextSpawnTime = Math.floor((Math.random() * 1) + 1);
this.spawnTime = 0;
this.spawnEnemy();
}
var flamesLength = this.flames.length;
this.flames.map(
function(flame, index) {
flame.mesh.position.add(flame.direction);
flame.mesh.updateInstancePosition();
//flame.mesh.instance.renderOrder = index;
}
);
if (this.spawningBullets === true) {
if (this.spawnBulletTime === 0) {
var material = new THREE.LineBasicMaterial({
color: 0xff0000,
linewidth: 5
});
var position = this.camera.position.instance.clone();
var lookAt = this.camera.lookAt.instance.clone();
var direction = lookAt.sub(position).normalize();
var start = new THREE.Vector3(0,0,0.5);
start.unproject(this.camera.instance);
var end = direction.clone().multiplyScalar(1000);
console.log('start: ', start);
console.log('end: ', end);
this.meshFireBall.renderOrder = Number(1000 - Number(this.flames.length));
var flameMesh = this.meshFireBall.clone();
flameMesh.position.x = start.x;
flameMesh.position.y = start.y;
flameMesh.position.z = start.z;
flameMesh.instance.lookAt(direction.clone().negate());
flameMesh.updateInstancePosition();
this.flames.push({
mesh : flameMesh,
direction: direction
});
//var geometry = new THREE.Geometry();
//geometry.vertices.push(
// start,
// end
//);
//var line = new THREE.Line( geometry, material );
//this.scene.instance.add( line );
var raycaster = new THREE.Raycaster();
raycaster.ray = new THREE.Ray(
this.camera.position.instance,
start.clone().sub(this.camera.position.instance).normalize()
);
var intersects = raycaster.intersectObjects(
R3.EntityManager.Instance.queryComponents(R3.D3.Mesh).map(
function(mesh){
return mesh.instance;
}
)
);
if (intersects.length > 0) {
console.log('intersected');
intersects.map(
function(intersect){
intersect.object.visible = false;
}
)
}
}
this.spawnBulletTime += data.delta;
if (this.spawnBulletTime > this.spawnBulletFrequency) {
this.spawnBulletTime = 0;
}
}
this.scene.instance.children.map(
function(child) {
if (child instanceof THREE.Line) {
child.position.multiply(new THREE.Vector3(0,-0.1,0));
}
}
);
//this.entityLoaded.updateMaterialRusted(this.totalTime);
this.enemies = this.enemies.reduce(
function(result, enemy) {
enemy.mesh.position.add(enemy.velocity);
enemy.mesh.updateInstancePosition();
enemy.lifeTime += data.delta;
if (enemy.lifeTime > enemy.life) {
R3.Event.Emit(
R3.Event.REMOVE_COMPONENT,
{
component : enemy.mesh
}
);
} else {
result.push(enemy);
}
return result;
},
[]
);
return null;
//# sourceURL=beforeRender.js