-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
361 lines (285 loc) · 11.7 KB
/
engine.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
// Physics constants.
var XV_ACCELERATION = 0.085;
var XV_TERMINAL = 0.6;
var XV_FRICTION = 0.05;
var YV_GRAVITY = 0.05;
var YV_TERMINAL = 100;
var XV_BULLET = 0.75;
// Jumping physics constants
var JUMP = 0.9;
var JUMP_MAX = 2;
var JUMP_COOLDOWN = 400;
var CLIP_THRESHOLD = 2;
// Particle constants
var XV_PARTICLE = 0.5;
var YV_PARTICLE = 1;
var PARTICLE_TIMEOUT = 5000;
// Player constants
var MAX_BULLETS = 2;
var BULLET_COOLDOWN = 200;
var INVINCIBILITY_TIME = 2000;
var SHIELD_TIME = 2000;
var MAP_TIME = 500;
// Platform constants.
PLATFORM_THICKNESS = 4;
// Animation limits.
var FPS_CAP = 100;
var FPS_INTERVAL = 1000 / FPS_CAP;
var F = 0;
var S = 0;
var units = [
"pole", "pound", "square meter", "second", "hour", "year", "decade", "mile", "stade", "acre", "dollar", "cookie", "frame",
"cubic millimeter", "quartic cubit", "object", "coffee", "error", "failure", "mistake", "exception", "warning", "point",
"gallon", "ounce", "gram", "kilograms", "decibal", "tonne", "ton", "furlong", "fortnight", "firkin"
];
var unit = " " + units[Math.floor(Math.random()*units.length)] + "s/" + units[Math.floor(Math.random()*units.length)];
window.onfocus = function() { F = 0; S = Date.now() ; }
// Input.
var keys = {};
var keymap = [
{left: 65, right: 68, up: 87, down: 83, shoot: 49, shield: 192},
{left: 37, right: 39, up: 38, down: 40, shoot: 220, shield: 221}
];
// Sprites and particles.
var spritesPaths = {zero: "images/zero.png", infinity: "images/infinity.png", ddx: "images/ddx.png", intsmall: "images/intsmall.png", intlarge: "images/intlarge.png"}
var spritesReady = {}
var sprites = {};
for (var key in spritesPaths) {
spritesReady[key] = false;
var image = new Image();
image.key = key;
image.onload = function() { spritesReady[this.key] = true; }
image.src = spritesPaths[key];
sprites[key] = image;
}
var particlePaths = ["images/particle/0.png", "images/particle/1.png", "images/particle/2.png", "images/particle/3.png", "images/particle/4.png", "images/particle/5.png", "images/particle/6.png", "images/particle/7.png", "images/particle/8.png", "images/particle/9.png"];
var particlesReady = [];
var particles = [];
for (var i = 0; i < particlePaths.length; i++) {
particlesReady[i] = false;
var image = new Image();
image.key = i;
image.onload = function() { particlesReady[this.key] = true; }
image.src = particlePaths[i];
particles[i] = image;
}
var ready = false;
// Animation
var w = window;
requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;
// Rectangular intersection.
function intersects(r1, r2) {
return !(r1[0]+r1[2] < r2[0] || r1[0] > r2[0]+r2[2] || r1[1]+r1[3] < r2[1] || r1[1] > r2[1]+r2[3]);
}
// The main game engine class.
function Engine(canvas) {
// Graphics.
this.canvas = canvas;
this.context = canvas.getContext("2d");
this.context.font = "20px Verdana";
// Set up timing.
this.time = Date.now();
// Input binding.
addEventListener("keydown", function(e) {
keys[e.keyCode] = true;
if ([37, 39, 38, 40].indexOf(e.keyCode) > -1) e.preventDefault();
}, false);
addEventListener("keyup", function(e) { delete keys[e.keyCode]; }, false);
// Game objects.
this.map = 1;
this.maps = [
{
platforms: [
new Platform((canvas.width - 400)/2, canvas.height * 13/20, 400, PLATFORM_THICKNESS),
new Platform((canvas.width - 650)/2, canvas.height * 9/20, 150, PLATFORM_THICKNESS),
new Platform((canvas.width + 350)/2, canvas.height * 9/20, 150, PLATFORM_THICKNESS),
new Platform((canvas.width - 100)/2, canvas.height * 17/20, 100, PLATFORM_THICKNESS)
],
spawns: {
zero: 100,
infinitus: canvas.width - 100
}
},
{
platforms: [
new Platform((canvas.width - 20)/2, canvas.height * 9/20, 20, PLATFORM_THICKNESS)
],
spawns: {
zero: canvas.width / 2,
infinitus: canvas.width / 2
}
},
{
platforms: [
new Platform(canvas.width * 1/5, canvas.height * 9/20, canvas.width * 3/5, PLATFORM_THICKNESS)
],
spawns: {
zero: canvas.width * 3/10,
infinitus: canvas.width * 7/10
}
},
{
platforms: [
new Platform(canvas.width * 2/5, canvas.height * 9/20, canvas.width * 1/5, PLATFORM_THICKNESS),
new Platform(canvas.width * 1/5, canvas.height * 13/20, canvas.width * 3/5, PLATFORM_THICKNESS),
new Platform(canvas.width * 2/5, canvas.height * 17/20, canvas.width * 1/5, PLATFORM_THICKNESS)
],
spawns: {
zero: canvas.width * 3/10,
infinitus: canvas.width * 7/10
}
}
];
this.mapTime = 0;
this.bullets = [];
this.players = {
zero: new Player("zero", sprites.zero, sprites.intlarge, particles, keymap[0], this),
infinitus: new Player("infinitus", sprites.infinity, sprites.intsmall, particles, keymap[1], this)
};
this.players.zero.x = 100;
this.players.zero.direction = 1;
this.players.infinitus.x = this.canvas.width - 100 - this.players.infinitus.image.width;
// Update the game.
this.update = function(delta) {
if (77 in keys) { this.setMap(this.map+1); }
// Update the players
for (var name in this.players) this.players[name].update(delta);
for (var i = 0; i < this.bullets.length; i++) {
var bullet = this.bullets[i];
bullet.update(delta);
// Check if a bullet has died.
if (bullet.x+bullet.image.width < 0 || bullet.x > canvas.width) {
this.dieBullet(i);
}
}
// Collision detection
for (var name in this.players) {
// Get the actual player.
var player = this.players[name];
// Generate boundary boxes.
var bbox = player.bbox();
// Platform collision.
player.grounded = false;
for (var i = 0; i < this.platforms.length; i++) {
// Access the individual platform.
var platform = this.platforms[i];
// Check if colliding with platform while FALLING.
if (player.yv > 0 && intersects(bbox, platform.bbox()) && !(i in player.collisions)) {
//console.log(platform.bbox());
player.y = platform.y - player.image.height;
player.yv = 0;
player.grounded = true;
player.jump = 0;
player.collisions[i] = true;
} else {
delete player.collisions[i];
}
}
for (var i = 0; i < this.bullets.length; i++) {
// Access the bullet.
var bullet = this.bullets[i];
// Intersection with bullet.
if (!player.invincible() && !player.shielded && intersects(bbox, bullet.bbox())) {
this.die(player);
this.dieBullet(i);
} else if (player.shielded && intersects(bbox, bullet.bbox())) {
this.dieBullet(i);
}
}
// Edge detection.
if (player.x < 0) player.x = 0;
else if (player.x+player.image.width > canvas.width) player.x = canvas.width - player.image.width;
if (player.y < 0) player.y = 0;
else if (player.y + player.image.height > canvas.height + 150) this.die(player);
}
}
// Draw the game to the canvas.
this.render = function() {
// Check if reasources are ready.
if (!ready) {
ready = true;
for (var key in spritesReady) ready &= spritesReady[key];
return;
}
// Redraw the background.
this.context.fillStyle = "#CCC";
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
// Draw the platforms.
this.context.fillStyle = "#000";
for (var i = 0; i < this.platforms.length; i++) {
this.platforms[i].render(this.context);
}
// Draw the players.
for (var name in this.players) {
this.players[name].render(this.context);
}
// Draw the bullets.
for (var i = 0; i < this.bullets.length; i++) {
this.bullets[i].render(this.context);
}
// Draw frames per second.
this.context.fillStyle = "#AAA";
this.context.textAlign = "left";
this.context.textBaseline = "top";
this.context.fillText(Math.round(F/(Date.now() - S) * 1000) + unit, 10, 10);
this.context.fillRect(10, this.canvas.height-40, 0.1*this.players.zero.shield, 3);
this.context.textBaseline = "bottom"
this.context.fillText("Captain Zero: " + this.players.zero.score, 10, this.canvas.height-10);
this.context.fillRect(this.canvas.width-10, this.canvas.height-40, -0.1*this.players.infinitus.shield, 3);
this.context.textAlign = "right";
this.context.fillText("Infinitus: " + this.players.infinitus.score, this.canvas.width-10, this.canvas.height-10);
this.context.textBaseline = "top";
this.context.fillText("(m) Map " + (this.map+1), this.canvas.width-10, 10);
}
// The main game loop.
this.main = function() {
// Record timing.
var now = Date.now();
var delta = now - this.time;
if (delta > FPS_INTERVAL) {
// Update and render.
this.update(delta);
this.render();
// Update timing.
this.time = now;
// Update frame count.
F++;
}
// Next frame
requestAnimationFrame(this.main.bind(this));
}
// Called when a player dies.
this.die = function(player) {
// Move the player and update score.
if (player.name == "zero") this.players.infinitus.score++;
else if (player.name == "infinitus") this.players.zero.score++;
player.die();
}
// Wait for resources before going to main.
this.start = function() {
S = Date.now();
this.setMap(0);
this.main();
}
// Kill a bullet.
this.dieBullet = function(index) {
this.bullets[index].player.bullet++;
this.bullets.splice(index, 1);
}
this.setMap = function(index) {
if (Date.now() - this.mapTime < MAP_TIME) return;
this.map = index % this.maps.length;
this.platforms = this.maps[this.map].platforms;
this.players.zero.respawn();
this.players.infinitus.respawn();
this.players.zero.x = this.maps[this.map].spawns.zero - this.players.zero.image.width / 2;
this.players.infinitus.x = this.maps[this.map].spawns.infinitus - this.players.infinitus.image.width / 2;
this.mapTime = Date.now();
}
}
// Start the game.
function start() {
var canvas = document.getElementById("canvas");
var e = new Engine(canvas);
e.start();
}