-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
70 lines (51 loc) · 1.3 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
58
59
60
61
62
63
64
65
66
67
68
69
function main()
{
var totalIterations = 3000;
var animatePixels = 5;
var sceneWidth = 800;
var sceneHeight = 300;
var ended = false;
var updateLoopTime = 1000 / 15;
var scene = new Scene(sceneWidth, sceneHeight);
var bunni = new Bunni(200, 150);
var bunni2 = new Bunni(300, 150);
scene.addObject(bunni);
scene.addObject(bunni2);
var updateInterval = setInterval(update, updateLoopTime);
requestAnimationFrame(render);
function update() {
scene.prepareUpdate();
/* TODO All this code needs to be moved to the bunni.update() function */
var init = bunni.getInitState();
var next = bunni.getNextState();
/* FIXME Handle up + left cases move sprite faster */
if (keys[keyNames.UP]) {
next.y += -animatePixels;
}
if (keys[keyNames.DOWN]) {
next.y += animatePixels;
}
if (keys[keyNames.LEFT]) {
next.x += -animatePixels;
}
if (keys[keyNames.RIGHT]) {
next.x += animatePixels;
}
if (keys[keyNames.ESC]) {
ended = true;
}
scene.finishUpdate();
if (ended) {
clearInterval(updateInterval);
}
}
function render() {
var elapsed = getMillis() - Networkable.lastUpdated;
if (elapsed < updateLoopTime) { /* if you remove this, shit happens */
scene.drawAll(elapsed / updateLoopTime);
}
if (!ended) {
requestAnimationFrame(render);
}
}
}