-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
328 lines (273 loc) · 9.25 KB
/
index.js
File metadata and controls
328 lines (273 loc) · 9.25 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
class Person {
constructor(name) {
this.name = name;
this.health = 100;
this.defense = 10;
this.attack = 10;
this.inventory = {}; // item key: value
this.partner = {}; // partner key : value
}
}
class Item {
// creates items
constructor(name, description, type, effect, quantity) {
this.name = name;
this.description = description;
this.type = type;
this.effect = effect;
this.quantity = quantity;
}
}
const water = new Item(
`Water`,
`A refreshing bottle of water that feels like magic to the soul. Some call it liquid healing. (+20HP)`,
"healing",
20,
1
);
const energyDrink = new Item(
`Energy Drink`,
`A tall can of lightning in a bottle. It will give you energy but at what cost?(-10HP)`,
"attacking",
10,
1
); //want to figure out how to have it also affect health
const seaMoss = new Item(
`Sea Moss`,
`A unique beverage acclaimed for it's health benefits. (+30HP)`,
"healing",
30,
1
);
class Move {
constructor(name, damage) {
this.name = name;
this.damage = damage;
}
effect(target) {
target.health -= this.damage;
}
}
const iceBlast = new Move("Ice Blast", 20);
const blobBuster = new Move("Blob Buster", 10);
const shadowSlash = new Move("Shadow Slash", 30);
const grassGun = new Move("Grass Gun", 50);
const lavaNuke = new Move("Lava Nuke", 80);
const mollyWhop = new Move("Molly Whop", 100);
const rainbowDestructor = new Move("Rainbow Destructor", 200);
const powerOfTheSun = new Move("Power of The Sun", 1000);
const globalMovePool = [
iceBlast,
blobBuster,
shadowSlash,
grassGun,
lavaNuke,
mollyWhop,
];
class GeneralMonster {
constructor(name) {
this.name = name;
this.moveSet = [];
this.item = [];
}
addMove() {
// make sure global move pool exists
if (!globalMovePool || globalMovePool.length === 0) {
return null;
}
// pick random index from global move pool
const randomIndex = Math.floor(Math.random() * globalMovePool.length);
const selectedMove = globalMovePool[randomIndex]; // we get the randomly selected move
// avoid duplicate moves so that the monster can't learn same move twice
if (!this.moveSet.includes(selectedMove)) {
this.moveSet.push(selectedMove);
return selectedMove;
}
// if move already exists, try again so we use recursion
return this.addMove();
}
}
class EnemyMonster extends GeneralMonster {
constructor(name, numMoves = 2) {
super(name);
this.health = 30;
this.defense = 5;
this.attack = 5;
//give enemy random moves
for (let i = 0; i < numMoves; i++) {
this.addMove(); //addMove to give enemy random moves
}
}
}
class MonPal extends GeneralMonster {
constructor(name, item, numMoves = 2) {
super(name);
}
}
const eIceBird = new EnemyMonster("IceBird", 2);
const eShadowBlobFish = new EnemyMonster("Shadow Blob", 2);
const eFireFossa = new EnemyMonster("fireFossa", 2);
const eIronPangolin = new EnemyMonster("Iron Pangolin", 2);
const fIceBird = new MonPal("IceBird", iceBlast);
const fShadowBlobFish = new MonPal("Shadow Blob", blobBuster);
const fFireFossa = new MonPal("fireFossa");
function exposition(player) {
const scene =
"The Valley of Choices lies before you: a land of shadowed ridges and dark rivers, where every path forks into risk and reward.";
("A mist coils at your feet and every sound-your breath, your heartbeat echoes louder than it should.");
const storyMode =
"Here, the valley tests not muscle, but decision. Every decision is a stone cast into the unseen depths. Attack recklessly, and you will get consumed.";
("Defend too often, and the shadows may close in.");
("Heal when you must, but the enemy will not wait.");
("For in this place, hesitation is death, and the courage is the only map.");
const playerStory = `${player.name}, you are not just a wanderer. You are a spark against the dark.
Your body is steady and your health soars at ${player.health} points, your guard sharpened by survival and a defense of ${player.defense} points, your strikes are sharp starting with hits as high as ${player.attack} points.`;
alert(scene);
alert(storyMode);
alert(playerStory);
}
function beginGame() {
const gameGreeting = confirm(`Would you like to play a game?`);
if (gameGreeting) {
alert(
`This world is full of suprises and will have you questioning your own abilities. Can you take on this journey on your own or will you need a little help from a friend? `
);
let name = prompt(`Awesome! What's your name?`);
if (name) {
alert(`Welcome to the World of MonPal, ${name}.`);
// create the player
const player = new Person(name);
// insert exposition function to be invoked
exposition(player);
startFight(player, eIronPangolin);
}
} else {
alert("Ok, bye...");
}
}
function addItem(target, itemName, item) {
if (!target.inventory[itemName]) {
target.inventory[itemName] = item;
} else {
target.inventory[itemName].quantity += 1;
}
}
function useItem(target, itemChoice) {
const choice = target.inventory[itemChoice];
if (!choice.type) {
alert("not a choice.");
}
switch (choice.type) {
case `healing`:
target.health += choice.effect;
alert(
`You used ${choice.name}. It changed your health by ${choice.effect}. Your health is now ${target.health}.`
);
break;
case `attacking`:
target.attack += choice.effect;
alert(
`You used ${choice.name}. It changed your attack by ${choice.effect} Your attack is now ${target.attack}.`
);
break;
default:
// this.health += this.effect;
alert(`I don't know this one...`);
}
}
function startFight(player, enemy, remainingEnemies = []) {
alert(
`Before you set off on your journey an empty inventory is a path to failure. The best way to stay on top is to stay hydrated. You grab a bottle of water before you go. "Water" has been added to your inventory. `
);
addItem(player, "Water", water);
alert(
`Uh oh, looks like trouble! An angry ${enemy.name} has appeared and wants to fight. ${player.name}, your current health is ${player.health}.`
);
tempHealth = enemy.health;
while (player.health > 0 && tempHealth > 0) {
let itemOrFight = prompt(
`How would you like to proceed? Type 1 to use item or Type 2 to fight.`
);
if (itemOrFight == "1") {
let itemNames = Object.keys(player.inventory).join(",");
let itemChoice = prompt(
`What item would you like to use? ${JSON.stringify(itemNames)}`
);
if (player.inventory[itemChoice]) {
useItem(player, itemChoice);
alert(
`Good Choice with ${itemChoice}! Your health is now ${player.health}.`
);
} else {
alert(
`That's not an item you have.You spent too much time searching for an item that doesn't exist. Your opponent is now attacking!`
);
}
} else {
let response = prompt(
`How would you like to proceed? Type attack to hit your opponent.`
);
if (response === "attack") {
let playerDamage = player.attack + 15 - enemy.defense; //(insert damage);
tempHealth -= playerDamage;
alert(
`Nice job you hit ${enemy.name}, its defense is ${enemy.defense} and your attack does ${playerDamage} damage. Their health is now ${tempHealth}.`
);
if (tempHealth <= 0) {
return endGame({ outcome: "win", player, enemy });
// return; //or start new round
}
} else {
alert(`It's really not the time for inaction, ${player.name}!`);
//insert consequence
}
}
//enemy begins
alert(`Think fast! It's ${enemy.name} turn!`);
let moveLength = enemy.moveSet.length - 1;
let movePicker = Math.round(Math.random() * moveLength);
let enemyMove = enemy.moveSet[movePicker].name;
let enemyDamage =
enemy.moveSet[movePicker].damage + enemy.attack - player.defense; //insert dmg
alert(
`Enemy uses ${enemyMove}, your defense is ${player.defense}, its attack is ${enemy.attack}, the move does ${enemy.moveSet[movePicker].damage} damage.`
);
player.health -= enemyDamage;
alert(
`Ouch, you got hit hard! and took ${enemyDamage} points of damage! Your health is now ${player.health}.`
);
// check if player is dead
if (player.health <= 0) {
return endGame({ outcome: "lose", player, enemy });
}
}
}
// Object.assign();
function symbioticRelationship(friendlyMon, player) {
Object.assign();
}
// Pseudocode - Kish
function endGame({ outcome, player, enemy, remainingEnemies = [] }) {
if (outcome === "win")
alert(`You did it, ${player.name}! You defeated ${enemy.name}.`);
//if there are more enemies, start the next battle
if (remainingEnemies.length > 0) {
const nextEnemy = remainingEnemies.shift();
alert(`A new Enemy appears ${nextEnemy.name}! Prepare yourself!`);
return startFight();
}
//No more enemies = Win
//Ask the user if they want to replay
const replay = confirm(
outcome === "win"
? "Play again from the start?"
: "Try again from the start"
);
//if yes, game has to restart from the existing entry flow
if (replay) {
beginGame();
} else {
alert("Thanks for playing!");
}
}
beginGame();