-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
354 lines (295 loc) · 9.72 KB
/
main.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
var socket = io();
var verbose = true;
var Meshes = {},
Materials = {},
Sounds = {}
var canvas;
var engine;
var scene;
var camera;
var BulletConstructor = new BulletConstructor();
var BoxManager = new BoxManager();
var PlayerManager = new PlayerConstructor();
function Game() {
canvas = document.getElementById("renderCanvas"); // Get the canvas element
engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
scene = new BABYLON.Scene(engine);
camera = new BABYLON.UniversalCamera('Camera', new BABYLON.Vector3(0, 200, 0), scene);
camera.setTarget(BABYLON.Vector3.Zero());
//camera.attachControl(canvas, true);
camera.keysUp.push(87);
camera.keysDown.push(83);
camera.keysLeft.push(65);
camera.keysRight.push(68);
camera.applyGravity = true;
camera.checkCollisions = true;
camera.ellipsoid = new BABYLON.Vector3(2, 10, 2);
camera.checkCollisions = true;
// Enable physics in the scene
var gravityVector = new BABYLON.Vector3(0, 0, 0);
var physicsPlugin = new BABYLON.OimoJSPlugin();
scene.enablePhysics(gravityVector, physicsPlugin);
this.loadResources(this.onLoadingComplete.bind(this));
}
Game.prototype.captureKeys = function() {
document.onkeydown = this.onKeyDown.bind(this),
document.onkeyup = this.onKeyUp.bind(this)
}
Game.prototype.onKeyDown = function(e) {
var inputToControlMap = {
87: "W",
68: "D",
83: "S",
65: "A",
80: "P",
39: "right",
37: "left"
}
var keyPressed = inputToControlMap[e.keyCode];
switch(keyPressed) {
case "W":
this.player.pressingW = true;
break;
case "D":
this.player.pressingD = true;
break;
case "S":
this.player.pressingS = true;
break;
case "A":
this.player.pressingA = true;
break;
case "right":
this.player.pressingRight = true;
break;
case "left":
this.player.pressingLeft = true;
break;
case "P":
camera = new BABYLON.UniversalCamera('Camera', new BABYLON.Vector3(0, 200, 0), scene);
camera.setTarget(new BABYLON.Vector3(0,0,0));
break;
case "L":
camera.parent = this.player;
camera.rotation.x = 0;
default:
break;
}
}
Game.prototype.onKeyUp = function(e) {
var inputToControlMap = {
87: "W",
68: "D",
83: "S",
65: "A",
80: "P",
39: "right",
37: "left"
}
var keyPressed = inputToControlMap[e.keyCode];
switch(keyPressed) {
case "W":
this.player.pressingW = false;
break;
case "D":
this.player.pressingD = false;
break;
case "S":
this.player.pressingS = false;
break;
case "A":
this.player.pressingA = false;
break;
case "right":
this.player.pressingRight = false;
break;
case "left":
this.player.pressingLeft = false;
break;
case "P":
camera = new BABYLON.UniversalCamera('Camera', new BABYLON.Vector3(0, 200, 0), scene);
camera.setTarget(new BABYLON.Vector3(0,0,0));
break;
case "L":
camera.parent = this.player;
camera.rotation.x = 0;
default:
break;
}
}
function BoxManager() {
this.list = {};
this.create = function() {
var size = 2;
// create the box into the scene
var box = BABYLON.MeshBuilder.CreateBox('box', {
height: size,
width: size,
depth: size
}, scene);
box.id = Math.random();
box.physicsImpostor = new BABYLON.PhysicsImpostor(box, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 100000, restitution: 0.9 }, scene);
box.material = Materials.woodenCrate;
box.position = new BABYLON.Vector3(30, 0, 0);
box.update = function() {
}
this.list[box.id] = box;
return box;
}
this.update = function() {
for(var id in this.list) {
this.list[id].update();
}
}
}
Game.prototype.onLoadingComplete = function() {
this.buildMap();
var id = Math.random();
this.player = PlayerManager.create(id);
camera.parent = this.player;
camera.position = new BABYLON.Vector3(0,0,0);
camera.rotation.x = 0;
socket.on("playerResponse", function(data) {
for(var id in data) {
if(!PlayerManager.list[id]) {
PlayerManager.create(id);
} else {
PlayerManager.list[id].position.x = data[id].x;
PlayerManager.list[id].position.y = data[id].y;
PlayerManager.list[id].position.z = data[id].z;
}
/*
if(!PlayerManager.list[id] && !EnemyManager.list[id]) {
// if it is not a player, and enemy has not been created yet, create an enemy at position
EnemyManager.create(id)
} else if(EnemyManager.list[id]) {
EnemyManager.list[id].position.x = data[id].x;
EnemyManager.list[id].position.y = data[id].y;
EnemyManager.list[id].position.z = data[id].z;
Ee
}
*/
}
})
/*
setInterval(function() {
PlayerManager.update()
}, 1000/60);
*/
engine.runRenderLoop(function () { // Register a render loop to repeatedly render the scene
this.captureKeys();
this.player.update();
BulletConstructor.update();
scene.render();
}.bind(this));
window.addEventListener("resize", function () { // Watch for browser/canvas resize events
engine.resize();
});
var decalSize = new BABYLON.Vector3(0.1, 0.1, 0.1);
window.addEventListener("click", function(e) {
var bullet = BulletConstructor.create(scene);
})
}
Game.prototype.loadResources = function(e) {
// Wooden Crate
Materials.woodenCrate = new BABYLON.StandardMaterial("woodenCrate", scene);
Materials.woodenCrate.diffuseTexture = new BABYLON.Texture("./textures/woodenCrate.jpg", scene);
// Red Material
Materials.redMaterial = new BABYLON.StandardMaterial("redMaterial", scene);
Materials.redMaterial.diffuseColor = new BABYLON.Color3(255, 0, 0);
// Blue Material
Materials.blueMaterial = new BABYLON.StandardMaterial("blueMaterial", scene);
Materials.blueMaterial.diffuseColor = new BABYLON.Color3(0, 0, 255);
// Green Material
Materials.greenMaterial = new BABYLON.StandardMaterial("greenMaterial", scene);
Materials.greenMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
// Black Material
Materials.blackMaterial = new BABYLON.StandardMaterial("blackMaterial", scene);
Materials.blackMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
// Bullet Hole Material
Materials.bulletHoleMaterial = new BABYLON.StandardMaterial("bulletHoleMaterial", scene);
Materials.bulletHoleMaterial.diffuseTexture = new BABYLON.Texture("./textures/impact.png", scene);
Materials.bulletHoleMaterial.diffuseTexture.hasAlpha = true;
Materials.bulletHoleMaterial.zOffset = -2;
// Brick Material
Materials.brickMaterial = new BABYLON.StandardMaterial(name, scene);
var brickTexture = new BABYLON.BrickProceduralTexture(name + "text", 512, scene);
brickTexture.numberOfBricksHeight = 60;
brickTexture.numberOfBricksWidth = 100;
Materials.brickMaterial.diffuseTexture = brickTexture;
// Grass Material
Materials.grassMaterial = new BABYLON.StandardMaterial(name + "bawl", scene);
var grassTexture = new BABYLON.GrassProceduralTexture(name + "textbawl", 256, scene);
Materials.grassMaterial.ambientTexture = grassTexture;
e();
}
Game.prototype.buildMap = function() {
var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
// Create World Axis
// world axis visualization
var size = 10;
var worldOrigin = BABYLON.Vector3.Zero();
var xAxis = BABYLON.Mesh.CreateLines("x", [worldOrigin, (BABYLON.Axis.X).scale(size)], scene);
var yAxis = BABYLON.Mesh.CreateLines("y", [worldOrigin, (BABYLON.Axis.Y).scale(size)], scene);
var zAxis = BABYLON.Mesh.CreateLines("z", [worldOrigin, (BABYLON.Axis.Z).scale(size)], scene);
xAxis.color = BABYLON.Color3.Red();
yAxis.color = BABYLON.Color3.Green();
zAxis.color = BABYLON.Color3.Blue();
// Create the ground plane
var o = BABYLON.Mesh.CreatePlane("ground", 100, scene);
o.position.y = -1;
o.rotation.x = Math.PI / 2;
o.physicsImposter = new BABYLON.PhysicsImpostor(o, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, restitution: 0.9 }, scene);
o.checkCollisions = true;
o.material = Materials.grassMaterial;
// Create a big wall
var wall1 = BABYLON.MeshBuilder.CreateBox('box', {
height: 10,
width: 100,
depth: 1
}, scene);
wall1.position.z -= 50;
wall1.material = Materials.brickMaterial;
wall1.checkCollisions = true;
var wall2 = BABYLON.MeshBuilder.CreateBox('box', {
height: 10,
width: 100,
depth: 1
}, scene);
wall2.checkCollisions = true;
wall2.rotation.y = Math.PI / 2;
wall2.position.x -= 50;
wall2.material = Materials.brickMaterial;
var wall3 = BABYLON.MeshBuilder.CreateBox('box', {
height: 10,
width: 100,
depth: 1
}, scene);
wall3.checkCollisions = true;
wall3.rotation.y = Math.PI / 2;
wall3.position.x += 50;
wall3.material = Materials.brickMaterial;
var wall4 = BABYLON.MeshBuilder.CreateBox('box', {
height: 10,
width: 100,
depth: 1
}, scene);
wall4.checkCollisions = true;
wall4.position.z += 50;
wall4.material = Materials.brickMaterial;
// Create a box
for(var i = 0; i < 5; i++) {
var box = BoxManager.create();
box.position.x += (i * 30);
}
//Using a procedural texture to create the sky
var boxCloud = BABYLON.Mesh.CreateSphere("boxCloud", 100, 1000, scene);
boxCloud.position = new BABYLON.Vector3(0, 0, 12);
var cloudMaterial = new BABYLON.StandardMaterial("cloudMat", scene);
var cloudProcText = new BABYLON.CloudProceduralTexture("cloud", 1024, scene);
cloudMaterial.emissiveTexture = cloudProcText;
cloudMaterial.backFaceCulling = false;
cloudMaterial.emissiveTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
boxCloud.material = cloudMaterial;
}
var game = new Game;