Merge pull request #38 from Protospace/optimize-laser

Update Laser.js
This commit is contained in:
Tanner Collin 2021-04-03 18:06:55 -06:00 committed by GitHub
commit 5abb91d838
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 35 deletions

View File

@ -3,6 +3,12 @@ import * as THREE from 'three';
const LASER_SIZE = 0.5;
const BULLET_SPREAD = 0.1;
const geometry = new THREE.BoxGeometry(
LASER_SIZE * 0.03,
LASER_SIZE * 0.03,
LASER_SIZE
);
export class Laser {
constructor(ship) {
const position = new THREE.Vector3();
@ -12,17 +18,13 @@ export class Laser {
this.stepX = (Math.random() - 0.5) * BULLET_SPREAD;
this.stepY = (Math.random() - 0.5) * BULLET_SPREAD;
ship.mesh.getWorldPosition(position)
ship.mesh.getWorldPosition(position);
const shipGeo = new THREE.BoxGeometry(
LASER_SIZE * 0.03,
LASER_SIZE * 0.03,
LASER_SIZE
);
this.mesh = new THREE.Mesh(
shipGeo,
new THREE.MeshStandardMaterial(this.direction > 0 ? 0xff0000 : 0x0000ff, { flatShading: true })
);
const material = new THREE.MeshStandardMaterial(0xffffff, {
flatShading: true,
});
this.mesh = new THREE.Mesh(geometry, material);
this.mesh.material.color.set(
new THREE.Color(`hsl(${ship.direction > 0 ? 0 : 180},100%,70%)`)

View File

@ -2,6 +2,14 @@ import * as THREE from 'three';
const SHIP_SIZE = 1;
const geometry = new THREE.BoxGeometry(
SHIP_SIZE * 0.1,
SHIP_SIZE * 0.1,
SHIP_SIZE
);
const burstGeo = new THREE.SphereGeometry(0.1, 32, 32);
export class Ship {
constructor(direction) {
this.direction = direction;
@ -10,18 +18,13 @@ export class Ship {
this.shotInterval = 3;
this.flyIn = true;
this.firing = false;
this.exploded = false;
const shipGeo = new THREE.BoxGeometry(
SHIP_SIZE * 0.1,
SHIP_SIZE * 0.1,
SHIP_SIZE
);
this.mesh = new THREE.Mesh(
shipGeo,
new THREE.MeshStandardMaterial(this.direction > 0 ? 0xff0000 : 0x0000ff, { flatShading: true })
);
const material = new THREE.MeshStandardMaterial(0xffffff, {
flatShading: true,
});
this.burstGeo = new THREE.SphereGeometry(0.1, 32, 32);
this.mesh = new THREE.Mesh(geometry, material);
this.y = (Math.random() - 0.5) * 4;
this.x = (Math.random() - 0.5) * 4;
@ -41,14 +44,9 @@ export class Ship {
this.mesh.scale.z = 10;
this.mesh.position.z += 4.75 * this.direction;
// ship accelerating decreasing
// check if in space
if (Math.abs(this.mesh.position.z) <= 6 && this.flyIn) {
this.flyIn = false;
this.mesh.scale.z = 0.5;
//this.mesh.material.color.set(
// new THREE.Color(`hsl(${this.hue},0%,30%)`)
//);
}
} else {
this.mesh.scale.z = 0.5;
@ -62,7 +60,9 @@ export class Ship {
);
const xs = Math.sin(this.life * 0.5 + this.x);
const yrot = Math.sin(this.life + this.x + (3.14/2 * this.direction));
const yrot = Math.sin(
this.life + this.x + (3.14 / 2) * this.direction
);
this.mesh.position.y = this.y + Math.sin(this.life + this.y) * 0.1;
this.mesh.position.x = this.x + xs * 0.15;
this.mesh.rotation.x = yrot * 0.05;
@ -74,14 +74,20 @@ export class Ship {
}
}
if (this.life < 0 && !this.flyin) {
this.mesh.geometry = this.burstGeo;
if (this.life < 0 && !this.flyin && !this.exploded) {
this.mesh.geometry = burstGeo;
this.mesh.material.transparent = true;
this.mesh.material.opacity *= 0.95;
this.exploded = true;
this.mesh.scale.x = 1;
this.mesh.scale.y = 1;
this.mesh.scale.z = 1;
}
if (this.exploded) {
this.mesh.scale.x *= 1.1;
this.mesh.scale.y *= 1.1;
this.mesh.scale.z *= 1.1;
this.mesh.material.transparent = true;
this.mesh.material.opacity *= 0.95;
}
if (this.mesh.scale.x > 10) {