forked from chinchang/js13k-2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
281 lines (252 loc) · 5.69 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
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
/**
* Game Core (WARNING: Handle with care)
*/
var wae = window.addEventListener;
var FPS = 60,
pi_by_180 = Math.PI / 180,
epsilon = 0.1,
c = null,
ctx = null,
bc = null,
bc_ctx = null,
objs = [],
keys = [],
last_time = 0,
debug = 1,
debug_color = '#ffffff',
stage = null,
supportsLocalStorage = false,
W, H,
shakeTime = 0,
shakeTimeMax = 0,
shakeIntensity = 7,
score = 0,
life = 2,
hs = 0,
PAUSE = true;
/**
* @class Any object which can be disaplyed on screen must inherit from DisplayObject
*/
var DisplayObject = function (){
}
var p = DisplayObject.prototype;
p.x = 0;
p.y = 0;
p.width = 0;
p.height = 0;
p.hitarea = null;
p.scale_x = 1;
p.scale_y = 1;
p.rotation = 0;
p.alpha = 1;
p.visible = true;
p.hitTestPoint = null;
p.draw = function(context) {};
p.update = function(dt) {};
/**
* @class A stage can't be instantiated and all properties and methods are static
*/
var Stage = function(){
};
/**
* Initializes variables, listeners etc
*/
function init(c_id) {
averagefps = {x: 0, y: 0};
W = window.innerWidth;
H = window.innerHeight;
c = document.getElementById(c_id);
c.width = W;
c.height = H;
ctx = c.getContext('2d');
bc = document.createElement('canvas')
bc.width = c.width;
bc.height = c.height;
bc_ctx = bc.getContext('2d');
objs = [];
stage = new Stage();
stage.update = function(dt){
if ('function' === typeof onStageUpdated)
onStageUpdated(dt);
}
addChild(stage);
// detect if the browser has localStorage support
try{
if(window['localStorage'] != null){
supportsLocalStorage = true;
if(!(hs = window.localStorage.getItem(game_config.score_key))){
hs = 0;
localStorage.setItem(game_config.score_key, 0)
}
}
} catch(e){
supportsLocalStorage = false;
}
// fps text
var fps_text = {
x: 5,
y: 15,
fps: 0,
visible: true,
update: function(dt){
this.fps = Math.round(1/dt);
if(this.fps !== Infinity){
averagefps.x = (averagefps.x * averagefps.y + this.fps) / ++averagefps.y;
}
},
draw: function(context){
if(!debug) return;
context.font = '12px Verdana';
context.fillStyle = '#F00';
context.fillText(this.fps + ' fps', 0, 0);
}
};
addChild(fps_text);
// Entities text
var entities_text = {
x: 50,
y: 15,
visible: true,
draw: function(context){
if(!debug) return;
context.font = '12px Verdana';
context.fillStyle = '#F00';
context.fillText(objs.length + ' Entities', 0, 0);
}
};
addChild(entities_text);
game.init();
wae('keypress', onKeyPress);
wae('keydown', onKeyDown);
wae('keyup', onKeyUp);
gameLoop();
}
function gameLoop(){
window.requestAnimFrame(gameLoop);
update();
draw();
}
function onKeyUp(e) {
keys[e.which] = false;
}
function onKeyDown(e) {
keys[e.which] = true;
}
function onKeyPress(e) {
if({68:1,100:1}[e.which]){
// debug ^= 1;
}
else if(PAUSE && {88:1,120:1}[e.which]){
}
}
/*
* Game's update function called from gameloop
* Updates all game entities
*/
function update() {
// get the time past the previous frame
var current_time = new Date().getTime();
if(!last_time) last_time = current_time;
var dt = (current_time - last_time) / 1000;
last_time = current_time;
if (shakeTime > 0) { shakeTime -= dt; }
for(var i = objs.length; i--;){
var obj = objs[i];
if(typeof obj.update == 'function'){
obj.update(dt);
}
}
}
/*
* Game's draw function called from gameloop
* Draws all game entities
*/
function draw() {
clearScreen(bc_ctx, '#9CC5C9');
// use double buffering technique to remove flickr :)
var context = bc_ctx;
if (shakeTime > 0) {
var magnitude = (shakeTime / shakeTimeMax) * shakeIntensity;
var shakeX = random(-magnitude, magnitude);
var shakeY = random(-magnitude, magnitude);
context.save();
context.translate(shakeX, shakeY);
}
for(var i = 0, l = objs.length; i < l; i++){
var obj = objs[i];
if(typeof obj.draw == 'function' && obj.visible){
context.save();
!isNaN(obj.x) && !isNaN(obj.y) && context.translate(obj.x, obj.y);
!isNaN(obj.scale_x) && !isNaN(obj.scale_y) && context.scale(obj.scale_x, obj.scale_y);
!isNaN(obj.rotation) && context.rotate(obj.rotation * pi_by_180);
!isNaN(obj.alpha) && (context.globalAlpha = obj.alpha);
obj.draw(context);
context.restore();
}
}
context.font = '18px Verdana';
context.fillStyle = '#FF0';
context.fillText('Score: ' + score, 20, H - 30);
context.fillText('Life: ' + life, 20, H - 10);
if (shakeTime > 0) {
context.restore();
}
clearScreen(ctx);
ctx.drawImage(bc, 0, 0);
}
function clearScreen(context, color){
context.clearRect(0, 0, c.width, c.height);
}
function addChild(c){
objs.push(c);
}
function removeChild(c) {
for(var i = objs.length; i--;)
if(objs[i] === c){
delete c;
objs.splice(i, 1);
break;
}
}
function removeChildAt(i) {
if(!objs[i]) return;
delete objs[i];
objs.splice(i, 1);
}
function setChildIndex(child, i) {
for(var j=-1, l=objs.length; ++j<l;){
if(objs[j] === child && j != i){
objs.splice(j, 1);
objs.splice(i, 0, child);
}
}
}
function shake(time) {
shakeTimeMax = shakeTime = time;
}
function random(min, max) {
if (!max) { max = min; min = 0; }
return min + ~~(Math.random() * (max - min + 1))
}
function resetScore(){
score > hs ? hs = score : null;
score = 0;
}
function saveScore(){
localStorage.setItem(game_config.score_key, hs);
}
function dist(x1, y1, x2, y2){
var dx = x1 - x2;
var dy = y1 - y2;
return Math.abs(Math.sqrt(dx * dx + dy * dy));
}