-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.class.js
64 lines (53 loc) · 1.26 KB
/
Game.class.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
function Game(width, height, name) {
this.obj = {};
this.width = width;
this.height = height;
this.execute = false;
this.dt, this.ctx;
var Game = this;
var lastTime, canvas;
var outer;
this.pre = function() {
outer = window['game_' + name];
canvas = document.createElement("canvas");
Game.ctx = canvas.getContext("2d");
Game.ctx.x = 0;
Game.ctx.y = 0;
canvas.width = this.width;
canvas.height = this.height;
document.body.appendChild(canvas);
document.body.style.overflow = "hidden";
outer.pre(Game);
window.resources.onReady(Game.start);
lastTime = Date.now();
};
this.start = function() {
var now = Date.now();
Game.ctx.dt = (now - lastTime) / 1000.0;
if(Game.execute === false) {
outer.start(Game);
Game.execute = true;
}
update(Game.ctx.dt);
render(Game.obj);
lastTime = now;
window.requestAnimFrame(Game.start);
};
var update = function(dt) {
outer.update(Game);
};
var render = function(ob) {
for(var i in ob) {
var value = ob[i];
if(typeof value.draw == 'function') {
value.draw(Game);
} else {
render(value);
}
}
outer.render(Game);
};
this.class = function() {
return outer;
};
}