/
main.js
44 lines (35 loc) · 896 Bytes
/
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
window.onload = function() {
// initialize global objects
activeKey = null;
map = new Map("test");
player = new Player("player", 1, 1, DOWN);
map.addChara(player);
// start mainloop
setInterval('mainLoop()', 16);
}
function mainLoop() {
var ctx = document.getElementById('canvas').getContext('2d');
// initialize
ctx.clearRect(0, 0, WIDTH, HEIGHT);
// update
map.update();
// draw
offset = calcOffset(player);
map.draw(ctx, offset);
player.draw(ctx, offset);
}
// key management
document.onkeydown = function(e) {
activeKey = e.which;
e.preventDefault();
}
document.onkeyup = function(e) {
activeKey = null;
e.preventDefault();
}
// calculate player-map offset
function calcOffset(player) {
var offsetx = player.px - WIDTH / 2;
var offsety = player.py - HEIGHT / 2;
return [offsetx, offsety];
}