forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntities.js
More file actions
71 lines (68 loc) · 2.45 KB
/
Entities.js
File metadata and controls
71 lines (68 loc) · 2.45 KB
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
import * as THREE from "three";
class Entities {
constructor(game) {
this.game = game;
this.mobMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color("red"),
});
this.mobGeometry = new THREE.BoxGeometry(1, 1, 1);
this.mobMaxCount = 200;
this.mobMesh = new THREE.InstancedMesh(
this.mobGeometry,
this.mobMaterial,
this.mobMaxCount
);
this.mobMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
this.game.scene.add(this.mobMesh);
this.playerMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color("blue"),
});
this.playerGeometry = new THREE.BoxGeometry(1, 1, 1);
this.playerMaxCount = 200;
this.playerMesh = new THREE.InstancedMesh(
this.playerGeometry,
this.playerMaterial,
this.playerMaxCount
);
this.playerMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
this.game.scene.add(this.playerMesh);
this.dummy = new THREE.Object3D();
}
update(entities) {
var offset = [-0.5, 16, -0.5];
var num_mobs = 0;
this.mobMesh.count = entities.mobs.length;
num_mobs = 0;
for (let i in entities.mobs) {
this.dummy.position.set(
entities.mobs[i][0] + offset[0],
entities.mobs[i][1] + offset[1],
entities.mobs[i][2] + offset[2]
);
this.dummy.updateMatrix();
this.mobMesh.setMatrixAt(num_mobs++, this.dummy.matrix);
}
this.mobMesh.instanceMatrix.needsUpdate = true;
var num_players = 0;
for (let i in entities.players) {
if (entities.players[i][0] !== this.game.nick) {
num_players++;
}
}
this.playerMesh.count = num_players;
num_players = 0;
for (let i in entities.players) {
if (entities.players[i][0] !== this.game.nick) {
this.dummy.position.set(
entities.players[i][1] + offset[0],
entities.players[i][2] + offset[1],
entities.players[i][3] + offset[2]
);
this.dummy.updateMatrix();
this.playerMesh.setMatrixAt(num_players++, this.dummy.matrix);
}
}
this.playerMesh.instanceMatrix.needsUpdate = true;
}
}
export { Entities };