-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnitsManager.js
83 lines (70 loc) · 2.03 KB
/
UnitsManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as THREE from "three";
import { generateRandFloat, generateRandInt } from "./utils/helpers";
import { scoreManager } from "./ScoreManager";
import { Events } from "./utils/constants";
class UnitsManager {
constructor() {
this.activeObjects = new THREE.Group();
this.material = new THREE.MeshLambertMaterial();
this.lastSpawned = 0;
}
get randomGeometry() {
const geometry = new THREE.BoxGeometry(
generateRandFloat(0.3, 0.6),
generateRandFloat(0.3, 0.6),
1
);
return geometry;
}
get randomMaterial() {
const colors = [
THREE.Color.NAMES.cyan,
THREE.Color.NAMES.blueviolet,
THREE.Color.NAMES.yellow,
THREE.Color.NAMES.magenta,
THREE.Color.NAMES.hotpink,
THREE.Color.NAMES.greenyellow,
];
const randomIdx = Math.floor(generateRandInt(0, colors.length - 1));
const randomMat = new THREE.MeshLambertMaterial({
color: new THREE.Color(colors[randomIdx]),
});
//this.material.color = new THREE.Color(colors[randomIdx])
randomMat.emissiveIntensity = 0;
return randomMat;
}
_spawnCube() {
const newCube = new THREE.Mesh(this.randomGeometry, this.randomMaterial);
newCube.position.setX(generateRandInt(-2, 2));
newCube.position.setY(generateRandInt(1, 2));
newCube.position.setZ(-12);
console.log("new cube");
this.activeObjects.add(newCube);
}
handleAnimateObjects(deltaTime) {
this.activeObjects.children.forEach((obj, i) => {
obj.position.z += 7 * deltaTime;
});
}
_handleAddObjects() {
//should spawn every 600ms
const now = Date.now();
if (now > this.lastSpawned + 700) {
this._spawnCube();
this.lastSpawned = now;
}
}
handleObjects() {
this._handleAddObjects();
this._handleRemoveObjects();
}
_handleRemoveObjects() {
this.activeObjects.children.forEach((obj) => {
if (obj.position.z >= 8) {
this.activeObjects.remove(obj);
scoreManager.score--;
}
});
}
}
export const unitsManager = new UnitsManager();