-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
704 lines (623 loc) · 20.3 KB
/
util.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*** util.js
* File for utility functions.
***/
// Variable for keeping track of sleep function calls.
var sleepNum;
/******************* TILES *******************/
/* Sets a tile on top of the tile already in the cell. */
function setTileOnTop(cell, tile, walkable) {
var img = document.createElement("img");
var cellElement = document.getElementById(cell);
var lastChild = cellElement.lastElementChild;
if(lastChild.getAttribute("src").search("player") != -1) {
cellElement.insertBefore(img, lastChild);
} else {
cellElement.appendChild(img);
}
cellElement.style.position = "relative";
img.setAttribute("id", cell.replace('c', 'o'));
img.setAttribute("src", tile);
img.style.position = "absolute";
img.style.top = "0";
img.style.left = "0";
cellElement.setAttribute("walkable", walkable.toString());
setEnv(cell, tile);
}
/* 'Overloaded' function for setting a tile on top of a tile.
* Calls setTileOnTop() with any number of cells passed as arguments after 'tile' and 'walkable'. */
function setTilesOnTop(tile, walkable) {
for(i = 2; i < arguments.length; i++) {
setTileOnTop(arguments[i], tile, walkable);
}
}
/* Removes the tile that covers another tile and set the 'env' attribute to the value of the bottom tile.
* walkable: boolean. optional parameter to set the walkable attribute of the cell */
function removeTileOnTop(cell, walkable) {
var overTile = document.getElementById(cell).lastElementChild;
if(overTile) {
var cellElement = document.getElementById(cell);
cellElement.removeChild(overTile);
let bottomTile = cellElement.lastElementChild.getAttribute("src");
setEnv(cell, bottomTile);
if(walkable != undefined) {
if(walkable == true) {
document.getElementById(cell).setAttribute("walkable", "true");
} else {
document.getElementById(cell).setAttribute("walkable", "false");
}
}
if(document.getElementById(cell).lastElementChild != null) {
if(document.getElementById(cell).lastElementChild.getAttribute("src").search("player") != -1) {
removeTileOnTop(cell, walkable);
}
}
}
}
/* Hides the given tile in the given cell */
function hideTile(cell, tile) {
var cellImgElements = document.getElementsByTagName("img");
let i;
for(i = 0; i < cellImgElements.length; i++) {
if(cellImgElements[i].getAttribute("src").search(tile) != -1) {
cellImgElements[i].style.visibility = "hidden";
}
}
}
/******************* CELLS *******************/
/* Returns true if given position is in bounds, false otherwise */
function inBounds(cell) {
let xPos = parseInt(cell.slice(3));
let yPos = parseInt(cell.slice(1,3));
// Check for board bounds.
if(xPos < 0 || xPos > (WIDTH - 1)) {
return false;
}
if(yPos < 0 || yPos > (HEIGHT - 1)) {
return false;
}
return true;
}
/* Checks whether a cell is walkable. */
function isWalkable(cell) {
var cellElement = document.getElementById(cell);
if(cellElement.getAttribute("walkable") == "true") {
return true;
}
return false;
}
/* Checks if a cell is movable.
* Cell is movable only if it's within board bounds and its 'walkable' attribute is set to 'true' */
function isMovable(cell) {
return (inBounds(cell) && isWalkable(cell));
}
/* Sets a tile to a cell */
function setCell(cell, tile, walkable) {
var element = document.getElementById(cell);
var imgElement = element.getElementsByTagName("img").length
if(imgElement < 1) {
var img = document.createElement("img");
document.getElementById(cell).appendChild(img);
img.setAttribute("id", cell.replace('c','i'));
img.setAttribute("src", tile);
} else {
element.getElementsByTagName("img")[0].setAttribute("src", tile);
}
setEnv(cell, tile);
element.setAttribute("walkable", walkable.toString());
}
/* Sets the cell to an environment game object */
function setEnv(cell, tile) {
var cellElement = document.getElementById(cell);
var env = tile.slice(15).toLowerCase();
env = env.slice(0, -4);
cellElement.setAttribute("env", env);
}
/* Takes a cell and returns its env attribute, or null if it doesn't have it */
function getEnv(cell) {
var cellElement = document.getElementById(cell);
if(cellElement !== null && cellElement.hasAttribute("env")) {
return cellElement.getAttribute("env");
}
return null;
}
/* Returns a string representing the cell given it's position.
For consistency, the string cell will be refered to as "cell" in the rest of the program */
function getCell(row, col) {
var biDigCurX = getTwoDigits(col);
var biDigCurY = getTwoDigits(row);
return "c" + biDigCurY + biDigCurX;
}
/* Returns a pair of row and col from the given cell */
function getRowAndColFromCell(cell) {
var row = parseInt(cell.substring(1, 3));
var col = parseInt(cell.substring(3, 5));
return {
'row': row,
'col': col
};
}
/******************* LOG *******************/
/* Prompts the player to press a key */
function promptContinue(player) {
printToLog(CONTINUE_PROMPT);
document.body.onkeydown = function(event) {exitShip(player)};
}
/* Prints string to game log
* inline: Optional. If set, prints string in the same line as last string.
* Returns the starting index of the printed string in the log string.
*/
function printToLog(string, inline) {
var startIndex = log.length;
if(log === "" || inline) {
log += string;
} else {
log += "\n\n" + string;
}
var logElement = document.getElementById("log");
logElement.innerHTML = log;
// Automatic scroll log to bottom.
logElement.scrollTop = logElement.scrollHeight;
return startIndex;
}
/* Stops movement and prompts the user to input a direction for the action */
function promptDirection(action) {
movement = false;
actionExecuted = action;
printToLog("In what direction do you want to perform the action?");
}
/* Prints a message and prompts the user for input.
* confirm: Optional. If set, acts as a confirm prompt. Will only return Y/N.
*/
async function promptInput(message, confirm) {
if(confirm === undefined) {
printToLog(message + " ");
} else {
printToLog(message + " (Y/n): ");
}
var inputLength = 0; // Counts the number of characters the user has entered.
var inputString = ""; // Stores the inputted string.
var enterPressed = false; // Changes to true when user presses the enter key.
var escPressed = false; // Changes to true when user presses the esc key.
var confirmChoice; // True if Y/y, false if N/n.
var handler;
document.addEventListener("keydown", handler = function(event) {
event.stopPropagation();
if(confirm && isConfirmInput(event.key)) {
printToLog(event.key, true); // Echo key to log.
inputLength++;
inputString += event.key;
if(event.key == 'n' || event.key == 'N') {
confirmChoice = false;
} else {
confirmChoice = true;
}
enterPressed = true;
} else if (confirm === undefined) {
// If key pressed is a number, letter, or a valid character, echo it to the log.
if(isNumOrAlphabetKey(event.keyCode)) {
printToLog(event.key, true); // Echo key to log.
inputLength++;
inputString += event.key;
}
// keyCode 13 is enter.
if(event.keyCode == 13) {
enterPressed = true;
}
// keyCode 8 is backspace.
else if (event.keyCode == 8) {
if(inputLength > 0) {
// Remove one inputted character from the log.
inputString = inputString.slice(0, -1);
inputLength--;
if(log.charAt(log.length-1) != "|") {
log = log.slice(0, -1);
} else {
log = log.slice(0, -2);
}
printToLog("", true);
}
}
// keyCode 27 is escape.
else if (event.keyCode == 27) {
if(inputLength > 0) {
log = log.slice(0, -inputLength);
}
escPressed = true;
}
}
}, true);
// Function will not return until user presses enter or esc.
while(!enterPressed && !escPressed) {
await vbarFlash(500); // Flashes a '|' to indicate user input. The sleep is also needed because the loop hangs the browser.
}
document.removeEventListener("keydown", handler, true);
// Returns the inputted string or false if esc was pressed.
if(!escPressed && confirm === undefined) {
return inputString;
} else {
if (escPressed || !confirmChoice) {
return false;
} else {
return true;
}
}
}
/* Flashes a '|' character at the end of the log to indicate user input.
* time: specifies the frequency of flashing in ms.
*/
async function vbarFlash(time) {
printToLog("|", true);
await sleep(time);
log = log.replace("|", "");
printToLog("", true);
await sleep(time);
}
/* Takes a keyCode (ASCII value of a key pressed) and returns whether the key is a number or a letter. */
function isNumOrAlphabetKey(keyCode) {
if(keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0) ||
keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0) ||
keyCode >= 'a'.charCodeAt(0) && keyCode <= 'z'.charCodeAt(0) ||
keyCode == 188 || keyCode == 189 || keyCode >= 96 && keyCode <= 105)
{
return true;
}
return false;
}
/* Takes a key and returns whether the key is a valid confirmation input (Y/y/N/n). */
function isConfirmInput(key) {
if(key == 'y' || key == 'Y' || key == 'n' || key == 'N') {
return true;
}
return false;
}
/******************* SOUND *******************/
/* Function for adding sound files to the game.
* path: string. The path for the sound file.
*/
function sound(path) {
this.sound = document.createElement("audio");
this.sound.src = path;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.setAttribute("loop", "true");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
if(allowSounds) {
this.sound.play();
}
}
this.stop = function(){
this.sound.pause();
}
this.volume = function(vol) {
if(allowSounds) {
this.sound.volume = vol;
}
}
this.loop = function(shouldLoop) {
this.sound.loop = shouldLoop;
}
}
/* Creates and plays a new sound */
function createSound(soundPath, isLoop) {
let newSound = new sound(soundPath);
newSound.loop(isLoop);
newSound.play();
if(isLoop) {
activeSounds.push(newSound);
}
return newSound;
}
/* Mute a singular HTML5 element */
function toggleMuteSound(elem) {
if(elem.muted == false) {
elem.muted = true;
elem.pause();
} else {
elem.muted = false;
if(elem.loop) {
elem.play();
}
}
}
/* Mutes/unmutes all sounds */
function toggleSounds() {
if(allowSounds) {
allowSounds = false;
document.getElementById("speaker-emoji").innerHTML = "🔇";
} else {
allowSounds = true;
document.getElementById("speaker-emoji").innerHTML = "🔊";
}
document.querySelectorAll("audio").forEach( elem => toggleMuteSound(elem) );
document.getElementById("toggle-sounds").blur();
}
/* Plays fire sound in a volume according to player position */
function shouldFirePlay(player, fireXPos, fireYPos) {
let fire = document.getElementsByClassName("fire")[0];
// Distance between player and source of fire.
var distance = getDistanceBetweenTwoPoints(player.xPos, player.yPos, fireXPos, fireYPos);
// Volume to reduce.
var distVolOffset = distance/10;
// Reduce volume by distVolOffset+FIRE_DIST_OFFSET but stay within 0 and 1.
if(activeSounds[0] && activeSounds[0].sound.src.search("fire.mp3") != -1) {
activeSounds[0].volume(Math.max(Math.min(1, 1-distVolOffset+FIRE_DIST_OFFSET), 0));
}
}
/******************* OTHER *******************/
/* Math formula for calculating the distance between two points */
function getDistanceBetweenTwoPoints(firstXPos, firstYPos, secondXPos, secondYPos) {
return Math.sqrt(Math.pow((firstXPos - secondXPos), 2) + Math.pow((firstYPos - secondYPos), 2));
}
/* Returns a 2 digit number of a 1-2 digits number */
function getTwoDigits(n) {
return ("0" + n).slice(-2);
}
/* Returns a random position within the given dimensions */
function getRandomPosition(xMax=HEIGHT, yMax=WIDTH) {
xPos = Math.floor((Math.random() * xMax));
yPos = Math.floor((Math.random() * yMax));
return [
xPos,
yPos
];
}
/** Increments turn counter and prints it to the stat line, advances NPCs **/
function incrementTurnCounter(player) {
turn++;
setTurnStat(turn);
player.updateLevel();
npcs.forEach(function(npc) {
if(npc.status == "enemy") {
if(!npc.die()) {
npc.attack(player);
npc.move(player);
}
return;
}
});
if(turn == 1) {
printTurnOneMsgs();
}
managePlot(player);
saveGame(player);
}
/*
* Function for introducing delay into the game.
* Calling this function should freeze game execution.
* duration: number. The amount of time to sleep.
*/
function sleep(duration){
return new Promise(
resolve => sleepNum = setTimeout(resolve, duration)
);
}
// DEPRECATED
// Create NPCS.
// Place them at the remaining corners of the board.
function getNPCArray(){
return [
new NPC(0, HEIGHT-1),
new NPC(WIDTH-1, 0),
new NPC(WIDTH-1, HEIGHT-1),
new NPC(
parseInt((WIDTH/2).toFixed(0)),
parseInt((HEIGHT/2).toFixed(0))
)
];
}
/* To Infinity and beyond! */
function godmode(player) {
player.hp = Infinity;
player.hunger = Infinity;
let hpVal = document.getElementById("hp-value");
hpVal.innerHTML = "∞";
hpVal.style.fontSize = "1em";
}
/* Util function for creating a range of numbers in an array.
* The range is from start to end - inclusive.
*/
function range(start, end) {
return Array(end - start + 1).fill().map(
(_, idx) => start + idx
);
}
/* Uses the map's mapItems key to check if the map was visited before. */
function isInitialVisit(map) {
if(mapItems[map] === undefined) {
return true;
}
return false;
}
/* Retrieves a list of items for each game cell that contains item(s). */
function getGameItems(itemCells) {
let items = {};
for (let cell of itemCells) {
items[cell] = getItemsInCell(cell).map(
itemElement => itemElement.getAttribute("item")
);
}
return items;
}
/* Saves the game state to the localStorage object. */
function saveGame(player) {
// Save main game variables.
localStorage.setItem("turn", turn);
localStorage.setItem("log", log);
localStorage.setItem("plot", JSON.stringify(PLOT));
localStorage.setItem("movement", movement);
localStorage.setItem("currMap", currMap);
localStorage.setItem("npcs", JSON.stringify(npcs));
localStorage.setItem("containers", JSON.stringify(containers));
items = getGameItems(itemCells);
localStorage.setItem("items", JSON.stringify(items));
// Save current map items.
saveMapItems(player.mapX+","+player.mapY);
localStorage.setItem("mapItems", JSON.stringify(mapItems));
// Save current ship state.
localStorage.setItem("builtShipParts", [...builtShipParts].join(','));
// Save player properties.
localStorage.setItem("playerX", player.xPos);
localStorage.setItem("playerY", player.yPos);
localStorage.setItem("playerHp", player.hp);
localStorage.setItem("playerInv", JSON.stringify(player.inventory));
localStorage.setItem("playerDmg", player.dmg);
localStorage.setItem("playerXp", player.xp);
localStorage.setItem("playerLvl", player.lvl);
localStorage.setItem("playerMapX", player.mapX);
localStorage.setItem("playerMapY", player.mapY);
localStorage.setItem("playerHunger", player.hunger);
// Save big objects.
localStorage.setItem("bigObjects", JSON.stringify(bigObjects));
}
/* Loads the game state from the localStorage object.
* afterGameDraw: optional. used to load objects that require interaction with the DOM elements,
* as they are only created after the first time this function runs. */
function loadGame(afterGameDraw) {
if(localStorage.length != 0) {
if(afterGameDraw === undefined) {
// If player died, force a new game.
if(parseInt(localStorage.getItem("playerHp")) <= 0) {
newGame();
return;
}
// Load main game variables.
turn = parseInt(localStorage.getItem("turn"));
log = localStorage.getItem("log");
movement = JSON.parse(localStorage.getItem("movement"));
currMap = localStorage.getItem("currMap");
containers = JSON.parse(localStorage.getItem("containers"));
items = JSON.parse(localStorage.getItem("items"));
// Load plot tree.
var loadedPlotTree = JSON.parse(localStorage.getItem("plot"));
for(plotNode in PLOT) {
if(loadedPlotTree[plotNode].isCompleted == true) {
PLOT[plotNode].complete();
}
}
// Load fire sound if fire exists.
// Since Chrome throws Uncaught (in promise) DOMException for play
// on sounds when the window loads, this is a temporary bypass.
if(!PLOT.FIRE.isCompleted) {
let newSound = new sound(FIRE_SOUND);
newSound.loop(true);
activeSounds.push(newSound);
newSound.sound.setAttribute("autoplay", "true");
}
// Load player properties.
var player = new Player(localStorage.getItem("playerX"), localStorage.getItem("playerY"));
player.xPos = parseInt(localStorage.getItem("playerX"));
player.yPos = parseInt(localStorage.getItem("playerY"));
player.hp = parseInt(localStorage.getItem("playerHp"));
player.dmg = parseInt(localStorage.getItem("playerDmg"));
player.xp = parseInt(localStorage.getItem("playerXp"));
player.lvl = parseInt(localStorage.getItem("playerLvl"));
player.mapX = parseInt(localStorage.getItem("playerMapX"));
player.mapY = parseInt(localStorage.getItem("playerMapY"));
player.hunger = parseInt(localStorage.getItem("playerHunger"));
// Load current map items.
mapItems = JSON.parse(localStorage.getItem("mapItems"));
// Load big objects.
bigObjects = JSON.parse(localStorage.getItem("bigObjects"));
// Load inventory items.
var restoredInv = JSON.parse(localStorage.getItem("playerInv"));
for(i = 0; i < restoredInv.length; i++) {
var restoredVal = restoredInv[i].itemValue;
var restoredEquipStatus = restoredInv[i].equipped;
restoredInv[i] = new Item(restoredInv[i].itemName);
restoredInv[i].value = restoredVal;
restoredInv[i].isEquipped = restoredEquipStatus;
}
player.setInventory(restoredInv);
return player;
} else {
// Load NPCs
npcs = JSON.parse(localStorage.getItem("npcs"));
if(npcs.length == 0) {
npcs = [];
} else {
for(i in npcs) {
var hp = npcs[i].hp;
npcs[i] = new NPC(npcs[i].x, npcs[i].y, npcs[i].type, npcs[i].friendStatus);
npcs[i].health = hp;
npcs[i].currMap = currMap;
}
}
loadShipStateIfInitialMap();
}
}
}
function loadShipStateIfInitialMap() {
let mapX = parseInt(localStorage.getItem("playerMapX"));
let mapY = parseInt(localStorage.getItem("playerMapY"));
if (mapX === mapY && mapY === 0) {
// Load and restore ship state.
builtShipParts = new Set(localStorage.getItem("builtShipParts").split(','));
if (builtShipParts.has(""))
builtShipParts = new Set();
builtShipParts.forEach(
part => {
document.getElementById(part.toLowerCase()+"-reqs").parentNode.style.textDecorationLine = "line-through";
// Add part to ship visually.
repairShip(part);
}
);
}
}
/* Clears the local storage and forces a new game. */
function newGame() {
localStorage.clear();
location.reload();
}
/* Repairs the given ship part (changes the tiles). */
function repairShip(part) {
switch(part) {
case "HULL":
removeTileOnTop("c0505");
setTileOnTop("c0505", T_SHIP2F, false);
removeTileOnTop("c0605");
setTileOnTop("c0605", T_SHIP3F, false);
removeTileOnTop("c0705");
setTileOnTop("c0705", T_SHIP6F, false);
break;
default:
break;
}
}
/* Spawns the big objects that were built in the given map */
function loadMapBigObjects(map) {
if(bigObjects[map]) {
for(bigObject = 0; bigObject < bigObjects[map].length; bigObject++) {
let cell = bigObjects[map][bigObject][0];
let object = bigObjects[map][bigObject][1];
setTileOnTop(cell, eval("T_"+object.toUpperCase()), false);
if(object == "BRIDGE") {
document.getElementById(cell).setAttribute("walkable", "true");
}
}
}
}
/* Resume player movement and check for fire sound after game was loaded */
function resumePlayerMovementAndCheckFireOnLoad(player) {
// Initial check on load.
shouldFirePlay(player, 5, 7);
document.body.onkeydown = function(event) {
control(event, player);
// Check if the player is within a reasonable distance from the fire
// such that he can still hear it burning.
shouldFirePlay(player, 5, 7);
};
}
async function attemptToOpenCityGate(gateElement) {
printToLog("You read the message written on the gate:");
let code = parseInt(await promptInput("\"Welcome stranger. To identify, please enter the city access code:\""));
if (code === 8375) {
gateElement.setAttribute("walkable", "true");
printToLog("Welcome friend, we hope you'll enjoy our city.");
createSound(CONTAINER_OPEN, false);
}
else {
printToLog("We're sorry, the code you entered is invalid. Access denied.");
}
}