-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.js
44 lines (36 loc) · 1.15 KB
/
Game.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
function Game() {
this.width = 512;
this.height = 448;
this.tileSize = 16;
this.renderer = new PIXI.autoDetectRenderer(this.width, this.height);
this.renderer.view.classList.add('vertical-center');
document.body.appendChild(this.renderer.view);
this.stage = new PIXI.Container();
this.level = new Level(this.stage, this, this.width, this.height,
this.tileSize);
this.controller = new Controller();
this.player = new Player(this.stage, this.level, this.tileSize);
this.lastRender = Date.now();
this.animate = this.animate.bind(this);
window.requestAnimationFrame(this.animate);
}
/**
* Render one frame
*/
Game.prototype.animate = function() {
window.requestAnimationFrame(this.animate);
var dt = Date.now() - this.lastRender;
var dir = this.controller.getDirection(0.1 * dt);
this.player.move(dir);
this.level.update(dt);
this.renderer.render(this.stage);
this.lastRender += dt;
};
/**
* Win the current level, moving on and increasing difficulty
*/
Game.prototype.win = function() {
this.level.map.difficulty += 5;
this.level.regenerate();
this.player.loc = this.level.getRandomEmptyLocation();
};