-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
1681 lines (1531 loc) · 72.5 KB
/
app.js
File metadata and controls
1681 lines (1531 loc) · 72.5 KB
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let textElement;
let optionButtonsElement;
let textNodes;
let itemStates;
class Character {
constructor(name, health, attackPower) {
this.name = name;
this.health = health;
this.attackPower = attackPower;
this.inventory = [];
this.location = null;
}
takeDamage(damage) {
this.health -= damage;
if (this.health < 0) {
this.health = 0;
}
}
useHealingItem(amount) {
this.health += amount;
if (this.health > 200) {
this.health = 200;
}
}
}
//List of all Characters
const gideonTheKnight = new Character("Gideon", 200, 50)
const evilArchduke = new Character("Archduke", 200, 50)
const prisonGuard = new Character ("Guard", 75, 20)
const wolves = new Character ("Wolves", 200, 30)
const hurtWolves = new Character("Hurt Wolves", 170, 30)
const characters = [
gideonTheKnight,
evilArchduke,
prisonGuard,
wolves
]
class Item {
constructor(name, type, value) {
this.name = name;
this.type = type; // e.g., "weapon", "healing", "quest"
this.value = value;
}
}
//List of all items
const guardSword = new Item("Chipped Sword", "weapon", 15);
const gideonSword = new Item("Gideon's Sword", "weapon", 45);
const guardArmor = new Item("Leather Armor", "Armor", 15);
const gideonArmor = new Item("Gold Armor", "Armor", 50);
const healthPotion = new Item ("Vial of Health", "healing", 25);
let breadRoll = new Item("Bread Roll", "healing", 10);
const halfBreadRoll = new Item("Half Bread Roll", "healing", 5);
const lockPick = new Item("Lock Pick", "Key Item", 0);
const smallRock = new Item("Small Rock", "Key Item", 0);
const items = [
guardSword,
gideonSword,
guardArmor,
gideonArmor,
healthPotion,
breadRoll,
halfBreadRoll,
lockPick,
smallRock
];
//All states of Item that change when the user picks them up/interacts with them
let state = {
lockPick: false,
smallRock: false,
smallRockDropped: false,
healthPotion: false,
breadRoll: false,
halfBreadRoll: false,
guardDead: false,
gideonDead: false,
wolvesDead: false,
archdukeDead: false,
lockPickKept: false,
triedGate: false,
triedClimbing: false,
lockPickBroken: false,
gideonSword: false,
gideonArmor: false,
guardArmor: false,
guardSword: false,
};
class Location {
constructor(name, description, imageSrc) {
this.name = name;
this.description = description;
this.imageSrc = imageSrc;
this.characters = [];
this.items = [];
this.connectedLocations = [];
}
move(newLocation) {
if (this.connectedLocations.includes(newLocation)) {
currentLocation = newLocation;
}
}
}
// List of all locations and their discriptions that get fed into the HTML page
const castle = new Location("Castle", "The Dungeons: A dark, gloomy maze of paths.", "/images/Dark Dungeon.jpg");
const courtyard = new Location("Castle Courtyard", "Courtyard: An empty enclosed green space.", "/images/Castle Courtyard.jpg");
const forest = new Location("Forest", "Forest: An eerily quiet and forboding forest of large trees.", "/images/Dark Forest.jpg");
const meadow = new Location("Meadow", "Meadow: The meadow is fragrant and brightly lit", "/images/Meadow.jpg");
const plateau = new Location("Plateau", "Plateau: Bright green grass spreads as far as the eye can see", "/images/Grassy Plateau.jpg");
const locations = [castle, courtyard, forest, meadow, plateau];
castle.connectedLocations.push(courtyard);
courtyard.connectedLocations.push(castle);
courtyard.connectedLocations.push(forest);
courtyard.connectedLocations.push(meadow);
meadow.connectedLocations.push(forest);
forest.connectedLocations.push(plateau);
let currentLocation = castle;
// How the images and descriptions get inserted into the HTML page
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
let option;
//Event Listener for when the page is loaded, all below will immediately load
document.addEventListener("DOMContentLoaded", function () {
const log1 = document.getElementById("log1");
const log2 = document.getElementById("log2");
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
const attackPowerElement = document.getElementById("attackPower");
attackPowerElement.textContent = `Attack Power: ${mainCharacter.attackPower}`;
log1.innerHTML = "";
log2.innerHTML = "";
textElement = document.getElementById("storyText");
optionButtonsElement = document.getElementById("option-buttons");
startGame();
});
let player1;
let player2;
// Alerts that ask for users name and title and stores them
const mainCharacterName = prompt("Please Enter your name:");
if (mainCharacterName !== null && mainCharacterName.trim() !== ""){
mainCharacter = new Character(mainCharacterName, 75, 20);
localStorage.setItem("mainCharacter", mainCharacterName);
}
else {
alert("You don't have a name? Even a ghost has a name. . . \nIf you do, please refresh this page and enter one.")
}
const mainCharacterTitle = prompt("Are you a Queen or a King? \nPress cancel if you'd prefer to be neither.");
if (mainCharacterTitle === "Queen" || "King") {
alert("Welcome, " + mainCharacterTitle + " " + mainCharacter.name + ". You're the ruler of these lands and have been captured by the evil Archduke! \nPlease select 'OK' and continue the story. \nGood luck.")
}
else if (mainCharacterTitle == null && mainCharacterTitle.trim() == ""){
alert("Welcome, " + mainCharacter.name + ". You're the ruler of these lands and have been captured by the evil Archduke! \nPlease select 'OK' and continue the story. \nGood luck.")
localStorage.setItem("mainCharacter", mainCharacter.name);
}
else {
alert("Welcome, " + mainCharacter.name + ". You're the ruler of these lands and have been captured by the evil Archduke! \nPlease select 'OK' and continue the story. \nGood luck.")
}
//How the maincharacter name gets loaded to the HTML page
const mainCharacterNameElement = document.getElementById("mainCharacterName");
mainCharacterNameElement.textContent = `${mainCharacter.name}'s Stats:`;
function startGame(){
const storedCharacterName = localStorage.getItem("mainCharacter");
if (storedCharacterName) {
mainCharacter.name = storedCharacterName;
}
mainCharacter.health = 75;
mainCharacter.attackPower = 20;
prisonGuard.health = 75;
gideonTheKnight.health = 200;
wolves.health = 200;
hurtWolves.health = 170;
evilArchduke.health = 200;
let currentLocation = castle;
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
const attackPowerElement = document.getElementById("attackPower");
attackPowerElement.textContent = `Attack Power: ${mainCharacter.attackPower}`;
window.onload = showTextNode(0.5);
state = {
lockPick: false,
smallRock: false,
smallRockDropped: false,
healthPotion: false,
breadRoll: false,
halfBreadRoll: false,
guardDead: false,
gideonDead: false,
wolvesDead: false,
archdukeDead: false,
lockPickKept: false,
triedGate: false,
triedClimbing: false,
lockPickBroken: false,
gideonSword: false,
gideonArmor: false,
guardArmor: false,
guardSword: false,
};
viewInventory();
showTextNode(0.5);
}
// Below code is how inventory is generated into the HTML element based on the states of the item. There is also a toggle visibility attached to an HTML button
function viewInventory() {
const inventoryElement = document.getElementById("currentInventory");
inventoryElement.style.display = (inventoryElement.style.display === "none") ? "block" : "none";
if (inventoryElement.style.display === "block") {
inventoryElement.innerHTML = "<ul>";
for (let itemName in state) {
if (state[itemName] === true && itemName === "lockPick") {
const listItem = document.createElement("li");
listItem.textContent = "Lock Pick";
inventoryElement.appendChild(listItem);
}
if (state[itemName] === true && itemName === "smallRock") {
const listItem = document.createElement("li");
listItem.textContent = "Small Rock";
inventoryElement.appendChild(listItem);
}
if (state[itemName] === true && itemName === "healthPotion") {
const listItem = document.createElement("li");
listItem.textContent = "Used Health Potion";
inventoryElement.appendChild(listItem);
}
if (state[itemName] === true && itemName === "guardArmor") {
const listItem = document.createElement("li");
listItem.textContent = "Leather Armor";
inventoryElement.appendChild(listItem);
}
if (state[itemName] === true && itemName === "gideonArmor") {
const listItem = document.createElement("li");
listItem.textContent = "Gold Armor";
inventoryElement.appendChild(listItem);
}
if (state[itemName] === true && itemName === "gideonSword") {
const listItem = document.createElement("li");
listItem.textContent = "Gideon's Sword";
inventoryElement.appendChild(listItem);
}
if (state[itemName] === true && itemName === "guardSword") {
const listItem = document.createElement("li");
listItem.textContent = "Dulled Sword";
inventoryElement.appendChild(listItem);
}
if (state[itemName] === true && itemName === "breadRoll") {
const listItem = document.createElement("li");
listItem.textContent = "Bread crumbs";
inventoryElement.appendChild(listItem);
}
}
inventoryElement.innerHTML += "</ul>"; // Close the unordered list
}
}
// Wait for the document to be ready
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("toggleInventoryBtn").addEventListener("click", viewInventory);
});
//Function for random dice rolls which is used in the below battle function
function getRandomDiceRoll() {
return Math.floor(Math.random() * 10) + 1;
}
//This rotates who goes first in the turn by random dice roll
function determineFirstPlayer(player1, player2) {
return (getRandomDiceRoll() <= 5 ? player1 : player2);
}
//Code for battle function that calculates damage, remaining health, and who wins and pushes the text along based on the outcome. All is displayed into an HTML element
function battle(player1, player2, logElement1, logElement2, option) {
// Determine the first player
const firstPlayer = determineFirstPlayer(player1, player2);
const secondPlayer = (firstPlayer === player1 ? player2 : player1);
let battleResult;
while (player1.health > 0 && player2.health > 0){
let currentAttacker = firstPlayer;
let currentDefender = secondPlayer;
const roll1 = getRandomDiceRoll();
const roll2 = getRandomDiceRoll();
let damage1 = 0;
let damage2 = 0;
console.log(player1);
console.log(player2);
if (currentAttacker === player1 && roll1 <= 2) {
logElement1.innerHTML += `<p>${player1.name} missed the attack on ${player2.name}.</p>`;
} else if (currentAttacker === player1 && roll1 >= 3 && roll1 <= 9) {
damage1 = Math.round(roll1 * 0.1 * player1.attackPower);
player2.takeDamage(damage1);
logElement1.innerHTML += `<p>${player1.name} dealt ${damage1} damage to ${player2.name}. ${player2.name}'s current health: ${player2.health}</p>`;
} else if (currentAttacker === player1) {
damage1 = player1.attackPower * 2;
player2.takeDamage(damage1);
logElement1.innerHTML += `<p>${player1.name} scored a critical hit, dealing ${damage1} damage to ${player2.name}. ${player2.name}'s current health: ${player2.health}</p>`;
}
if (currentDefender === player1 && roll2 <= 2) {
logElement1.innerHTML += `<p>${player1.name} missed the attack on ${player2.name}.</p>`;
} else if (currentDefender === player1 && roll2 >= 3 && roll2 <= 9) {
damage2 = Math.round(roll2 * 0.1 * player1.attackPower);
player2.takeDamage(damage2);
logElement1.innerHTML += `<p>${player1.name} dealt ${damage2} damage to ${player2.name}. ${player2.name}'s current health: ${player2.health}</p>`;
} else if (currentDefender === player1) {
damage2 = player1.attackPower * 2;
player2.takeDamage(damage2);
logElement1.innerHTML += `<p>${player1.name} scored a critical hit, dealing ${damage2} damage to ${player2.name}. ${player2.name}'s current health: ${player2.health}</p>`;
}
if (currentAttacker === player2 && roll1 <= 2) {
logElement2.innerHTML += `<p>${player2.name} missed the attack on ${player1.name}.</p>`;
} else if (currentAttacker === player2 && roll1 >= 3 && roll1 <= 9) {
damage1 = Math.round(roll1 * 0.1 * player2.attackPower);
player1.takeDamage(damage1);
logElement2.innerHTML += `<p>${player2.name} dealt ${damage1} damage to ${player1.name}. ${player1.name}'s current health: ${player1.health}</p>`;
} else if (currentAttacker === player2) {
damage1 = player2.attackPower * 2;
player1.takeDamage(damage1);
logElement2.innerHTML += `<p>${player2.name} scored a critical hit, dealing ${damage1} damage to ${player1.name}. ${player1.name}'s current health: ${player1.health}</p>`;
}
if (currentDefender === player2 && roll2 <= 2) {
logElement2.innerHTML += `<p>${player2.name} missed the attack on ${player1.name}.</p>`;
} else if (currentDefender === player2 && roll2 >= 3 && roll2 <= 9) {
damage2 = Math.round(roll2 * 0.1 * player2.attackPower);
player1.takeDamage(damage2);
logElement2.innerHTML += `<p>${player2.name} dealt ${damage2} damage to ${player1.name}. ${player1.name}'s current health: ${player1.health}</p>`;
} else if (currentDefender === player2) {
damage2 = player2.attackPower * 2;
player1.takeDamage(damage2);
logElement2.innerHTML += `<p>${player2.name} scored a critical hit, dealing ${damage2} damage to ${player1.name}. ${player1.name}'s current health: ${player1.health}</p>`;
}
// Swap attacker and defender for the next round
[currentAttacker, currentDefender] = [currentDefender, currentAttacker];
if (currentDefender === player1 && player1.health <= 0) {
logElement2.innerHTML += `<p>${player2.name} wins the battle!</p>`;
battleResult = option.player2;
} else if (currentDefender === player2 && player2.health <= 0) {
logElement1.innerHTML += `<p>${player1.name} wins the battle!</p>`;
battleResult = option.player1;
}
if (currentAttacker === player2 && player2.health <= 0) {
logElement1.innerHTML += `<p>${player1.name} wins the battle!</p>`;
battleResult = option.player1;
} else if (currentAttacker === player1 && player1.health <= 0) {
logElement2.innerHTML += `<p>${player2.name} wins the battle!</p>`;
battleResult = option.player2;
}
}
let nextTextNodeId;
if (battleResult === option.player1) {
showTextNode(option.nextTextOnPlayer1Win);
} else if (battleResult === option.player2) {
showTextNode(option.nextTextOnPlayer2Win);
}
if (player1.health <= 0) {
nextTextNodeId = option.nextTextOnPlayer2Win;
} else {
nextTextNodeId = option.nextTextOnPlayer1Win;
}
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
return nextTextNodeId;
}
//Function that dynamically enters in text and options into the HTML page and removes options if below 4 exist.
function showTextNode(textNodeIndex) {
const textNode = textNodes.find((textNode) => textNode.id === textNodeIndex);
textElement.innerText = textNode.text;
while (optionButtonsElement.firstChild) {
optionButtonsElement.removeChild(optionButtonsElement.firstChild);
}
const filteredOptions = textNode.options.filter((option) => showOption(option));
textNode.options.forEach((option) => {
if (showOption(option)) {
const button = document.createElement("button");
button.innerText = option.text;
button.classList.add("btn");
button.addEventListener("click", () => selectOption(option));
optionButtonsElement.appendChild(button);
}
});
}
function showOption(option) {
if (option.requiredState) {
return option.requiredState(state);
}
if (option.requiredItem) {
return state[option.requiredItem];
}
if (option.id === 3 && state.smallRockDropped) {
return false;
}
if (option.id === 1 && option.text === "See if you can climb the stone walls over the prison bars.") {
return state.smallRock && !state.smallRockDropped;
}
return true;
}
//An important function that handles the next text logic based on who wins a battle, and if certain chosen options affect the players health/attack power.
function selectOption(option) {
const nextTextNodeId = option.nextText;
if (nextTextNodeId <= 0) {
return startGame();
}
log1.innerHTML = "";
log2.innerHTML = "";
let nextTextId;
if (option.prompt) {
alert(option.prompt);
}
if (option.nextTextOnPlayer1Win && option.nextTextOnPlayer2Win) {
// Battle is involved, so determine the winner
console.log(option);
const battleResult = battle(option.player1, option.player2, log1, log2, option);
if (option.player1 === mainCharacter){
player1.health = mainCharacter.health;
}
if (battleResult === option.player1) {
nextTextId = option.nextTextOnPlayer1Win;
} else if (battleResult === option.player2) {
nextTextId = option.nextTextOnPlayer2Win;
}
} else {
// No battle, just use nextText
nextTextId = option.nextText;
}
if (option.text === "Drink it now") {
const healthPotionItem = items.find(item => item.name === "Vial of Health");
if (healthPotionItem) {
const healingAmount = healthPotionItem.value;
mainCharacter.useHealingItem(healingAmount);
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
}
}
if (option.text === "Take the bread and continue down the hall") {
const breadRollItem = items.find(item => item.name === "Bread Roll");
if (breadRollItem) {
const healingAmount = breadRollItem.value;
mainCharacter.useHealingItem(healingAmount);
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
console.log(mainCharacter.health);
}
}
if (option.text === "Try to take a bread roll.") {
const breadRollItem = items.find(item => item.name === "Bread Roll");
if (breadRollItem) {
const healingAmount = breadRollItem.value;
mainCharacter.useHealingItem(healingAmount);
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
console.log(mainCharacter.health);
}
}
if (option.text === "Continue down the hall.") {
const guardArmorItem = items.find(item => item.name === "Leather Armor");
const guardSwordItem = items.find(item => item.name === "Chipped Sword");
if (guardArmorItem) {
const healingAmount = guardArmorItem.value;
mainCharacter.useHealingItem(healingAmount);
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
console.log(mainCharacter.health);
}
if (guardSwordItem) {
mainCharacter.attackPower += guardSwordItem.value;
const attackPowerElement = document.getElementById("attackPower");
attackPowerElement.textContent = `Attack Power: ${mainCharacter.attackPower}`;
console.log(mainCharacter.attackPower);
}
}
if (option.text === "Adjust your new armor and continue.") {
const guardArmorItem = items.find(item => item.name === "Leather Armor");
const guardSwordItem = items.find(item => item.name === "Chipped Sword");
if (guardArmorItem) {
const healingAmount = guardArmorItem.value;
mainCharacter.useHealingItem(healingAmount);
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
console.log(mainCharacter.health);
}
if (guardSwordItem) {
mainCharacter.attackPower += guardSwordItem.value;
const attackPowerElement = document.getElementById("attackPower");
attackPowerElement.textContent = `Attack Power: ${mainCharacter.attackPower}`;
console.log(mainCharacter.attackPower);
}
meadow.move(forest);
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
}
if (option.text === "Hurriedly climb down.") {
mainCharacter.health -= 10;
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
console.log(mainCharacter.health);
}
if (option.text === "Attack the Archduke!") {
const gideonSwordItem = items.find(item => item.name === "Gideon's Sword");
if (gideonSwordItem) {
mainCharacter.attackPower += gideonSwordItem.value;
const attackPowerElement = document.getElementById("attackPower");
attackPowerElement.textContent = `Attack Power: ${mainCharacter.attackPower}`;
console.log(mainCharacter.attackPower);
}
}
if (option.text === "Adjust Gideon's armor across your shoulders.") {
const gideonArmorItem = items.find(item => item.name === "Gold Armor");
const gideonSwordItem = items.find(item => item.name === "Gideon's Sword");
if (gideonArmorItem) {
const healingAmount = gideonArmorItem.value;
mainCharacter.useHealingItem(healingAmount);
const healthElement = document.getElementById("health");
healthElement.textContent = `Health: ${mainCharacter.health}`;
console.log(mainCharacter.health);
}
if (gideonSwordItem) {
mainCharacter.attackPower += gideonSwordItem.value;
const attackPowerElement = document.getElementById("attackPower");
attackPowerElement.textContent = `Attack Power: ${mainCharacter.attackPower}`;
console.log(mainCharacter.attackPower);
}
}
if (option.text === "Go through the doors."){
castle.move(courtyard);
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
}
if (option.text === "Take the left path, towards the meadow."){
courtyard.move(meadow);
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
}
if (option.text === "Move towards the horses."){
forest.move(plateau);
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
}
if (option.text === "Take the right path, deeper into the forest."){
courtyard.move(forest);
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
}
if (option.text === "Follow him."){
meadow.move(forest);
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
}
if (option.text === "Refuse, and run off into the woods."){
meadow.move(forest);
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
}
if (option.text === "Run!"){
courtyard.move(forest);
document.getElementById("locationImage").src = currentLocation.imageSrc;
document.getElementById("description").textContent = currentLocation.description;
}
if (nextTextId <= 0) {
startGame();
return;
}
const updatedState = Object.assign({}, state, option.setState); // Combine all state changes
state = updatedState; // Update the state+
showTextNode(nextTextId);
}
//All story text, options, setting current states, entering characters into battle. All is done via player choice based on which option is chosen
textNodes = [
{
id: 0.5,
text: "Welcome, " + mainCharacter.name +". You've been captured by your enemy, the Archduke, who threatens to kill you unless you hand over rulership of your kingdom, Scriptopia, to him. You must escape. Click continue to progress the story.",
options: [
{
text: "Continue",
nextText: 1,
},
]
},
{
id: 1,
text: "Awakening in a dark prison, you search your surroundings and see a skeleton in the corner, an old, ripped mattress on the floor. You also notice a small gap between the ceiling and top bar of the prison bars. Perhaps you could fit in?",
options: [
{
text: "Search the skeleton's clothes for anything of use.",
requiredState: (currentState) => !currentState.lockPickKept,
setState: {lockPick: true},
prompt: "You found a lock pick!",
nextText: 2
},
{
text: "See if you can climb the stone walls over the prison bars.",
setState: {smallRock: true},
prompt: "As your grip the wall, a rock comes loose! You gained a rock!",
requiredState: (currentState) => !currentState.smallRockDropped,
nextText: 3
},
{
text: "Look under the mattress.",
setState: {healthPotion: true},
requiredState: (currentState) => !currentState.healthPotionKept,
nextText: 1.3
},
{
text: "Accept your fate.",
setState: {playerDead: true},
nextText: 1.5
},
]
},
{
id: 1.3,
text: "Lifting the mattress, you find a tiny vial. It's dirty, but the red sparkling liquid inside looks fine.",
options: [
{
text: "Drink it now",
nextText: 1.35,
prompt: "You gain instantly +25 health! You're feeling much better.",
},
]
},
{
id: 1.35, //FIGHT
text: "Footsteps sound down the hall, and you quickly hide the empty vial as a guard appears at your prison door, opens it, and orders you outside.",
options: [
{
text: "Stay where you are.",
nextTextOnPlayer1Win: 3.7,
nextTextOnPlayer2Win: 3.81,
player1: mainCharacter,
player2: prisonGuard,
},
{
text: "Follow his order.",
nextText: 3.9
},
]
},
{
id: 2,
requiredState: (currentState) => currentState.lockPickKept,
text: "Running your thumb down the metal, you notice that the end is sharpened enough to possibly fit into the lock at the jail door.",
options: [
{
text: "Use the tool to pick the lock.",
requiredState: (currentState) => currentState.lockPick,
setState: { lockPick: true, lockPickBroken: true},
nextText: 4,
prompt: "The lock pick breaks just as the door unlocks.",
},
{
text: "Keep the pick for later.",
requiredState: (currentState) => currentState.lockPick,
setState: { lockPickKept: true },
nextText: 2.5,
},
]
},
{
id: 1.5,
text: "Eventually, the guard comes for you. Taking you up winding stairs of the tower. There, waiting, is the Archduke. Asking you once more for your country, you refuse, and he grows furious. He kills you before you even realize. You have died.",
options: [
{
text: "Restart?",
nextText: -1
},
]
},
{
id: 2.5,
requiredState: (currentState) => currentState.lockPick,
text: "You search your surroundings once more and see the climbable wall, and the old, ripped mattress on the floor.",
options: [
{
text: "Search the skeleton's clothes for anything of use.",
requiredState: (currentState) => !currentState.lockPickKept,
setState: {lockPick: true},
prompt: "You found a lock pick!",
nextText: 2
},
{
text: "See if you can climb the stone walls over the prison bars.",
setState: {smallRock: true},
requiredState: (currentState) => !currentState.smallRockDropped,
nextText: 3
},
{
text: "Look under the mattress.",
setState: {healthPotion: true},
requiredState: (currentState) => !currentState.healthPotionKept,
nextText: 1.3
},
{
text: "Accept your fate.",
setState: {playerDead: true},
nextText: 1.5
},
]
},
{
id: 3, // FIGHT
requiredState: (currentState) => currentState.smallRock,
text: "The guard hears you and orders you to drop the rock.",
options: [
{
text: "Refuse.",
nextTextOnPlayer1Win: 3.7,
nextTextOnPlayer2Win: 3.81,
player1: mainCharacter,
player2: prisonGuard,
},
{
text: "Drop the item.",
setState: {smallRockDropped: true},
nextText: 3.5,
},
]
},
{
id: 3.5,
requiredState: (currentState) => currentState.smallRockDropped,
text: "The guard glowers at you before once more disappearing. He's left you alone for now. You search your surroundings once more and see the skeleton in the corner, and the old, ripped mattress on the floor.",
options: [
{
text: "Search the skeleton's clothes for anything of use.",
requiredState: (currentState) => !currentState.lockPickKept,
setState: {lockPick: true},
prompt: "You found a lock pick!",
nextText: 2
},
{
text: "See if you can climb the stone walls over the prison bars.",
setState: {smallRock: true},
requiredState: (currentState) => !currentState.smallRockDropped,
nextText: 3
},
{
text: "Look under the mattress.",
setState: {healthPotion: true},
requiredState: (currentState) => !currentState.healthPotionKept,
nextText: 1.3
},
{
text: "Accept your fate.",
setState: {playerDead: true},
nextText: 1.5
},
]
},
{
id: 3.7,
text: "You defeated the guard! You take the guard's sword and some of his leather uniform and then hurry out of the prison cell.",
options: [
{
text: "Continue down the hall.",
prompt: "You gained 15 health and 15 attack power!",
setState: {guardArmor: true, guardSword: true, guardDead: true},
nextText: 5,
},
]
},
{
id: 3.8, // FIGHT //
text: "You know you will likely need this later. Putting it into your pocket, you hear footsteps coming down the hall. The guard comes to your cell, opens the door, and orders you outside.",
options: [
{
text: "Stay where you are.",
nextTextOnPlayer1Win: 3.7,
nextTextOnPlayer2Win: 3.81,
player1: mainCharacter,
player2: prisonGuard,
},
{
text: "Follow his order.",
nextText: 3.9
},
]
},
{
id: 3.81, //LOST TO GUARD
text: "You lose to the guard. Taking your arm, he hauls you to your feet and drags you up the tower and straight to the Archduke. You have died.",
options: [
{
text: "Restart?",
nextText: -1,
},
]
},
{
id: 3.9,
text: "He pushes you down the hallway in front of him, a hand on his sword. You both travel down a long, dark hallway and above the dampness of the stone walls, you smell bread. The smell gets bigger until you arrive in a brightly lit kitchen. An ornery cook glares at you both as you enter, and the guard shoves you aside as he closely examines the bread rolls lined up across the table to cool. The guard and the cook are both distracted.",
options: [
{
text: "Try to slowly sneak away",
prompt: "You get away successfully!",
nextText: 5,
},
{
text: "Don't move.",
nextText: 3.92,
},
{
text: "Try to take a bread roll.",
setState: {breadRoll: true},
prompt: "You succeed! You eat it right away, gaining 10 health!",
nextText: 3.93,
},
]
},
{
id: 3.92,
text: "The guard takes you up the winding stairs of the tower. There, waiting, is the Archduke. Asking you once more for your country, you refuse, and he grows furious. He kills you before you even realize. You have died.",
options: [
{
text: "Restart?",
nextText: -1
},
]
},
{
id: 3.93,
text: "The guard and the cook are both distracted still . . .",
requiredState: (currentState) => currentState.breadRoll,
options: [
{
text: "Try to slowly sneak away",
prompt: "You get away without them noticing!",
nextText: 3.95
},
{
text: "Don't move.",
nextText: 3.92
},
]
},
{
id: 3.95,
text: "You make your way down the hall and reach a tall, ominous door and a set of stairs to the right, or should you go back down the hall to try to take some bread?",
options: [
{
text: "Return to the kitchen.",
nextText: 5.6,
},
{
text: "Keep going down the hall.",
nextText: 5
},
]
},
{
id: 4,
text: "Carefully, you sneak down the hall. The smell of food wafts down the corridor, and you remember how hungry you are.",
options: [
{
text: "Follow your nose to the food.",
nextText: 4.1
},
{
text: "Keep going down the hall.",
nextText: 5
},
]
},
{
id: 4.1,
text: "You reach the kitchens, where the castle cook is preparing fresh bread rolls. Your stomach churns loudly, and the cook nearly drops a hot pan. They are startled, but don't seem to realize who you are. They quickly shove a few rolls at you and angrily shoo you from the kitchen.",
options: [
{
text: "Take the bread and continue down the hall",
prompt: "You gained a fresh bread roll!",
setState: {breadRoll: true},
nextText: 5,
},
]
},
{
id: 5,
text: "You make your way down the hall and reach a tall, ominous door and a set of stairs to the right",
options: [
{
text: "Go through the doors.",
// ENTER THE COURTYARD //
nextText: 6
},
{
text: "Go up the stairs.",
nextText: 5.4,
},
{
text: "Go back down the hall and get more bread",
requiredState: (currentState) => currentState.breadRoll,
nextText: 5.6
},
]
},
{
id: 5.4,
text: "The staircase is windy and dark. You think it's going on forever when you bump directly into someone. It's the Archduke! Furious at your escape, he immediately executes you. You have died.",
options: [
{
text: "Restart?",
nextText: -1