-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (70 loc) · 2.03 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
window.addEventListener("resize", sizeCanvas);
PIXI.SCALE_MODES.DEFAULT = PIXI.SCALE_MODES.NEAREST;
scene = new PIXI.Container();
WIDTH = 400;
HEIGHT = 240;
TILE = 16;
HALFTILE = 8;
GRAVITY = 3;
function sizeCanvas() {
document.body.style.width = window.innerWidth;
document.body.style.height = window.innerHeight;
var w = Math.floor(window.innerWidth / WIDTH);
var h = Math.floor(window.innerHeight / HEIGHT);
var ratio = Math.max(Math.min(w, h), 1);
if (typeof renderer == "undefined") {
renderer = new PIXI.WebGLRenderer(WIDTH * ratio, HEIGHT * ratio);
document.body.appendChild(renderer.view);
} else {
renderer.resize(WIDTH * ratio, HEIGHT * ratio);
}
renderer.view.style.marginLeft = (window.innerWidth - ratio*WIDTH) /2;
renderer.view.style.marginTop = (window.innerHeight - ratio*HEIGHT)/2;
renderer.view.style.border = '1px solid #111';
scene.scale.x = ratio;
scene.scale.y = ratio;
}
document.addEventListener("DOMContentLoaded", function(event) {
sizeCanvas();
Keyboard.initialize();
Camera.initialize();
Tiles.initialize();
//
// setup the map
//
bunny = new Bunny({position: [WIDTH/2, HEIGHT/2]});
for (var i = 0; i < WIDTH/16*2; i++) {
new Brick({tileposition: [i*16, HEIGHT - 16]});
}
new Brick({tileposition: [WIDTH/2, HEIGHT - 16*4]});
new Brick({tileposition: [0, HEIGHT - 16*2]});
new Brick({tileposition: [WIDTH-16*3, HEIGHT - 16*2]});
new Brick({tileposition: [WIDTH-16*3, HEIGHT - 16*7]});
for (var i2 = 0; i2 < (WIDTH/16/2 - 1); i2++) {
new Brick({tileposition: [i2*16, HEIGHT/3*2]});
}
new WalkingFoe({position: [16, 16]});
//
// main loop
//
var lastTime = (new Date()).getTime();
animate(lastTime);
function animate(time) {
var delta = (time - lastTime) / 1000.0;
lastTime = time;
requestAnimationFrame(animate);
TickManager.tick(delta);
Camera.tick(bunny);
renderer.render(scene);
}
});
//
//
//
Bunny = Entity.extend('bunny', [
Position,
Size,
PlayerInput,
Motion,
[Sprite, 'bunny']
]);