-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
341 lines (289 loc) · 10.4 KB
/
game.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
const UPDATES_PER_SECOND = 30;
const TERRAIN_SEED = 'asteroids yo';
const WORLD_SIZE = 1024; // not sure how big things are
const TERRAIN_SMOOTHING = 32;
const TERRAIN_EXP = 2; // mountains higher, valleys lower
const MAX_ASTEROIDS = 50;
const Engine = require("./engine.js");
const Pos = Engine.Pos;
const GameObject = Engine.GameObject;
const Player = Engine.Player;
const Laser = Engine.Laser;
const Asteroid = Engine.Asteroid;
const SimplexNoise = require('simplex-noise');
const simplex = new SimplexNoise(TERRAIN_SEED);
var updateData = {
initialized: {},
updated: {}, // visually updated
removed: [] // list of ids
};
var world = {
size: WORLD_SIZE,
objects: {},
static_objects: []
};
function sanitize(obj) {
return {
pos: {x: obj.pos.x, y: obj.pos.y},
type: obj.type,
_r: obj._r,
tag: obj.tag
}
}
var lastId = 0;
var asteroidCount = 0;
function noise(x, y) {
return simplex.noise2D(x, y) / 2 + 0.5;
}
function addObject(obj) {
// if the object is not a game object, force it to be one
if(!(obj instanceof GameObject))
obj = GameObject.from(obj);
world.objects[lastId] = obj;
updateData.initialized[lastId] = obj;
return lastId++;
}
function initWorld() {
for(let y = -WORLD_SIZE; y < WORLD_SIZE; y++) {
for(let x = -WORLD_SIZE; x < WORLD_SIZE; x++) {
let nx = x / TERRAIN_SMOOTHING - 0.5,
ny = y / TERRAIN_SMOOTHING - 0.5;
let val =
1 * noise(nx, ny) +
0.5 * noise(nx * 2, ny * 2) +
0.25 * noise(nx * 4, ny * 4);
val /= 1.75; // 1.75 == all weights summed
if(Math.pow(val, TERRAIN_EXP) > .85) {
let rand = 5+Math.floor((Math.random()*10) % 3);
world.static_objects.push(new GameObject(x, y, rand));
}
}
}
}
initWorld();
function emptyUpdateData() {
updateData.initialized = {};
updateData.updated = {};
updateData.removed = [];
}
function update(dt) {
for(let objId in world.objects) {
let obj = world.objects[objId];
obj.update(dt);
updateData.updated[objId] = obj;
}
cleanup();
checkCollision();
if(asteroidCount < MAX_ASTEROIDS){
asteroidSpawn();
asteroidCount++;
}
}
function getTimeMs() {
return +new Date();
}
function checkCollision() {
for(let objId in world.objects){
let obj1 = world.objects[objId];
if(obj1.type == 3 || (obj1.type == 2 && obj1.components[3].invincible)){
continue;
}
for(let objId2 in world.objects){
if(objId == objId2){
continue;
}
let obj2 = world.objects[objId2];
let h1 = obj1.components[1];
let h2 = obj2.components[1];
let dx = obj1.x - obj2.x,
dy = obj1.y - obj2.y;
if(Math.sqrt(dx * dx + dy * dy) < h1.radius + h2.radius){
collide(objId, objId2);
}
}
}
}
function collide(objId, objId2){
let obj1 = world.objects[objId];
let obj2 = world.objects[objId2];
if(obj2.type == 3){
//laser hit something
//push object1
//use bounce with laser data before deleting the laser
if(obj2.shooterId == objId){
return;
}else{
obj1.components[2].bounce(obj2.components[0].velocity, .1)
updateData.removed.push(objId2);
}
}
if(obj2.type == 4 && obj1.type == 2){
//player crash with asteroid
updateData.removed.push(objId);
}
if(obj1.type == 4 && obj2.type == 2){
//player crash with asteroid
updateData.removed.push(objId2);
}
if(obj1.type == obj2.type){
//bounce
let vel1 = new Pos(obj1.components[0].velocity.x, obj1.components[0].velocity.y),
vel2 = new Pos(world.objects[objId2].components[0].velocity.x, world.objects[objId2].components[0].velocity.y);
obj1.components[2].bounce(vel2, obj2.components[2].mass);
obj2.components[2].bounce(vel1, obj1.components[2].mass);
}
}
function cleanup(){
for(let objId in world.objects){
if(world.objects[objId].type == 2 || world.objects[objId].type == 4) {
if(world.objects[objId].x > WORLD_SIZE + 50 || world.objects[objId].x < -WORLD_SIZE - 50 || world.objects[objId].y > WORLD_SIZE + 50 || world.objects[objId].y < -WORLD_SIZE - 50){
if(world.objects[objId].type == 4){
asteroidCount--;
}
updateData.removed.push(objId);
}
}else if(world.objects[objId].type == 3 && !world.objects[objId].components[2].alive){
updateData.removed.push(objId);
}
}
}
function asteroidSpawn(){
if (Math.random() < .5){
let x = (WORLD_SIZE + 20) * (Math.random() < .5 ? -1 : 1);
let y = WORLD_SIZE * Math.random();
let vectorAreaX = (Math.random() - .5) * WORLD_SIZE / 2;
let vectorAreaY = (Math.random() - .5) * WORLD_SIZE / 2;
let rotation = Math.atan2(vectorAreaY-y, vectorAreaX-x);
addObject(new Asteroid(x, y, rotation));
} else {
let y = (WORLD_SIZE + 20) * (Math.random() < .5 ? -1 : 1);
let x = WORLD_SIZE * Math.random();
let vectorAreaX = (Math.random() - .5) * WORLD_SIZE / 2;
let vectorAreaY = (Math.random() - .5) * WORLD_SIZE / 2;
let rotation = Math.atan2(vectorAreaY-y, vectorAreaX-x);
addObject(new Asteroid(x, y, rotation));
}
}
exports.setup = function(io, info) {
let lastTime,
currentTime,
dt;
let game = io.of('/game').on('connection', socket => {
let userObj;
let objId;
let new_world = {
size: world.size,
static_objects: world.static_objects,
objects: { ...world.objects }
};
for(let id in new_world.objects) {
new_world.objects[id] = sanitize(new_world.objects[id]);
}
socket.emit('setup', new_world);
socket.on('key state', keys => {
if(typeof userObj === 'undefined')
return;
// server side state management
userObj.vertical = 0;
userObj.horizontal = 0;
let tb = 1;
if(userObj.turboCooldown > 0) {
userObj.turboCooldown -= dt;
if(userObj.turboCooldown <= 0){
userObj.turboCharge = 1.5;
}
}
if(keys.turbo && userObj.turboCharge > 0) {
tb = 2;
userObj.turboCharge -= dt;
if(userObj.turboCharge <= 0) {
userObj.turboCooldown = 10;
}
}
if(keys.forward) {
if(userObj.vertical >= 100){
userObj.vertical += 0;
}else{
userObj.vertical += 0.5 * tb;
}
}
if(keys.backward) {
userObj.vertical -= 0.5;
}
if(keys.left) {
userObj.horizontal -= 1;
}
if(keys.right) {
userObj.horizontal += 1;
}
if(keys.shoot && userObj.laserCooldown <= 0) {
// shoots from current x and y with rotation r
//console.log(`user ${userObj.tag} shot`);
world.objects[addObject(new Laser(userObj.x, userObj.y, userObj.r))].shooterId = objId;
//Id of Laser object is used to assign the shooter id with current obj that is shooting
userObj.laserCooldown = 1;
} else {
userObj.laserCooldown -= dt;
}
updateData.updated[objId] = userObj;
});
// Occurs when the user inputs a name on the splash
socket.on('ready', nickname => {
/* When the user inputs a nickname (splash)
let taken = false; // If that nickname is taken
for(let player in players) { // Cycle through all players
if(player.tag == nickname) { // If one of their tags is equal to the given nick
taken = true;
break; // Eh-Fish-In-See, yeah
}
}
if(!taken) { // Nickname is free
userObj.tag = nickname;
// ? addObject(userObj);
socket.emit('ready', userObj);
} else { // Nickname isn't free
}*/
if(typeof userObj !== 'undefined')
return;
userObj = new Player((Math.random() - .5) * WORLD_SIZE, (Math.random() - .5) * WORLD_SIZE, 0);
userObj.tag = info[socket.conn.id].nickname = nickname;
info[socket.conn.id].object = userObj;
objId = addObject(userObj);
info[socket.conn.id].objectId = objId;
socket.emit('ready', objId);
});
socket.on('disconnect', reason => {
console.log('user disconnected');
if(typeof objId !== 'undefined')
updateData.removed.push(objId);
});
});
lastTime = getTimeMs();
setInterval( () => {
currentTime = getTimeMs();
dt = (currentTime - lastTime) / 1000;
lastTime = currentTime;
update(dt);
// only send data that is needed,
// ie. split update into init, updated, and removed
if(Object.entries(updateData.initialized).length !== 0) {
for(let obj in updateData.initialized)
updateData.initialized[obj] = sanitize(updateData.initialized[obj]);
game.emit('objects initial', updateData.initialized);
}
if(Object.entries(updateData.updated).length !== 0) {
for(let obj in updateData.updated)
updateData.updated[obj] = sanitize(updateData.updated[obj]);
game.emit('objects updated', updateData.updated);
}
if(Object.entries(updateData.removed).length !== 0) {
for(let id of updateData.removed) {
//console.log(`actually deleted object ${id}`);
delete world.objects[id];
}
game.emit('objects removed', updateData.removed);
}
emptyUpdateData();
}, 1000 / UPDATES_PER_SECOND);
return game;
};