-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
79 lines (68 loc) · 2.56 KB
/
player.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
/* global BABYLON */
/* global World */
class Player {
constructor(id, username) {
var height = 20;
this.id = id;
this.mesh = BABYLON.MeshBuilder.CreateCapsule("player", { height: Avatar.height-5, radius: 6, topCapSubdivisions:6, capSubdivisions:1 }, World.scene);
var data = username.split(",");
Player.material.diffuseTexture = new BABYLON.Texture("./assets/skins/" + data[1], World.scene);
//this.mesh = BABYLON.MeshBuilder.CreateBox("avatar", {height: Avatar.height + 20, width: Avatar.width + 20, depth: Avatar.depth + 20}, World.scene);
this.mesh.position = BABYLON.Vector3.Zero();
this.mesh.position.x = 0;
this.mesh.position.y = height/2;
this.mesh.position.z = 0;
this.mesh.material = Player.material;
console.log(username)
this.skin = data[1];
this.billboard = new Billboard(this.mesh, data[0]);
Player.all.push(this);
}
//Destroy player meshes
destroy() {
this.billboard.mesh.dispose();
this.mesh.dispose();
}
//Find existing player or create if not exists
static find(playerID, username) {
//Check if player is in our list
for (var objPlayer of Player.all) {
if (objPlayer.id === playerID) {
//Found player, so lets return it
return(objPlayer);
}
};
//Player doesn't exist, so lets create a new one
return(new Player(playerID, username));
}
static init() {
Player.material = new BABYLON.StandardMaterial("matPlayer", World.scene);
}
//Find player and move them
static move(data) {
var playerID = parseInt(data.id);
var objPlayer = Player.find(playerID, data.username);
objPlayer.transform(data.x, data.y, data.z, data.rotation);
}
//Remove player from world
static remove(playerID) {
for (var objPlayer of Player.all) {
if (objPlayer.id === playerID) {
objPlayer.destroy();
break;
}
}
//Remove me from list of all players
Player.all = Player.all.filter((obj) => {
return obj.id !== playerID;
});
}
//Change position and rotation
transform(x, y, z, rotation) {
this.mesh.position.x = x;
this.mesh.position.y = y + 17;
this.mesh.position.z = z;
this.mesh.rotationQuaternion = BABYLON.Quaternion.FromEulerAngles(0, -rotation, 0);
}
}
Player.all = new Array();