-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
92 lines (85 loc) · 2.67 KB
/
game.js
File metadata and controls
92 lines (85 loc) · 2.67 KB
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
/*global ROT, TextBuffer, Level, Player, Being*/
/**
* @fileoverview The primary file of the game. Contains the Game object and its
* initialization, and calls all the other objects from their own files.
*/
/**
* The main class of the game. Holds the objects of its graphical display, its
* text area under it, its level data, its scheduler and engine. To start the
* game, its init function has to be called.
* @constructor
*/
var Game = function () {
"use strict";
this.display = null;
this.text = null;
this.level = null;
this.scheduler = null;
this.engine = null;
this.WIDTH = 45;
this.HEIGHT = 22;
this.TEXT_HEIGHT = 3;
};
/**
* Initializes the loading of the game. After the game window has loaded, the
* handleEvent function continues the initialization. Meanwhile everytime the
* window is resized, the display size will reset.
*/
Game.prototype.init = function () {
"use strict";
window.addEventListener("load", this);
window.addEventListener("resize", this);
};
/**
* Finishes the initialization of the game. Creates the display, sets its size
* to full window. Creates the text area, the scheduler, the engine, the level.
* Creates the player and the NPC-s, adds them to the scheduler, and finally
* starts the engine.
* @param {Event} e The load or resize event.
*/
Game.prototype.handleEvent = function (e) {
"use strict";
var i;
switch (e.type) {
case "load":
window.removeEventListener("load", this);
this.display = new ROT.Display({
width: this.WIDTH,
height: this.HEIGHT + this.TEXT_HEIGHT,
fontFamily: "Arial"
});
document.body.appendChild(this.display.getContainer());
this.setSize(this.display);
this.text = new TextBuffer(this.display, this.HEIGHT, this.TEXT_HEIGHT);
this.scheduler = new ROT.Scheduler.Speed();
this.engine = new ROT.Engine(this.scheduler);
this.level = new Level(this.WIDTH, this.HEIGHT);
this.level.setBeing(new Player("⬆⬈➡⬊⬇⬋⬅⬉", this.level, this.scheduler));
for (i = 0; i < 10; i += 1) {
this.level.setBeing(new Being("⬆⬈➡⬊⬇⬋⬅⬉", this.level, this.scheduler));
}
for (i in this.level.beings) {
if (this.level.beings.hasOwnProperty(i)) {
this.scheduler.add(this.level.beings[i], true);
}
}
this.engine.start();
break;
case "resize":
this.setSize(this.display);
break;
}
};
Game.prototype.setSize = function (display) {
"use strict";
display.setOptions({
fontSize: display.computeFontSize(window.innerWidth, window.innerHeight)
});
};
Game.prototype.over = function () {
"use strict";
this.engine.lock();
/* FIXME show something */
};
var GAME = new Game();
GAME.init();