Delete bolts that are far away

This commit is contained in:
Tanner Collin 2021-03-21 02:22:22 +00:00
parent cfe4cb3382
commit 3529916726
3 changed files with 18 additions and 4 deletions

View File

@ -6,6 +6,7 @@ export class Laser {
constructor(ship) { constructor(ship) {
const position = new THREE.Vector3(); const position = new THREE.Vector3();
this.direction = ship.direction; this.direction = ship.direction;
this.kill = false;
ship.mesh.getWorldPosition(position) ship.mesh.getWorldPosition(position)
@ -28,5 +29,9 @@ export class Laser {
update({ deltaTime }) { update({ deltaTime }) {
this.mesh.position.z += 0.5 * this.direction; this.mesh.position.z += 0.5 * this.direction;
if (Math.abs(this.mesh.position.z > 475/2)) {
this.kill = true;
}
} }
} }

View File

@ -52,10 +52,11 @@ export class Ship {
this.life -= deltaTime; this.life -= deltaTime;
if (!this.flyIn) { if (!this.flyIn) {
const xs = Math.sin(this.life*0.5 + this.x); const xs = Math.sin(this.life * 0.5 + this.x);
this.mesh.position.y = this.y + Math.sin(this.life + this.y) * 0.08; const yrot = Math.sin(this.life + this.x + (3.14/2 * this.direction));
this.mesh.position.x = this.x + xs * 0.08; this.mesh.position.y = this.y + Math.sin(this.life + this.y) * 0.2;
this.mesh.rotation.y = xs*0.25; this.mesh.position.x = this.x + xs * 0.15;
this.mesh.rotation.x = yrot * 0.15;
this.mesh.position.z += 0.01 * this.direction; this.mesh.position.z += 0.01 * this.direction;
this.nextShot -= deltaTime; this.nextShot -= deltaTime;
if (this.nextShot <= 0) { if (this.nextShot <= 0) {
@ -69,6 +70,9 @@ export class Ship {
this.mesh.position.z += a * 2 * this.direction; this.mesh.position.z += a * 2 * this.direction;
this.mesh.scale.z = a * 4; this.mesh.scale.z = a * 4;
this.firing = false; this.firing = false;
this.mesh.rotation.y = 0;
this.mesh.rotation.x = 0;
} }
if (Math.abs(this.mesh.position.z > 475/2)) { if (Math.abs(this.mesh.position.z > 475/2)) {

View File

@ -82,9 +82,14 @@ export const scene = ({ ref }) => {
for (const bolt of bolts) { for (const bolt of bolts) {
bolt.update({ deltaTime }); bolt.update({ deltaTime });
if (bolt.kill) {
scene.remove(bolt.mesh);
}
} }
ships = ships.filter((s) => !s.kill); ships = ships.filter((s) => !s.kill);
bolts = bolts.filter((s) => !s.kill);
requestAnimationFrame(animate); requestAnimationFrame(animate);
renderer.render(scene, camera); renderer.render(scene, camera);