-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
57 lines (45 loc) · 1.33 KB
/
main.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
// -*- Mode: js2; js2-basic-offset: 2 -*-
//----------------------------------------------------------------------------
// Create Game Canvas and Loop
//----------------------------------------------------------------------------
function GameShell(initialState) {
this.canvas = document.createElement("canvas");
this.state = initialState;
// Init canvas
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
// Init time
this.lastFrame = (new Date()).getTime();
this.interval = setInterval(gameLoop, 20);
// Init state
this.state.dt = 0; // milliseconds.
this.state.canvas = this.canvas;
this.state.context = this.context;
this.state.start();
this.update = function() {
// Update time
let ms = (new Date()).getTime();
this.state.dt = ms - this.lastFrame;
this.lastFrame = ms;
this.state.update();
};
this.draw = function() {
this.clear();
this.state.draw();
};
this.clear = function() {
let w = this.canvas.width;
let h = this.canvas.height;
this.context.clearRect(0, 0, w, h);
};
};
var gGameShell = null;
function startGame() {
gGameShell = new GameShell(new Game());
};
function gameLoop() {
gGameShell.update();
gGameShell.draw();
}