-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathplayer.js
73 lines (62 loc) · 1.75 KB
/
player.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
class Player extends Being {
constructor() {
super({ch:"@", fg:"#fff"});
this._keys = {};
this._keys[ROT.KEYS.VK_K] = 0;
this._keys[ROT.KEYS.VK_UP] = 0;
this._keys[ROT.KEYS.VK_NUMPAD8] = 0;
this._keys[ROT.KEYS.VK_U] = 1;
this._keys[ROT.KEYS.VK_NUMPAD9] = 1;
this._keys[ROT.KEYS.VK_L] = 2;
this._keys[ROT.KEYS.VK_RIGHT] = 2;
this._keys[ROT.KEYS.VK_NUMPAD6] = 2;
this._keys[ROT.KEYS.VK_N] = 3;
this._keys[ROT.KEYS.VK_NUMPAD3] = 3;
this._keys[ROT.KEYS.VK_J] = 4;
this._keys[ROT.KEYS.VK_DOWN] = 4;
this._keys[ROT.KEYS.VK_NUMPAD2] = 4;
this._keys[ROT.KEYS.VK_B] = 5;
this._keys[ROT.KEYS.VK_NUMPAD1] = 5;
this._keys[ROT.KEYS.VK_H] = 6;
this._keys[ROT.KEYS.VK_LEFT] = 6;
this._keys[ROT.KEYS.VK_NUMPAD4] = 6;
this._keys[ROT.KEYS.VK_Y] = 7;
this._keys[ROT.KEYS.VK_NUMPAD7] = 7;
this._keys[ROT.KEYS.VK_PERIOD] = -1;
this._keys[ROT.KEYS.VK_CLEAR] = -1;
this._keys[ROT.KEYS.VK_NUMPAD5] = -1;
}
act() {
Game.textBuffer.write("It is your turn, press any relevant key.");
Game.textBuffer.flush();
Game.engine.lock();
window.addEventListener("keydown", this);
}
die() {
Being.prototype.die.call(this);
Game.over();
}
handleEvent(e) {
let code = e.keyCode;
let keyHandled = this._handleKey(e.keyCode);
if (keyHandled) {
window.removeEventListener("keydown", this);
Game.engine.unlock();
}
}
_handleKey(code) {
if (code in this._keys) {
Game.textBuffer.clear();
let direction = this._keys[code];
if (direction == -1) { /* noop */
/* FIXME show something? */
return true;
}
let dir = ROT.DIRS[8][direction];
let xy = this._xy.plus(new XY(dir[0], dir[1]));
this._level.setEntity(this, xy); /* FIXME collision detection */
return true;
}
return false; /* unknown key */
}
}