-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.js
87 lines (71 loc) · 2.26 KB
/
UI.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
class UI {
constructor(options = {}) {
const {
game,
element,
AIPlayer = null
} = options;
this.game = game;
this.element = element;
this.AIPlayer = AIPlayer;
if(this.AIPlayer) {
this.log("Using internal input hooked to AI outputs.");
let date = null;
this.AIPlayer.on("computestart", e => {
date = Date.now();
this.log("Computing route...");
});
this.AIPlayer.on("computefinish", e => {
this.log(`Computing finished (${Date.now() - date}ms)`);
});
this.AIPlayer.on("fail", e => {
this.log(`AI failed: ${e.reason}`);
});
} else {
this.log("Using external input.");
this.log("Press arrow keys to move, space + arrow to shoot; or WASD alternatively.");
this.log("Press alphanumeric keys 1-3 for toggling the navigation.");
}
this.game.on("init", e => {
this.setupEventListeners();
});
}
setupEventListeners() {
const player = this.game.map.getPlayer();
player.on("inventorychange", e => {
this.updateInventory(player);
});
this.game.map.on("update", e => {
this.updateStats();
});
this.game.navigation.on("update", e => {
this.updateNavigation(e);
});
this.game.navigation.on("unreachable", e => {
this.log(`Navigation to ${e.type.name} failed: Destination is unreachable.`);
});
this.game.on("end", e => {
this.log(`Game ended: ${e.result}`);
//this.log(`Press Ctrl+R to restart.`);
});
this.updateInventory(player);
this.updateStats();
this.updateNavigation();
}
updateInventory(player = this.game.map.getPlayer()) {
const html = player.inventory.map(item => `<div class="item">
<span>${item.name}</span>
</div>`).join(", ");
JL(this.element, ".inventory").innerHTML = `Inventory: [ ${html} ]`;
}
updateStats() {
JL(this.element, ".monsters").innerText = `Monsters: ${this.game.map.getMonsters().length}`;
JL(this.element, ".arrows").innerText = `Arrows: ${this.game.map.getArrows().length}`;
}
updateNavigation(event = {}) {
JL(this.element, ".navigation").innerText = `Navigation: ${event.result && event.result.found ? `${event.type.name} (${event.result.path.length})` : "none"}`;
}
log(message) {
JL(this.element, ".log").innerHTML += message + "<br>";
}
}