-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
563 lines (509 loc) · 17.7 KB
/
script.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
// Victor Ortiz & Justin Yim 6/4/19
/*
* **************************
* Initialize Game Objects
* Player, Items, Enemies...
* **************************
*/
/////////////
// Items //
/////////////
// weapons
knife = { name: "Sharp Knife", damage: 2 };
bow = { name: "Hunting Bow", damage: 3 };
sword = { name: "Broadsword", damage: 4 };
axe = { name: "War Axe", damage: 5 };
wand = { name: "Magic Wand", damage: 6 };
godSpear = { name: "Spear of the Hero", damage: 10 };
// Potions
smallPotion = { heal: 5 };
largePotion = { heal: 15 };
/////////////
// Player //
/////////////
// player object, which consists of a name, health, and experience and item(s)
player = {
name: "",
health: 0,
weapon: knife,
currentXP: 0,
treasures: 0
};
/////////////
// Enemies //
/////////////
ogre = { name: "Ogre", hp: 15, attack: 10, xpGranted: 25, escapeChance: .25, drop: wand };
troll = { name: "Troll", hp: 8, attack: 3, xpGranted: 5, escapeChance: .60, drop: bow };
centaur = { name: "Centaur", hp: 12, attack: 4, xpGranted: 15, escapeChance: .35, drop: axe };
gargoyle = { name: "Gargoyle", hp: 10, attack: 5, xpGranted: 10, escapeChance: .40, drop: sword };
minotaur = { name: "Minotaur", hp: 14, attack: 6, xpGranted: 10, escapeChance: .50, drop: knife };
// ...Game objects finished initializing
// Begin Methods for game functions...
// Set up game
function startOfGame() {
player.name = prompt("What is your name?"); {
if (player.name == "" | player.name == null) {
player.name = "Player One";
}
}
document.getElementById("playerName").innerHTML = player.name;
player.health = 25;
player.weapon = knife;
player.currentXP = 0;
player.treasures = 0;
updateUI();
console.log("Welcome to Fight or Flight!\n\n" +
"You are an adventurer seeking to grow strong enough to leave your home. \n" +
"Defeat monsters to gain experience, keep fighting until you are confident \n" +
"enough to leave on a real adventure!\n\n" +
"Player Name set to: " + player.name + "\n\n");
}
// win game at 100 experience points
function winGame() {
if (player.currentXP >= 100) {
alert("Congratulations " + player.name + "!\n\n" +
"You have become a veteran adventurer and monster slayer.\n" +
"You decide to rest to prepare for tomorrow...");
setColor("gold");
console.clear();
gameSummary();
console.log(sessionStorage.getItem("thisPlay"));
playAgain();
}
}
// lose game at 0 or less health
function endOfGame() {
if (player.health < 1) {
stopTimer();
setColor("red");
alert("Game Over!");
console.clear();
console.log("\n" + player.name + " has been killed!\n\n");
gameSummary();
console.log(sessionStorage.getItem("thisPlay"));
playAgain();
}
}
// ask player to play again
function playAgain() {
if (confirm("Play again " + player.name + "?")) {
setColor("grey");
console.clear();
startOfGame();
}
else {
setColor("grey");
player.health = 25;
player.currentXP = 0;
updateUI();
console.clear();
gameSummary();
console.log(sessionStorage.getItem("thisPlay"));
}
}
// method to store play history and show the user
function gameSummary() {
var message = "**** Last Play Record ****\n\n" + "Player Name: " + player.name + "\n\n" +
"Total Experience: " + player.currentXP + "\n\n" +
"Equipped Weapon: " + player.weapon.name + "\n\n" +
"**** Last Play Record ****\n\n";
sessionStorage.setItem("thisPlay", message);
}
// update status on page & check if win or end of game condition
function updateUI() {
document.getElementById("numHealth").innerHTML = player.health;
document.getElementById("numCurrentXP").innerHTML = player.currentXP;
document.getElementById("currentWeapon").innerHTML = player.weapon.name;
document.getElementById("weaponDamage").innerHTML = player.weapon.damage;
document.getElementById("treasureCount").innerHTML = player.treasures;
// check if experience points are 100 or more, if so player wins
winGame();
// zero or less health, game over
endOfGame();
}
// change background color
function setColor(color) {
document.body.style.background = color;
}
// method that generates a random event encounter
function callRandomEvent() {
// random number from 0 to 1 multiply by cases, round to nearest whole integer
var randomNum = Math.floor(Math.random() * 6) // multiply by total number of cases + 1
switch (randomNum) {
case 1:
stopTimer();
eventOne();
startTimer();
document.getElementById("actionView").src = "images/startRoad.jpg";
break;
case 2:
stopTimer();
eventTwo();
startTimer();
document.getElementById("actionView").src = "images/startRoad.jpg";
break;
case 3:
stopTimer();
eventThree();
startTimer();
document.getElementById("actionView").src = "images/startRoad.jpg";
break;
case 4:
stopTimer();
eventFour();
startTimer();
document.getElementById("actionView").src = "images/startRoad.jpg";
break;
case 5:
stopTimer();
eventFive();
startTimer();
document.getElementById("actionView").src = "images/startRoad.jpg";
break;
}
}
/**************************************************************************
There are 5 random events that can occur, below are all possible events
**************************************************************************/
// first random event is finding a healing potion
function eventOne() {
document.getElementById("actionView").src = "images/smallPotion.jpg";
if (confirm("You found a Small Health Potion!\n\n" +
"'OK' to drink it or 'Cancel' to save it.")) {
setColor("green");
if (player.health <= 50) {
player.health += smallPotion.heal;
console.log("You gained " + smallPotion.heal + " health.");
}
else
console.log("You greedily gulp the potion down, but nothing happens...\n" +
"No overhealing!");
}
else {
// if player does not drink it, it is added to the treasure count
player.treasures ++;
setColor("grey");
console.log("You decide to stash the potion in your bag.");
}
updateUI();
}
// second random event is finding a large health potion
function eventTwo() {
document.getElementById("actionView").src = "images/bigPotion.jpg";
if (confirm("You found a Large Health Potion!\n\n" +
"'OK' to drink it or 'Cancel' to save it.")) {
setColor("green");
if (player.health <= 50) {
player.health += largePotion.heal;
console.log("You gained " + largePotion.heal + " health.");
}
}
else {
player.treasures += 1;
setColor("grey");
console.log("You decide to stash potion in your bag.");
}
updateUI();
}
// third random event is responsible for monster encounters
function eventThree() {
alert("You see something ahead...\n" +
"It's a monster!");
setColor("yellow");
callRandomEnemy();
}
// fourth random event you encounter a shrine
function eventFour() {
document.getElementById("actionView").src = "images/shrine.jpg";
if (confirm("You encounter a shrine to the gods...\n" +
"Offer your stashed items to the shrine?")) {
setColor("grey");
console.log("You offer your treasures to the shrine.");
if (player.treasures >= 15) {
player.treasures -= 15;
player.weapon = godSpear;
setColor("gold");
console.log("A loud crack of lightning shoots down and carves a crater next to you.\n" +
"Crackling with energy, an ornate spear is embedded in the ground. You pick it up.");
}
else {
player.treasures -= player.treasures;
console.log("Your offering wasn't enough to please the gods...");
}
updateUI();
}
else {
setColor("grey");
console.log("You walk past the shrine. The Gods can find their own offerings.");
}
}
// fifth random event you encounter a dead adventurer
function eventFive() {
document.getElementById("actionView").src = "images/corpse.jpg";
if (confirm("A dead adventurer lies across the road.\n" +
"Loot the body?")) {
player.treasures += 3;
setColor("yellow");
updateUI();
console.log("Sifting through the remains, you find several gold coins and take them.");
}
else {
setColor("grey");
console.log("You let the dead rest, theirs is not yours to take.");
}
}
// method that generates a random enemy encounter
function callRandomEnemy() {
// random number from 0 to 1 multiply by cases, round to nearest whole integer
var randomNum = Math.floor(Math.random() * 6) // multiply by total number of cases + 1
switch (randomNum) {
case 1:
enemyOne();
break;
case 2:
enemyTwo();
break;
case 3:
enemyThree();
break;
case 4:
enemyFour();
break;
case 5:
enemyFive();
break;
}
}
// first random method that generates an ogre encounter
function enemyOne() {
document.getElementById("actionView").src = "images/ogre.jpg";
if (confirm("You got attacked by an Orge!\n" +
"Try to fight it?\n\n" +
"'OK' to fight or 'Cancel' to try to escape.")) {
setColor("grey");
// call method to fight ogre
deathMatch(ogre);
}
else {
// call escape method to test if escape action fails or succeeds
if (escape(ogre)) {
setColor("green");
console.log("You got away by the skin of your teeth!");
}
else {
setColor("red");
player.health -= ogre.attack;
console.log("You got injured trying to escape, you lost " + enemy.attack + " health.");
deathMatch(ogre);
}
}
}
// second random method that generates a troll encounter
function enemyTwo() {
document.getElementById("actionView").src = "images/troll.jpg";
if (confirm("You got attacked by a Troll!\n" +
"Try to fight it?\n\n" +
"'OK' to fight or 'Cancel' to try to escape.")) {
setColor("grey");
deathMatch(troll);
}
else {
if (escape(troll)) {
setColor("green");
console.log("You got away by the skin of your teeth!");
}
else {
setColor("red");
player.health -= troll.attack;
console.log("You got injured trying to escape, you lost " + enemy.attack + " health.");
deathMatch(troll);
}
}
}
// third random method that generates an centaur encounter
function enemyThree() {
document.getElementById("actionView").src = "images/centaur.jpg";
if (confirm("You got attacked by a Centaur!\n" +
"Try to fight it?\n\n" +
"'OK' to fight or 'Cancel' to try to escape.")) {
setColor("grey");
deathMatch(centaur);
}
else {
if (escape(centaur)) {
setColor("green");
console.log("You got away by the skin of your teeth!");
}
else {
setColor("red");
player.health -= centaur.attack;
console.log("You got injured trying to escape, you lost " + enemy.attack + " health.");
deathMatch(centaur);
}
}
}
// fourth random method that generates an gargoyle encounter
function enemyFour() {
document.getElementById("actionView").src = "images/gargoyle.jpg";
if (confirm("You got attacked by a Gargoyle!\n" +
"Try to fight it?\n\n" +
"'OK' to fight or 'Cancel' to try to escape.")) {
setColor('grey');
deathMatch(gargoyle);
}
else {
if (escape(gargoyle)) {
setColor("green");
console.log("You got away by the skin of your teeth!");
}
else {
setColor("red");
player.health -= gargoyle.attack;
console.log("You got injured trying to escape, you lost " + enemy.attack + " health.");
deathMatch(gargoyle);
}
}
}
// fifth random method that generates an minotaur encounter
function enemyFive() {
document.getElementById("actionView").src = "images/minotaur.jpg";
if (confirm("You got attacked by a Minotaur!\n" +
"Try to fight it?\n\n" +
"'OK' to fight or 'Cancel' to try to escape.")) {
setColor("grey");
deathMatch(minotaur);
}
else {
if (escape(minotaur)) {
setColor("green");
console.log("You got away by the skin of your teeth!");
}
else {
setColor("red");
player.health -= minotaur.attack;
console.log("You got injured trying to escape, you lost " + enemy.attack + " health.");
deathMatch(minotaur);
}
}
}
//////////////////////////////////////////////////
// Time methods that determine pace of the game //
//////////////////////////////////////////////////
ticks = 0; // start at zero ticks
// clockTick function to time called events
function clockTick() {
// ticks in 1/2 seconds
ticks = (ticks + 1) % 1000;
// calls a random event every 2 seconds
if ((ticks % 4) == 0)
callRandomEvent();
}
interval = 0; // startTimer & stopTimer variable
// startTimer sets clockTick to run at 500 milliseconds
function startTimer() {
interval = setInterval(clockTick, 500);
}
// stops the clockTick function
function stopTimer() {
clearInterval(interval);
}
///////////////////////////////////////////////////////////////////////////////////////////////
// event listeners that allow user to use enter and space to start / pause game //
// source: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_trigger_button_enter //
///////////////////////////////////////////////////////////////////////////////////////////////
// use enter key to start game
addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("startClockTicks").click();
}
});
// use spacebar key to pause game
addEventListener("keyup", function (event) {
if (event.keyCode === 32) {
event.preventDefault();
document.getElementById("stopClockTicks").click();
}
});
//////////////////////////////////////////
// Procedures for dealing with monsters //
//////////////////////////////////////////
// method where you deal damage to enemy or kill the enemy
function fight(enemy, enemyHP) {
if (player.weapon.damage < enemyHP) {
setColor("red");
player.health -= enemy.attack;
enemyHP -= player.weapon.damage;
console.log("You dealt " + player.weapon.damage + " damage to the " + enemy.name +
" and lost " + enemy.attack + " health.");
updateUI();
}
else if (player.weapon.damage >= enemyHP) {
enemyHP -= player.weapon.damage;
setColor("gold");
player.currentXP += enemy.xpGranted;
console.log("You slew the monster and gained " + enemy.xpGranted + " experience!");
if (confirm("The " + enemy.name + " dropped a " + enemy.drop.name + ". Equip it? \n\n " +
"You will replace your current weapon.")) {
player.weapon = enemy.drop;
}
else {
console.log("You add the " + enemy.drop.name + " to your stash.");
player.treasures += 1;
}
updateUI();
}
return enemyHP;
}
// method where player must either A: kill the monster or B: successfully flees the fight
function deathMatch(enemy) {
var currentEnemyHealth = enemy.hp;
currentEnemyHealth = fight(enemy, currentEnemyHealth);
// fight loop continues until either the player or monster is dead
while (player.health > 0 && currentEnemyHealth > 0) {
if (player.weapon.damage < enemy.hp) {
if (confirm("Continue the fight or flee?\n\n" +
"'OK' to fight or 'Cancel' to try to escape.")) {
currentEnemyHealth = fight(enemy, currentEnemyHealth);
}
else {
if (escape(enemy)) {
break;
}
else if (!escape(enemy)) {
currentEnemyHealth = fight(enemy, currentEnemyHealth);
}
}
}
else {
setColor("gold");
player.currentXP += enemy.xpGranted;
console.log("You gained " + enemy.xpGranted + " experience!");
updateUI();
break;
}
}
}
// boolean method that determines if player made a successful escape attempt
function escape(enemy) {
var chance = Math.random();
if (chance > enemy.escapeChance) {
setColor("green");
console.log("Your escape roll was: " + Math.floor(chance * 100) + "%\n" /*+
"You got away by the skin of your teeth!"*/);
return true;
}
else {
setColor("red");
console.log("Your escape roll was: " + Math.floor(chance * 100) + "%\n" +
"Escape failed, you have to fight again!");
return false;
}
}
function checkStatus(player) {
var properties = [];
for (property in player) {
properties.push(property);
}
console.log(properties);
}