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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
let Game = {
display: null,
map: {},
engine: null,
player: null,
pedro: null,
ananas: null,
init: function () {
this.display = new ROT.Display({ spacing: 1.1 });
document.body.appendChild(this.display.getContainer());
this._generateMap();
let scheduler = new ROT.Scheduler.Simple();
scheduler.add(this.player, true);
scheduler.add(this.pedro, true);
this.engine = new ROT.Engine(scheduler);
this.engine.start();
},
_generateMap: function () {
let digger = new ROT.Map.Digger();
let freeCells = [];
let digCallback = function (x, y, value) {
if (value) {
return;
}
let key = x + "," + y;
this.map[key] = ".";
freeCells.push(key);
};
digger.create(digCallback.bind(this));
this._generateBoxes(freeCells);
this._drawWholeMap();
this.player = this._createBeing(Player, freeCells);
this.pedro = this._createBeing(Pedro, freeCells);
},
_createBeing: function (what, freeCells) {
let index = Math.floor(ROT.RNG.getUniform() * freeCells.length);
let key = freeCells.splice(index, 1)[0];
let parts = key.split(",");
let x = parseInt(parts[0]);
let y = parseInt(parts[1]);
return new what(x, y);
},
_generateBoxes: function (freeCells) {
for (let i = 0; i < 10; i++) {
let index = Math.floor(ROT.RNG.getUniform() * freeCells.length);
let key = freeCells.splice(index, 1)[0];
this.map[key] = "*";
if (!i) {
this.ananas = key;
} /* first box contains an ananas */
}
},
_drawWholeMap: function () {
for (let key in this.map) {
let parts = key.split(",");
let x = parseInt(parts[0]);
let y = parseInt(parts[1]);
this.display.draw(x, y, this.map[key]);
}
},
};
let Player = function (x, y) {
this._x = x;
this._y = y;
this._draw();
};
Player.prototype.getSpeed = function () {
return 100;
};
Player.prototype.getX = function () {
return this._x;
};
Player.prototype.getY = function () {
return this._y;
};
Player.prototype.act = function () {
Game.engine.lock();
window.addEventListener("keydown", this);
};
Player.prototype.handleEvent = function (e) {
let code = e.keyCode;
if (code == 13 || code == 32) {
this._checkBox();
return;
}
let keyMap = {};
keyMap[38] = 0;
keyMap[33] = 1;
keyMap[39] = 2;
keyMap[34] = 3;
keyMap[40] = 4;
keyMap[35] = 5;
keyMap[37] = 6;
keyMap[36] = 7;
/* one of numpad directions? */
if (!(code in keyMap)) {
return;
}
/* is there a free space? */
let dir = ROT.DIRS[8][keyMap[code]];
let newX = this._x + dir[0];
let newY = this._y + dir[1];
let newKey = newX + "," + newY;
if (!(newKey in Game.map)) {
return;
}
Game.display.draw(this._x, this._y, Game.map[this._x + "," + this._y]);
this._x = newX;
this._y = newY;
this._draw();
window.removeEventListener("keydown", this);
Game.engine.unlock();
};
Player.prototype._draw = function () {
Game.display.draw(this._x, this._y, "@", "#ff0");
};
Player.prototype._checkBox = function () {
let key = this._x + "," + this._y;
if (Game.map[key] != "*") {
alert("There is no box here!");
} else if (key == Game.ananas) {
alert("Hooray! You found an ananas and won this game.");
Game.engine.lock();
window.removeEventListener("keydown", this);
} else {
alert("This box is empty :-(");
}
};
let Pedro = function (x, y) {
this._x = x;
this._y = y;
this._draw();
};
Pedro.prototype.getSpeed = function () {
return 100;
};
Pedro.prototype.act = function () {
let x = Game.player.getX();
let y = Game.player.getY();
let passableCallback = function (x, y) {
return x + "," + y in Game.map;
};
let astar = new ROT.Path.AStar(x, y, passableCallback, { topology: 4 });
let path = [];
let pathCallback = function (x, y) {
path.push([x, y]);
};
astar.compute(this._x, this._y, pathCallback);
path.shift();
if (path.length == 1) {
Game.engine.lock();
alert("Game over - you were captured by Pedro!");
} else {
x = path[0][0];
y = path[0][1];
Game.display.draw(this._x, this._y, Game.map[this._x + "," + this._y]);
this._x = x;
this._y = y;
this._draw();
}
};
Pedro.prototype._draw = function () {
Game.display.draw(this._x, this._y, "P", "red");
};
Game.init();
|