/
game.js
112 lines (89 loc) · 2.73 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var Game = {
engine: new ROT.Engine(),
player: null,
level: null,
story: null,
storyFlags: {
wantsFlower: false
},
init: function() {
window.addEventListener("load", this);
},
handleEvent: function(e) {
switch (e.type) {
case "load":
window.removeEventListener("load", this);
this._load();
break;
case "resize":
this._resize();
break;
}
},
over: function() {
this.engine.lock();
var floor = Game.Cells.create("floor").setId("start");
var level = new Game.Level().setSize(3, 3);
level.setCell(floor, 1, 1);
level.setBeing(this.player, 1, 1);
Game.switchLevel(level, null, "fade");
this.story.newChapter("I am dead! What the hell? How could this ever happen? My mission is over and I will have to start again.");
this.story.setTask("Reload the page to start a new game.");
},
switchLevel: function(newLevel, cell, direction) {
var oldLevel = this.level;
this.level = null;
if (oldLevel) { this.engine.lock(); }
var position = newLevel.getCellById(cell || "start").getPosition();
if (oldLevel) { /* clear the old level */
oldLevel.removeBeing(this.player);
this.engine.clear();
}
this.level = newLevel;
/* welcome the new level */
newLevel.setBeing(this.player, position[0], position[1]);
for (var p in newLevel.beings) {
this.engine.addActor(newLevel.beings[p]);
}
this._resize();
this._transitionLevel(newLevel, oldLevel, direction);
this.engine.unlock();
},
_transitionLevel: function(newLevel, oldLevel, direction) {
var oppositeMap = {
north: "south",
south: "north",
east: "west",
west: "east",
fade: "fade"
};
var newNode = newLevel.getContainer();
if (direction) { newNode.className = direction; }
document.querySelector("#level").appendChild(newNode);
document.body.offsetWidth; /* FIXME hack */
if (oldLevel && oppositeMap[direction]) { oldLevel.getContainer().className = oppositeMap[direction]; }
newNode.className = "";
},
_load: function(e) {
this.story = new Game.Story();
this.status = new Game.Status(document.querySelector("#status"));
this.stats = new Game.Stats(document.querySelector("#stats"));
this.legend = new Game.Legend(document.querySelector("#legend"));
this.player = Game.Beings.create("player");
new Game.Intro().then(this._start.bind(this));
},
_start: function(e) {
window.addEventListener("resize", this);
Game.LevelManager.get("chapel").then(function(level) {
this.switchLevel(level);
}.bind(this));
},
_resize: function() {
if (!this.level) { return; }
var parent = document.querySelector("#level");
this.level.resize(parent.offsetWidth, parent.offsetHeight);
var position = this.player.getPosition();
Game.legend.update(position[0], position[1]);
}
}
Game.init();