/
kaboom.game.js
416 lines (361 loc) · 13.7 KB
/
kaboom.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
* Created by JetBrains WebStorm.
* User: dylanbeattie
* Date: 04/06/2011
* Time: 15:07
* To change this template use File | Settings | File Templates.
*/
if (typeof require == "function") {
var KaboomPlayer = require("./kaboom.player").KaboomPlayer;
var Position = require("./kaboom.player").Position;
};
var KaboomGame = function (levelMap) {
this.level = new KaboomLevel(levelMap);
this.players = [];
this.DISTANCE = 5;
this.TILE_SIZE = 48;
this.COLLISION_RANGE = 1;
};
KaboomGame.prototype = {
copyStateFrom : function(gameState) {
this.DISTANCE = gameState.DISTANCE;
this.TILE_SIZE = gameState.TILE_SIZE;
this.level = this.level || new KaboomLevel();
this.level.copyStateFrom(gameState.level);
for (var i = 0; i < gameState.players.length; i++) {
var sourcePlayer = gameState.players[i];
if (sourcePlayer) {
var targetPlayer = this.findPlayer(gameState.players[i]) || this.addPlayer(new KaboomPlayer());
targetPlayer.copyStateFrom(gameState.players[i]);
}
}
},
/* Finds the player instance within THIS game representing the same player as the supplied player */
findPlayer : function(player) {
for (var i = 0; i < this.players.length; i++) {
if (this.players[i].name == player.name) return(this.players[i]);
}
},
playerChangedVelocity : function(player) {
/* TODO: find the GAME player matching the supplied player and
update their position and velocity with those from the
supplied player.
*/
},
addPlayer : function(player) {
this.players.push(player);
return(player);
},
removePlayer: function(player) {
/* TODO: Find and free the player slot used by the specified player */
/* TODO: remember to set the corresponding spawn point.player back to null */
},
createPlayer : function() {
var spawn = this.level.getFirstEmptySpawnPoint();
var that = this;
if (spawn == null) return(null);
var player = new KaboomPlayer(spawn.number, "Player " + spawn.number, that.tilesToPixels(spawn.position));
console.log(player);
this.players[spawn.number - 1] = player;
console.log(spawn.number, this.players);
spawn.player = player;
return player;
},
tilesToPixels : function(position) {
var pixelX = position.x * this.TILE_SIZE;
var pixelY = position.y * this.TILE_SIZE;
return new Position(pixelX, pixelY);
},
pixelsToTiles : function(position) {
var tileX = Math.floor(position.x / this.TILE_SIZE);
var tileY = Math.floor(position.y / this.TILE_SIZE);
return new Position(tileX, tileY);
},
update: function() {
var game = this;
var tryMove = function(pos, delta, vel) {
if (delta.y == 0 && delta.x == 0) return pos;
var newPos = pos.translate(delta);
var gridPos = {
topLeft: game.pixelsToTiles(newPos.topLeft),
topRight: game.pixelsToTiles(newPos.topRight),
bottomLeft: game.pixelsToTiles(newPos.bottomLeft),
bottomRight: game.pixelsToTiles(newPos.bottomRight)
};
game.level.forEachTile(function(tile) {
tile.candidate = false;
tile.isIntersecting = false;
});
if (vel.dx == 1) {
// moving right
var tile1 = game.level.rows[gridPos.topRight.y][gridPos.topRight.x];
var tile2 = game.level.rows[gridPos.bottomRight.y][gridPos.bottomRight.x];
if (tile1.solid && tile2.solid) {
// both tiles ahead are solid, don't move
tile1.isIntersecting = true;
tile2.isIntersecting = true;
return pos;
}
if (tile1.solid) {
// tile to the top right of us is solid but the one to the bottom right is not
// move down a bit
tile1.isIntersecting = true;
return pos.translate({ x: vel.dx, y: 1 });
}
if (tile2.solid) {
// tile to the bottom right is solid, top right is not
// move up a bit
return pos.translate({ x: vel.dx, y: -1 });
}
} else if (vel.dx == -1) {
// moving left
var tile1 = game.level.rows[gridPos.topLeft.y][gridPos.topLeft.x];
var tile2 = game.level.rows[gridPos.bottomLeft.y][gridPos.bottomLeft.x];
if (tile1.solid && tile2.solid) {
// both tiles ahead are solid, don't move
tile1.isIntersecting = true;
tile2.isIntersecting = true;
return pos;
}
if (tile1.solid) {
// tile to the top left of us is solid but the one to the bottom left is not
// move down a bit
tile1.isIntersecting = true;
return pos.translate({ x: vel.dx, y: 1 });
}
if (tile2.solid) {
// tile to the bottom left is solid, top left is not
// move up a bit
tile2.isIntersecting = true;
return pos.translate({ x: vel.dx, y: -1 });
}
}
if (vel.dy == 1) {
// moving down
var tile1 = game.level.rows[gridPos.bottomLeft.y][gridPos.bottomLeft.x];
var tile2 = game.level.rows[gridPos.bottomRight.y][gridPos.bottomRight.x];
if (tile1.solid && tile2.solid) {
// both tiles ahead are solid, don't move
tile1.isIntersecting = true;
tile2.isIntersecting = true;
return pos;
}
if (tile1.solid) {
// tile to the bottom left of us is solid but the one to the bottom right is not
// move right a bit
tile1.isIntersecting = true;
return pos.translate({ x: 1, y: vel.dy });
}
if (tile2.solid) {
// tile to the bottom left is solid, bottom right is not
// move left a bit
tile2.isIntersecting = true;
return pos.translate({ x: -1, y: vel.dy });
}
} else if (vel.dy == -1) {
// moving up
var tile1 = game.level.rows[gridPos.topLeft.y][gridPos.topLeft.x];
var tile2 = game.level.rows[gridPos.topRight.y][gridPos.topRight.x];
if (tile1.solid && tile2.solid) {
// both tiles ahead are solid, don't move
tile1.isIntersecting = true;
tile2.isIntersecting = true;
return pos;
}
if (tile1.solid) {
// tile to the top left of us is solid but the one to the top right is not
// move right a bit
tile1.isIntersecting = true;
return pos.translate({ x: 1, y: vel.dy });
}
if (tile2.solid) {
// tile to the top left is solid, top right is not
// move left a bit
tile2.isIntersecting = true;
return pos.translate({ x: -1, y: vel.dy });
}
}
return newPos;
};
this.players.forEach(function(p, idx) {
if (!p) return;
if (p.velocity.dx == 0 && p.velocity.dy == 0) return;
var bounds = p.getBounds(game);
bounds = tryMove(bounds, { x: game.DISTANCE * p.velocity.dx, y: 0 }, p.velocity);
bounds = tryMove(bounds, { x: 0, y: game.DISTANCE * p.velocity.dy }, p.velocity);
p.position = bounds.topLeft;
});
}
};
if (typeof exports == "object") {
exports.KaboomGame = KaboomGame;
}
TileType = {
Solid: 0,
Destroyable: 1,
Blank: 2
};
Direction = {
North: 0,
NorthEast: 1,
East: 2,
SouthEast: 3,
South: 4,
SouthWest: 5,
West: 6,
NorthWest: 7
}
function KaboomLevel(initialTileMap) {
/*this.rows = 13;
this.cols = 17;*/
this.rows = [];
this.spawns = [];
this.copyStateFrom = function(that) {
this.rows = that.rows.map(function(row) {
return row.map(function(cell) {
return new Tile(cell.solid, cell.tileType, cell.row, cell.col);
});
});
this.spawns = that.spawns;
}
this.forEachTile = function(callback) {
for (var ri = 0; ri < this.rows.length; ri++) {
for (var ti = 0; ti < this.rows[ri].length; ti++) {
callback(this.rows[ri][ti]);
}
}
},
this.parseLevel = function(tileMap) {
var r, c;
var entry;
var index;
var spawn, spawnNum = 0;
var solid, tileType;
var row = [];
for (var i = 0; i < tileMap.length; i++) {
var tileChar = tileMap[i];
switch (tileChar) {
case '\n':
this.rows.push(row);
row = [];
continue;
case "*":
tileType = TileType.Solid;
solid = true;
spawn = false;
break;
case "-":
tileType = TileType.Destroyable;
solid = true;
spawn = false
break;
case " ":
tileType = TileType.Blank;
solid = false;
spawn = false;
break;
case "1":
tileType = TileType.Blank;
solid = false;
spawn = true;
spawnNum = 1;
break;
case "2":
tileType = TileType.Blank;
solid = false;
spawn = true;
spawnNum = 2;
break;
case "3":
tileType = TileType.Blank;
solid = false;
spawn = true;
spawnNum = 3;
break;
case "4":
tileType = TileType.Blank;
solid = false;
spawn = true;
spawnNum = 4;
break;
default:
console.log(entry);
}
if (spawn) {
this.spawns.push(new Spawn(spawnNum, row.length, this.rows.length));
}
row.push(new Tile(solid, tileType, this.rows.length, row.length));
}
if (row.length) this.rows.push(row);
}
if (initialTileMap) {
this.parseLevel(initialTileMap);
}
this.getFirstEmptySpawnPoint = function() {
for (var i = 0; i < this.spawns.length; i++) {
var spawn = this.spawns[i];
if (spawn.player == null) return(spawn);
}
}
}
function Tile (solid, tileType, row, col) {
this.solid = solid;
this.tileType = tileType;
this.row = row;
this.col = col;
}
Tile.prototype = {
getBounds: function(game) {
return new Rectangle(this.col * game.TILE_SIZE, this.row * game.TILE_SIZE, game.TILE_SIZE, game.TILE_SIZE);
}
};
function Spawn(num, x, y) {
this.number = num;
this.position = new Position(x, y);
this.player = null;
};
function Rectangle(x, y, width, height) {
this.x = this.left = x;
this.y = this.top = y;
this.width = width;
this.height = height;
this.right = this.left + width;
this.bottom = this.top + height;
this.topLeft = { x: this.left, y: this.top };
this.topRight = { x: this.right, y: this.top};
this.bottomLeft = { x: this.left, y: this.bottom };
this.bottomRight = { x: this.right, y: this.bottom};
};
Rectangle.prototype = {
pointIntersects: function(point) {
return point.x >= this.left && point.x <= this.right &&
point.y >= this.top && point.y <= this.bottom;
},
intersects: function(that) {
return this.pointIntersects(that.topLeft) ||
this.pointIntersects(that.topRight) ||
this.pointIntersects(that.bottomLeft) ||
this.pointIntersects(that.bottomRight);
},
translate: function(point) {
return new Rectangle(this.x + point.x, this.y + point.y, this.width, this.height);
},
contract: function(amount) {
return new Rectangle(this.x + amount, this.y + amount, this.width - (amount * 2), this.height - (amount * 2));
},
// debug function to display a rect on the screen as a green div
drawToScreen: function() {
var div = window.DEBUG_RECTANGLE || null;
if (div == null) {
div = window.DEBUG_RECTANGLE = $('<div style="background: rgba(0, 255, 0, 0.5)" />');
$('#playerLayer').append(div);
}
div.css({
position: 'absolute',
top: this.top + 'px',
left: this.left + 'px',
width: this.width + 'px',
height: this.height + 'px'
});
}
};