-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
224 lines (182 loc) · 6.08 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var scene;
var player;
var player_mesh;
var player_color;
var update_player_skin = true;
var walk_speed = 0.0;
var max_walk_speed = 1.0;
var started_walking = 0.0;
var stopped_walking = 0.0;
var walking = false;
var acceleration = 1.0;
var player_height;
function Player(opts) {
if (!(this instanceof Player)) return new Player(opts || {});
scene = opts.scene;
player_color = opts.player_color;
player_height = opts.player_height;
// Get each body part
let body_parts_options = this.get_body_parts_options();
// Create each of them
let player_head = BABYLON.MeshBuilder.CreateBox('player_head', body_parts_options[0], scene);
let player_body = BABYLON.MeshBuilder.CreateBox('player_body', body_parts_options[1], scene);
let player_right_arm = BABYLON.MeshBuilder.CreateBox('player_right_arm', body_parts_options[2], scene);
let player_left_arm = BABYLON.MeshBuilder.CreateBox('player_left_arm', body_parts_options[3], scene);
let player_right_leg = BABYLON.MeshBuilder.CreateBox('player_right_leg', body_parts_options[4], scene);
let player_left_leg = BABYLON.MeshBuilder.CreateBox('player_left_leg', body_parts_options[5], scene);
// Add pivot/joints for arm and legs
let matrix = BABYLON.Matrix.Translation(0, -0.5, 0);
player_right_arm.bakeTransformIntoVertices(matrix);
player_left_arm.bakeTransformIntoVertices(matrix);
player_right_leg.bakeTransformIntoVertices(matrix);
player_left_leg.bakeTransformIntoVertices(matrix);
player = new BABYLON.SolidParticleSystem("Player", scene);
player.addShape(player_head, 1);
player.addShape(player_body, 1);
player.addShape(player_right_arm, 1);
player.addShape(player_left_arm, 1);
player.addShape(player_right_leg, 1);
player.addShape(player_left_leg, 1);
player_mesh = player.buildMesh();
player_head.dispose();
player_body.dispose();
player_right_arm.dispose();
player_left_arm.dispose();
player_left_leg.dispose();
player_right_leg.dispose();
// Player init
player.initParticles = function () {
// Head
player.particles[0].position.y += 1.25;
// Body
player.particles[1].position.y = 0;
// Right arm
player.particles[2].position.x += 0.75;
player.particles[2].position.y += 0.5;
// Left arm
player.particles[3].position.x -= 0.75;
player.particles[3].position.y += 0.5;
// Right leg
player.particles[4].position.x -= 0.25;
player.particles[4].position.y -= 1.;
// Left leg
player.particles[5].position.x += 0.25;
player.particles[5].position.y -= 1.;
};
// Player animation
player.updateParticle = function (particle) {
let time = Date.now() / 1000
if (walking && time < started_walking + acceleration) {
walk_speed = (time - started_walking) / acceleration;
}
if (!walking && time < stopped_walking + acceleration) {
walk_speed = -1 / acceleration * (time - stopped_walking) + 1;
}
// Range-check
walk_speed = Math.min(Math.abs(walk_speed), max_walk_speed);
// Head
player.particles[0].rotation.y = Math.sin(time * 1.5) / 3 * walk_speed;
player.particles[0].rotation.x = Math.sin(time) / 2 * walk_speed;
// Arms
player.particles[2].rotation.x = 2 * Math.cos(0.6662 * time * 10 + Math.PI) * walk_speed;
player.particles[3].rotation.x = 2 * Math.cos(0.6662 * time * 10) * walk_speed;
// Legs
player.particles[4].rotation.x = 1.4 * Math.cos(0.6662 * time * 10) * walk_speed;
player.particles[5].rotation.x = 1.4 * Math.cos(0.6662 * time * 10 + Math.PI) * walk_speed;
if (update_player_skin) {
player.mesh.hasVertexAlpha = true;
player.particles[0].color = player_color;
player.particles[1].color = player_color;
player.particles[2].color = player_color;
player.particles[3].color = player_color;
player.particles[4].color = player_color;
player.particles[5].color = player_color;
update_player_skin = false;
}
};
// Init particle system
player.initParticles();
player.setParticles();
}
module.exports = Player;
Player.prototype.update_particles = function() {
player.setParticles();
};
Player.prototype.get_player_mesh = function() {
return player_mesh;
};
Player.prototype.get_player_height = function() {
return player_height;
};
Player.prototype.set_player_color = function(color) {
player_color = color;
update_player_skin = true;
};
Player.prototype.start_walking = function() {
let now = Date.now() / 1000;
walking = true;
if (stopped_walking + acceleration > now) {
let progress = now - stopped_walking;
started_walking = now - (stopped_walking + acceleration - now);
} else {
started_walking = Date.now() / 1000;
}
};
Player.prototype.stop_walking = function() {
let now = Date.now() / 1000;
walking = false;
if (started_walking + acceleration > now) {
stopped_walking = now - (started_walking + acceleration - now);
} else {
stopped_walking = Date.now() / 1000;
}
};
Player.prototype.is_walking = function() {
return walking;
};
// Makes all the options for each body part
// And initializes the UV of each face
Player.prototype.get_body_parts_options = function() {
let body_options = new Array(6);
// Head
body_options[0] = {
size: 1,
updatable: true,
};
// Body
body_options[1] = {
width: 1,
depth: 0.5,
height: player_height,
updatable: true,
};
// Right Arm
body_options[2] = {
width: 0.5,
depth: 0.5,
height: player_height,
updatable: true,
};
// Left Arm
body_options[3] = {
width: 0.5,
depth: 0.5,
height: player_height,
updatable: true,
};
// Right Leg
body_options[4] = {
width: 0.5,
depth: 0.5,
height: player_height,
updatable: true,
};
// Left Leg
body_options[5] = {
width: 0.5,
depth: 0.5,
height: player_height,
updatable: true,
};
return body_options;
};