-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
266 lines (241 loc) · 7.97 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* Color codes */
const NORMAL_COLOR = "orange";
const LOW_HP_COLOR = "darkorange";
const CRIT_HP_COLOR = "darkred";
const POISONED_COLOR = "darkred";
const MALNUR_COLOR = "darkred";
const LOW_HP_UPPER_BOUND = 20;
const LOW_HP_LOWER_BOUND = 10;
/* Sets the player status stat to the given status. */
function setStatusStat(pStatus) {
document.getElementById("status-value").innerHTML = pStatus;
switch (pStatus) {
case PLAYER_STATUS.HEALTHY:
document.getElementById("status-value").style.color = NORMAL_COLOR;
break;
case PLAYER_STATUS.POISONED:
document.getElementById("status-value").style.color = POISONED_COLOR;
break;
case PLAYER_STATUS.MALNOURISHED:
document.getElementById("status-value").style.color = MALNUR_COLOR;
break;
}
}
/* Sets the player HP stat to the given value. */
function setHpStat(hp) {
document.getElementById("hp-value").innerHTML = hp;
if(hp >= LOW_HP_UPPER_BOUND) {
document.getElementById("hp-value").style.color = NORMAL_COLOR;
} else if(hp >= LOW_HP_LOWER_BOUND && hp < LOW_HP_UPPER_BOUND) {
document.getElementById("hp-value").style.color = LOW_HP_COLOR;
} else {
document.getElementById("hp-value").style.color = CRIT_HP_COLOR;
}
}
/* Sets the player XP stat to the given value. */
function setXpStat(xp) {
document.getElementById("xp-value").innerHTML = xp;
}
/* Sets the player Lvl stat to the given value. */
function setLvlStat(lvl) {
document.getElementById("lvl-value").innerHTML = lvl;
}
/* Sets the player Dmg stat to the given value. */
function setDmgStat(dmg) {
document.getElementById("dmg-value").innerHTML = dmg;
}
/* Sets the player Def stat to the given value. */
function setDefStat(def) {
document.getElementById("def-value").innerHTML = def;
}
/* Sets the player coordinates stat to the given coordinates. */
function setCoordsStat(mapX, xPos, mapY, yPos) {
document.getElementById("coords-value").innerHTML = "(" + mapX + "," + xPos + " ; " + mapY + "," + yPos + ")";
}
function setTurnStat(turn) {
document.getElementById("turn-value").innerHTML = turn;
}
/* Marks a ship part from the list as built. */
function markBuilt(partKey) {
document.getElementById(partKey.toLowerCase()+"-reqs").parentNode.style.textDecorationLine = "line-through";
}
/* Sets a given weapon in the player weapon slot. */
function setWeaponSlot(weapon) {
document.getElementById("weapon-slot").innerHTML = weapon;
}
/* Sets a given mask in the player mask slot. */
function setMaskSlot(mask) {
document.getElementById("mask-slot").innerHTML = mask;
}
/* Sets a given suit in the player suit slot. */
function setSuitSlot(suit) {
document.getElementById("suit-slot").innerHTML = suit;
}
/* Sets a given accessory in the player accessory slot. */
function setAccessorySlot(accessory) {
document.getElementById("accessory-slot").innerHTML = accessory;
}
/* Hides any open left windows */
function hideLeftWindow() {
var leftWindows = document.getElementsByClassName("left-window");
Array.from(leftWindows).forEach(function (window) {
window.style.display = "none";
});
}
/* Shows/hides the given window.
* name: string. The name of the window to toggle.
* player: Optional. The player object.
* force: Optional. If set opens the window even if it is already opened.
* Returns true if window was open when called, false otherwise.
*/
function toggleWindow(name, player, force) {
var window = document.getElementById(name);
if(name == "world-map-wrapper") {
if(window.style.visibility != "visible") {
window.style.visibility = "visible";
} else {
window.style.visibility = "hidden";
}
return true;
}
if(window.style.display != "block") {
if(name == "inventory") {
repopInv(player);
} else if (window.getAttribute("class") == "left-window") {
hideLeftWindow();
}
window.style.display = "block";
return false;
} else if (force === undefined) {
window.style.display = "none";
}
return true;
}
/* Repopulates the player inventory or store inventory*/
function repopInv(player, store=false) {
var invElement;
if (store)
invElement = document.getElementById("store");
else
invElement = document.getElementById("inventory");
// Removes old inventory list.
var invLists = document.getElementsByClassName("inv-list");
if(invLists.length > 0) {
invElement.removeChild(invLists[0]);
}
// Creates new inventory list.
var olElement = document.createElement("ol");
olElement.setAttribute("class", "inv-list");
if (store)
olElement.setAttribute("type", "a");
invElement.appendChild(olElement);
var inventory;
if (store)
inventory = storeInv;
else
inventory = player.getInventory();
var i;
for(i = 0; i < inventory.length; i++) {
var liElement = document.createElement("li");
var li = olElement.appendChild(liElement);
li.setAttribute("id", "inv-item-"+i);
let item = inventory[i];
if(item.isEquipped) {
li.style.color = "green";
}
if(!item.meetsReq(player)) {
li.style.color = "red";
}
if(item.lvl != 0) {
li.innerHTML = item.name + " (" + item.type + ", " + item.value + ", Lvl " + item.lvl + ")";
} else {
li.innerHTML = item.name + " (" + item.type + ", " + item.value + ")";
}
}
// Checks whether player equips a ranged weapon and if so updates the equipment window to update the projectile amount.
if(player.equipment.Weapon != null) {
if(player.equipment.Weapon.weaponType == "Ranged") {
updateEquipment(player.equipment.Weapon.name, player);
}
}
}
/* Takes an item name and sets its information in the relevant equipment slot */
function updateEquipment(name, player) {
let type = ITEMS[name].split(";")[1];
let value = ITEMS[name].split(";")[2];
switch (type) {
case "Weapon":
var equipmentSlot = document.getElementById("weapon-slot");
if(ITEMS[name].split(";")[WEAPON_TYPE_SLOT] == "Ranged") {
let projectile = ITEMS[name].split(";")[PROJECTILE_SLOT];
let projItem = player.getInvItem(projectile);
let projValue = 0;
if(projItem) {
projValue = projItem.value;
}
equipmentSlot.innerHTML = name + " (" + value + "), " + projectile + ": " + projValue;
return;
}
break;
case "Mask":
var equipmentSlot = document.getElementById("mask-slot");
break;
case "Suit":
var equipmentSlot = document.getElementById("suit-slot");
break;
case "Accessory":
var equipmentSlot = document.getElementById("accessory-slot");
break;
}
equipmentSlot.innerHTML = name + " (" + value + ")";
}
/* Takes a requirements list (e.g.: PARTS_REQS) and populates the suitable window */
function popBuildList(reqList) {
for(let [key, value] of Object.entries(reqList)) {
var elementId = key.toLowerCase() + "-reqs";
var metal = value.split(";")[0];
var wood = value.split(";")[1];
var gravel = value.split(";")[2];
var reqString = "";
if(metal != 0) {
reqString += "Metal: " + metal;
}
if(wood != 0) {
if(reqString != "") {
reqString += "; ";
}
reqString += "Wood: " + wood;
}
if(gravel != 0) {
if(reqString != "") {
reqString += "; ";
}
reqString += "Gravel: " + gravel;
}
document.getElementById(elementId).innerHTML = reqString;
}
}
/* Draws a progress bar underneath the log for the given period of time */
function showProgressBar(period) {
const NUM_OF_INTERVALS = 100;
var container = document.getElementById("cinematic-control-container");
var bar = document.getElementById("conversation-progress-bar");
container.style.display = "block";
bar.style.display = "block";
var width = 0;
var id = setInterval(frame, period / NUM_OF_INTERVALS);
function frame() {
if (width >= 100) {
clearInterval(id);
container.style.display = "none";
bar.style.display = "none";
} else {
width++;
bar.style.width = width + '%';
}
}
}
/* Prints messages that should be displayed on turn one */
function printTurnOneMsgs() {
printToLog(`\"Crash site\" area has been added to your world map.`);
}