-
Notifications
You must be signed in to change notification settings - Fork 2
/
engine.js
86 lines (71 loc) · 1.88 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
var Engine = (function(global) {
var doc = global.document,
win = global.window,
canvas = doc.createElement('canvas'),
ctx = canvas.getContext('2d'),
patterns = {},
lastTime;
canvas.width = 505;
canvas.height = 606;
doc.body.appendChild(canvas);
function main() {
var now = Date.now(),
dt = (now - lastTime) / 1000.0;
update(dt);
render();
lastTime = now;
win.requestAnimationFrame(main);
};
function init() {
reset();
lastTime = Date.now();
main();
}
function update(dt) {
updateEntities(dt);
// checkCollisions();
}
function updateEntities(dt) {
allEnemies.forEach(function(enemy) {
enemy.update(dt);
});
player.update();
}
function render() {
var rowImages = [
'images/water-block.png',
'images/stone-block.png',
'images/stone-block.png',
'images/stone-block.png',
'images/grass-block.png',
'images/grass-block.png'
],
numRows = 6,
numCols = 5,
row, col;
for (row = 0; row < numRows; row++) {
for (col = 0; col < numCols; col++) {
ctx.drawImage(Resources.get(rowImages[row]), col * 101, row * 83);
}
}
renderEntities();
}
function renderEntities() {
allEnemies.forEach(function(enemy) {
enemy.render();
});
player.render();
}
function reset() {
// noop
}
Resources.load([
'images/stone-block.png',
'images/water-block.png',
'images/grass-block.png',
'images/enemy-bug.png',
'images/char-boy.png'
]);
Resources.onReady(init);
global.ctx = ctx;
})(this);