-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_utils.js
46 lines (41 loc) · 1.04 KB
/
game_utils.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
function require(o) {
if (o === null || o === undefined) {
throw "object must be defined";
}
}
function Ticker(tickFunc, ticksDoneCallback) {
require(tickFunc);
var lastTickAt = null;
this.tick = function(timestamp) {
require(timestamp);
if (!lastTickAt) {
lastTickAt = timestamp;
}
var delta = timestamp - lastTickAt;
lastTickAt = timestamp;
// If too much time has passed, start over to avoid queuing up too much
if (delta < 1000 && delta > 0) {
doTicks(delta, tickFunc);
}
ticksDoneCallback();
};
function doTicks(delta, tickFunc) {
var maxTick = 30;
while (delta > maxTick) {
tickFunc(maxTick);
delta = delta - maxTick;
}
tickFunc(delta);
}
}
function setupCanvasContext(canvasWidth, canvasHeight) {
if (canvasWidth < 1 || canvasHeight < 1) {
throw "Width and height must be more than 1";
}
var canvas = document.createElement("canvas");
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.border = "1px solid";
document.body.appendChild(canvas);
return canvas.getContext("2d");
}