-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.js
522 lines (462 loc) · 13.3 KB
/
core.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/* exported engine */
/* exported b2Body, b2CircleShape */
/* global $, spawnable, sounds,Waves, playSoundInstance, soundManager, spawnableList, DEBUG, stats, log,Finance, missile, Camera, story, requestAnimFrame, UI, time,Layer,planet,unitTest,asteroid, initControls*/
"use strict";
// Box2D
var b2Vec2 = Box2D.Common.Math.b2Vec2;
var b2BodyDef = Box2D.Dynamics.b2BodyDef;
var b2Body = Box2D.Dynamics.b2Body;
var b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
var b2World = Box2D.Dynamics.b2World;
var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
var engine = {
frame: new Point(), // main canva size
size: new Point(2000,2000), //layer sizes
center: new Point(), // layer center position
BOUNDED:true, // should we limit the panning?
limit: new Point(), // kinda fake world size for BOUNDED limits
MAX_SCALE:2.5, // MAX_SCALE and MIN_SCALE are to be tweak once minimap is implemented
MIN_SCALE:0.4,
FRAME_RATE:1000/60, // in milisecond
camera: new Camera() , // position and zoom of the view ((0,0) is the center)
finance: new Finance(),
level:0,
waves: new Waves(),
timer:0,
areas: {
MIN_RADIUS:200,
MAX_RADIUS:1000,
FALL_OFF:500
},
earth:null,
updating:false, // if true, update
playing:false, // if true update but no spawn or finance count (in dialog)
objects: [], // all global objects ever created
entities: [], // list of all updatable and drawable entities
layers: [], // list of all layers
asteroids: [], // list of asteroids (to apply physics on them)
interactive:[], // list of interactive entities
killable:[], // list of player's killable entities
uiElements:[], // list of uiElements (to tick on window resize)
layerName: {}, // associative list of layer by name
canvas:null, // main canvas
context:null, // main context
$dom:null, // jQuery object for #main div containing the canvas
physics: { // physics data
world: new b2World(new b2Vec2(0,0), true), // no gravity
SCALE: 5/2000, // the gravity world is scaled so measures stay between 0 and 10
G: 0.00001, // gravity multiplier
MIN_VECT: -1, // MIN_VECT and MAX_VECT are limiters to avoid lightning fast asteroids
MAX_VECT: 1,
fixDef: new b2FixtureDef(), // box2D fixture definition helper
bodyDef: new b2BodyDef() // box2D body definition helper
},
story:null,
dialog:null,
init: function () {
if (DEBUG) {
document.body.appendChild( stats.domElement );
}
// we set the limit arbitrarily... might need to tweak that when working on minimap
this.$dom = $("#main");
this.limit = this.size.multiplyBy(5);
this.center = this.size.half();
// create the main canvas
this.canvas = document.getElementById('game');
this.context = this.canvas.getContext('2d');
// fit to window size
this.resize(window.innerWidth,window.innerHeight);
// init physic world
this.physics.G *= this.physics.SCALE;
var fixDef = this.physics.fixDef;
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
//*/
// unitTest
if(DEBUG) {
unitTest();
}
function waitForSound() {
var barSize = new Point(400,20);
if(soundManager.counter < Object.keys(sounds).length) {
var ctx = engine.context;
var center = engine.frame.half();
var ratio = soundManager.counter/Object.keys(sounds).length;
ctx.clearRect(0,0,engine.frame.x,engine.frame.y);
ctx.fillStyle = 'hsl(0,10%,30%)';
var x = center.x-barSize.x/2;
var y = center.y-barSize.y/2;
ctx.fillRect(
x,y,
barSize.x,barSize.y);
ctx.fillStyle = "red";
ctx.fillRect(
x,y,
barSize.x*ratio,barSize.y);
ctx.strokeStyle = "black";
ctx.strokeRect(
x,y,
barSize.x,barSize.y);
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = 'bold 18px Trebuchet ms';
ctx.fillStyle = 'hsl(0,100%,90%)';
ctx.fillText(Math.round(ratio*100)+"%",center.x,center.y);
setTimeout(waitForSound,engine.FRAME_RATE);
} else {
engine.startMenu();
initControls();
engine.updating = true;
engine.update();
}
}
waitForSound();
},
destroyAll: function() {
// recursively destroy layer and their content
while(this.layers.length>0) {
this.layers.pop().destroy();
}
// destroy any remaining object
while(this.objects.length>0) {
this.objects.pop().destroy();
}
},
startMenu: function() {
this.playing = false;
this.level = 0;
var $title = $('<div />')
.width(1024)
.height(800);
UI.uiElement({
hotspot:"center center",
anchor:"center center-100",
id:"title"
},$title);
UI.button({
text:'START',
hotspot: "center center",
anchor: "center center"
},function() {
engine.startLevel();
});
/*
UI.button({
hotspot: "center center",
text:'CREDITS',
anchor: "center center+100"
},function() {
log('credits');
}).$dom.button("option","disabled",true);
*/
var $credits = $('<p/>')
.html(
'Art and programming by <a href="http://yanngranjon.com">Yann Granjon</a><br/>'+
'Explosion animation from <a href="http://gritsgame.appspot.com/#/UI_MainMenu">grits</a><br/>'+
'Music by James Wilson, published on <a href="http://www.freeplaymusic.com/">freeplaymusic.com</a>'
);
UI.uiElement({
hotspot:"center bottom",
anchor:"center bottom-30",
id:"credits"
},$credits);
},
startLevel: function() {
// cleanup
this.destroyAll();
soundManager.stopAll();
playSoundInstance(sounds['music'],true);
this.story = story();
function startPlaying () {
if(engine.dialog === null) {
engine.playing = true;
return true;
} else {
return false;
}
}
function always () {return true;}
function pauseGame () {engine.playing = false; return true;}
function firstBlood() {
if (engine.earth.hp < engine.earth.hpMax) {
engine.playing = false;
return true;
} else {
return false;
}
}
this.story.add(1,pauseGame,[]);
this.story.add(1,always,
[{ character: 'obama',
text:"We are under attack!"},
{ character: 'obama',
text:"Asteroids are, as we speak,\nheading for our planet."},
{ character: 'obama',
text:"Our only hope is to deviate\nthem from earth\nusing our moon's gravity"},
{ character: 'obama',
text:"Yes... We can!"}]
);
this.story.add(1,startPlaying,[]);
this.story.add(1,firstBlood,
[{ character: 'obama',
text:"This Asteroid did a lot of damage!"},
{ character: 'obama',
text:"The financial rate takes a huge slow down each time we take damage"},
{ character: 'obama',
text:"Please, be carefull"},
{ character: 'obama',
text:"If you can!"}]
);
this.story.add(1,startPlaying,[]);
this.story.add(2,pauseGame,[]);
this.story.add(2,always,
[{ character: 'obama',
text:"Good! you managed to keep us safe!"},
{ character: 'obama',
text:"But it was only the first wave..."},
{ character: 'obama',
text:"More are coming."},
{ character: 'obama',
text:"Save the world!\nYes... You can!"}]
);
this.story.add(2,startPlaying,[]);
this.camera.x = 0;
this.camera.y = 0;
this.camera.scale = 1.0;
//starfield layers
new Layer ({dynamic:false,parallaxe:{x:0.0125,y:0.0125,scale:0.0125}}).genStarField(200,{intensity:30});
new Layer ({dynamic:false,parallaxe:{x:0.025,y:0.025,scale:0.025}}).genStarField(300,{intensity:60});
new Layer ({dynamic:false,parallaxe:{x:0.05,y:0.05,scale:0.05}}).genStarField(400,{intensity:100});
//entity layers
new Layer ({name:'main'});
this.earth = planet({
layerName:'main',
position: this.center
});
this.earth.addMoon({
layerName:'main',
distance:250,
angle:Math.random()*Math.PI*2
});
this.buildHUD();
this.nextLevel();
},
nextLevel: function() {
this.level += 1;
this.waves.next();
},
spawnAsteroid:(function() {
var lastSpawnTime = 0;
return function (frequency,reset) {
if (reset === true) {
lastSpawnTime = 0;
} else if (engine.timer - lastSpawnTime >= frequency &&
this.waves.destroyed + this.asteroids.length < this.waves.limit) {
lastSpawnTime = engine.timer;
asteroid({
layerName:'main'
});
}
};
})(),
update : function () {
if (DEBUG) {
stats.end();
stats.begin();
}
var self = this;
var dt;
// if the user resize his window, we resize
if(this.frame.x !== window.innerWidth ||
this.frame.y !== window.innerHeight) {
this.resize(window.innerWidth,window.innerHeight);
}
if (this.updating) {
dt = time.getDt();
this.timer += dt;
this.camera.update(dt);
// tick all entities (even story)
forEach(this.entities, function (entity) {
entity.update(dt);
});
if(this.playing) {
this.spawnAsteroid(this.waves.rate);
this.finance.update(dt);
// physics stuff
this.applyForces();
//*
forEach(this.asteroids, function (asteroid) {
// check if too close to earth
// and if earth's cool down is done
// fire missile
var earth = self.earth;
if(earth.position.distanceToSquared(asteroid.position) <= Math.square(self.areas.MIN_RADIUS) &&
earth.coolDown === 0) {
earth.coolDown = earth.fireRate;
missile({
layerName:"main",
position:toCartesian (
earth.radius,
earth.position.angleTo(asteroid.position),
earth.position)
}, asteroid);
}
forEach(self.killable,function(entity) {
if(asteroid.position.distanceToSquared(entity.position) <= Math.square(asteroid.radius+entity.radius)){
entity.hit(asteroid.strength);
asteroid.hit(entity);
}
});
});
if (this.waves.destroyed >= this.waves.limit) {
this.nextLevel();
}
}
this.draw(dt);
}
requestAnimFrame(function() {self.update();});
},
draw: function () {
var self = this;
var ctx = this.context;
ctx.clearRect(0,0,this.frame.x,this.frame.y);
//*
// paste each layer on to the engine's context
forEach(this.layers,function(layer) {
ctx.save();
var scale = Math.lerp(1.0,self.camera.scale,layer.parallaxe.scale);
var x = (self.center.x*scale + self.camera.view.x * layer.parallaxe.x) - self.frame.x/2;
var y = (self.center.y*scale + self.camera.view.y * layer.parallaxe.x) - self.frame.y/2;
ctx.translate(-x,-y);
ctx.scale (scale,scale);
if(layer.dynamic) {
forEach(layer.entities, function (entity) {
entity.draw(ctx);
});
} else {
// static layer (for background)
ctx.drawImage(layer.canvas,
0,0,self.size.x,self.size.y,
0,0,self.size.x,self.size.y);
}
ctx.restore();
});
//*/
},
applyForces: function() {
var phy = this.physics;
var self = this;
forEach(this.asteroids, function(asteroid) {
forEach(self.entities, function (entity) {
if(entity.mass !== undefined && entity.type !== 'asteroid' ) {
// p1 and p2 are positions in physic-space
var p1 = asteroid.body.m_xf.position;
var m1 = asteroid.mass;
var p2 = entity.position.multiplyBy(phy.SCALE);
var m2 = entity.mass;
// gravity formula
var force = phy.G*m1*m2/Math.square(sqDistance(p1,p2));
// breaking into vector components
var v = new b2Vec2 (
force * Math.cos(angle(p1,p2)),
force * Math.sin(angle(p1,p2))
);
asteroid.applyForce(v,p1);
}
});
asteroid.body.m_force.x = Math.clamp(asteroid.body.m_force.x,phy.MIN_VECT*phy.SCALE,phy.MAX_VECT*phy.SCALE);
asteroid.body.m_force.y = Math.clamp(asteroid.body.m_force.y,phy.MIN_VECT*phy.SCALE,phy.MAX_VECT*phy.SCALE);
});
phy.world.Step(this.FRAME_RATE/1000,8,3);
phy.world.ClearForces();
forEach(this.asteroids, function (asteroid) {
// convert back from physics to game world
asteroid.position.x = asteroid.body.m_xf.position.x/phy.SCALE;
asteroid.position.y = asteroid.body.m_xf.position.y/phy.SCALE;
asteroid.angle = asteroid.body.GetAngle();
});
},
resize: function(width,height) {
log('resized!');
this.frame.set(width,height);
this.$dom.width(this.frame.x);
this.$dom.height(this.frame.y);
this.canvas.width = this.frame.x;
this.canvas.height = this.frame.y;
forEach(this.uiElements,function(ui) {
ui.place();
});
},
lose: function() {
this.playing = false;
UI.button({
text:"Retry",
hotspot:"center center",
anchor:"center center"
},
function() {
engine.reset();
engine.startLevel();
});
},
buildHUD: function () {
new Layer ({
name:'HUD',
parallaxe:{x:0,y:0,scale:1.0}
});
UI.radar({
hotspot:"right bottom",
anchor:"right-20 bottom-20"
},this.earth);
UI.zoomSlider();
UI.finance();
this.waves.$dom = UI.wave();
UI.button({
hotspot:"left bottom",
anchor:"left bottom",
text:"mute",
checkbox:true,
id:"mute"
},function() {
soundManager.toggleMute();
});
UI.button({
hotspot:"left bottom",
anchor:"left bottom-40",
text:"pause",
checkbox:true,
id:"pause"
},function() {
time.update();
engine.updating = !engine.updating;
});
var menu = UI.menu({
hotspot:"left top",
anchor:"left top+120",
id:"build-menu"
});
forEach(spawnableList,function(type){
var obj = spawnable[type];
log(type,obj);
menu.add(
UI.shopButton(type)
);
}) ;
},
reset : function () {
this.finance.money = 0;
this.finance.growthRate = 10;
this.camera.x = 0;
this.camera.y = 0;
this.camera.scale = 1.0;
this.timer = 0;
this.level = 0;
this.story = null;
this.dialog = null;
this.spawnAsteroid(null,true);
}
};
window.onload = function() {
engine.init();
};