kallisor.js
1    let herbPower = 10;
2    let root = "http://coding.stephenjwolf.com/cfk2/images/";
3    let monster;
4    let hero = getHero();
5    
6    //text boxes
7    let text = document.getElementById("text");
8    let foe = document.getElementById("foe-info");
9    let stats = document.getElementById("stats");
10   
11   //image
12   let image = document.getElementById("foe-image");
13   image.src = root + "eaglon.png";
14   
15   //buttons
16   let fightButton = document.getElementById("fight");
17   let herbButton = document.getElementById("herb");
18   let defendButton = document.getElementById("defend");
19   let runButton = document.getElementById("run");
20   let okButton = document.getElementById("ok");
21   
22   fightButton.onclick = fight;
23   herbButton.onclick = herb;
24   defendButton.onclick = defend;
25   runButton.onclick = run;
26   okButton.onclick = startGame;
27   
28   //introduce game
29   let info = "<h3>Welcome to Battles in Kallisor</h3>";
30   info += "<p>You are " + hero.name + ", a warrior. ";
31   info += "Fight against the monsters that roam Kallisor. ";
32   info += "Use herbs to heal your wounds. ";
33   info += "Try to run from fights that are too hard.</p>";
34   info += "<p>When you're ready, click OK.</p>";
35   text.innerHTML = info;
36   showOK(true);
37   
38   
39 function getHero() 40 { 41 let hero = { 42 name: "Gabrion", 43 hp: 20, 44 hpMax: 20, 45 attack: 5, 46 defense: 3, 47 defending: false, 48 herbs: 2, 49 exp: 0, 50 level: 1, 51 gold: 0, 52 enemies: [] 53 }; 54 return hero; 55 } 56
57 function startGame() 58 { 59 showOK(false); 60 okButton.onclick = nextMonster; 61 okButton.innerHTML = "OK"; 62 text.innerHTML = ""; 63 hero = getHero(); 64 monster = getMonster(); 65 showStats(); 66 } 67
68 function showOK(show) 69 { 70 if (show) 71 { 72 okButton.style.display = "block"; 73 } 74 else 75 { 76 okButton.style.display = "none"; 77 } 78 } 79 80
81 function showStats() 82 { 83 stats.innerHTML = "HP: " + hero.hp + "/" + hero.hpMax; 84 stats.innerHTML += "<br>Herbs: " + hero.herbs; 85 stats.innerHTML += "<br>Exp: " + hero.exp; 86 stats.innerHTML += "<br>Gold: " + hero.gold; 87 stats.innerHTML += "<br>Attack: " + hero.attack; 88 stats.innerHTML += "<br>Defense: " + hero.defense; 89 stats.innerHTML += "<br>Level: " + hero.level; 90 91 foe.innerHTML = monster.name; 92 foe.innerHTML += "<br>HP: " + monster.hp + "/" + monster.hpMax; 93 } 94
95 function fight() 96 { 97 text.innerHTML = ""; 98 doBattle(hero, monster); 99 checkMonster(); 100 showStats(); 101 } 102
103 function monsterTurn() 104 { 105 doBattle(monster, hero); 106 checkHero(); 107 showStats(); 108 } 109
110 function nextMonster() 111 { 112 showOK(false); 113 okButton.innerHTML = "OK"; 114 text.innerHTML = ""; 115 monster = getMonster(); 116 showStats(); 117 } 118
119 function doBattle(attacker, defender) 120 { 121 text.innerHTML += "<p>" + attacker.name + " attacks.</p>"; 122 123 let attack = attacker.attack; 124 let random = Math.floor(Math.random() * 10); 125 if (random === 0) 126 { 127 attack++; 128 } 129 else if (random === 1) 130 { 131 attack--; 132 } 133 134 let defense = defender.defense; 135 let random2 = Math.floor(Math.random() * 10); 136 if (random2 === 0) 137 { 138 defense++; 139 } 140 else if (random2 === 1) 141 { 142 defense--; 143 } 144 145 if (defender.defending) 146 { 147 text.innerHTML += "<p>" + defender.name + " is defending.</p>"; 148 defense *= 2; 149 defender.defending = false; 150 } 151 152 let damage = Math.floor(attack - (defense / 2)); 153 if (damage > 0) 154 { 155 text.innerHTML += attacker.name + " scores "; 156 text.innerHTML += damage + " point"; 157 if (damage > 1) 158 { 159 text.innerHTML += "s"; 160 } 161 text.innerHTML += " of damage against "; 162 text.innerHTML += defender.name + "."; 163 defender.hp -= damage; 164 defender.hp = Math.max(defender.hp, 0); 165 } 166 else 167 { 168 text.innerHTML += attacker.name + " misses!"; 169 } 170 } 171
172 function checkHero() 173 { 174 if (hero.hp === 0) 175 { 176 text.innerHTML += "<br>" + hero.name; 177 text.innerHTML += " has been slain."; 178 gameOver(); 179 } 180 } 181
182 function checkMonster() 183 { 184 if (monster.hp === 0) 185 { 186 text.innerHTML += "<p>" + monster.name + " has been defeated.</p>"; 187 hero.enemies.push(monster.name); 188 awardPrizes(); 189 } 190 else 191 { 192 monsterTurn(); 193 } 194 } 195 196
197 function gameOver() 198 { 199 showOK(true); 200 okButton.onclick = startGame; 201 okButton.innerHTML = "YOU DIED!"; 202 text.innerHTML += "<p>Monsters defeated:</p>"; 203 text.innerHTML += hero.enemies.join(", "); 204 text.innerHTML += "<h3>Click OK to play again.</h3>"; 205 } 206
207 function awardPrizes() 208 { 209 text.innerHTML += "<br>" + monster.exp + " exp earned<br>"; 210 text.innerHTML += monster.gold + " gold found<br>"; 211 212 hero.exp += monster.exp; 213 hero.gold += monster.gold; 214 215 if (Math.random() < 0.1) 216 { 217 text.innerHTML += "Herb found!"; 218 hero.herbs++; 219 } 220 221 checkLevelUp(); 222 showStats(); 223 showOK(true); 224 } 225 226
227 function herb() 228 { 229 if (hero.herbs === 0) 230 { 231 text.innerHTML = hero.name + " doesn't have any herbs."; 232 } 233 else 234 { 235 text.innerHTML = hero.name + " used an herb."; 236 hero.herbs--; 237 hero.hp += herbPower; 238 hero.hp = Math.min(hero.hp, hero.hpMax); 239 } 240 monsterTurn(); 241 } 242
243 function run() 244 { 245 if (Math.random() < 0.25) 246 { 247 text.innerHTML = hero.name + " runs away!"; 248 okButton.onclick = nextMonster; 249 showOK(true); 250 } 251 else 252 { 253 text.innerHTML = hero.name + " could not escape."; 254 monsterTurn(); 255 } 256 } 257
258 function defend() 259 { 260 text.innerHTML = hero.name + " defends."; 261 hero.defending = true; 262 monsterTurn(); 263 } 264 265
266 function checkLevelUp() 267 { 268 if (hero.exp > 15 * hero.level) 269 { 270 text.innerHTML += "<h3>" + hero.name + " leveled up!</h3>"; 271 let attack = Math.floor(Math.random() * 3) + 1; 272 let defense = Math.floor(Math.random() * 3) + 1; 273 274 text.innerHTML += "Attack increased by " + attack + ".<br>"; 275 text.innerHTML += "Defense increased by " + defense + ".<br>"; 276 277 hero.attack += attack; 278 hero.defense += defense; 279 hero.level++; 280 281 okButton.innerHTML = "LEVEL UP!"; 282 } 283 } 284
285 function getMonster() 286 { 287 let mon = {}; 288 let pick = Math.floor(Math.random() * (hero.level + 1)); 289 if (pick === 0) 290 { 291 mon = { 292 name: "Squirret", 293 src: "squirret.png", 294 hpMax: 5, 295 gold: 2, 296 exp: 2, 297 attack: 2, 298 defense: 3 299 }; 300 } 301 else if (pick === 1) 302 { 303 mon = { 304 name: "Swallomer", 305 src: "swallomer.png", 306 hpMax: 10, 307 gold: 3, 308 exp: 3, 309 attack: 4, 310 defense: 3 311 }; 312 } 313 else if (pick === 2) 314 { 315 mon = { 316 name: "Gleese", 317 src: "gleese.png", 318 hpMax: 12, 319 gold: 6, 320 exp: 6, 321 attack: 5, 322 defense: 6 323 }; 324 } 325 else if (pick === 3) 326 { 327 mon = { 328 name: "Rodia", 329 src: "rodia.png", 330 hpMax: 14, 331 gold: 8, 332 exp: 7, 333 attack: 5, 334 defense: 8 335 }; 336 } 337 else if (pick === 4) 338 { 339 mon = { 340 name: "Eaglon", 341 src: "eaglon.png", 342 hpMax: 18, 343 gold: 10, 344 exp: 8, 345 attack: 7, 346 defense: 8 347 }; 348 } 349 else if (pick === 5) 350 { 351 mon = { 352 name: "Lupino", 353 src: "lupino.png", 354 hpMax: 25, 355 gold: 15, 356 exp: 11, 357 attack: 8, 358 defense: 9 359 }; 360 } 361 else if (pick === 6) 362 { 363 mon = { 364 name: "Tigroar", 365 src: "tigroar.png", 366 hpMax: 28, 367 gold: 18, 368 exp: 10, 369 attack: 9, 370 defense: 10 371 }; 372 } 373 else if (pick === 7) 374 { 375 mon = { 376 name: "Brigand", 377 src: "brigand.png", 378 hpMax: 33, 379 gold: 25, 380 exp: 15, 381 attack: 11, 382 defense: 13 383 }; 384 } 385 else if (pick === 8) 386 { 387 mon = { 388 name: "Wild mage", 389 src: "wildmage.png", 390 hpMax: 24, 391 gold: 22, 392 exp: 18, 393 attack: 9, 394 defense: 15 395 }; 396 } 397 else if (pick === 9) 398 { 399 mon = { 400 name: "Sandorpion", 401 src: "sandorpion.png", 402 hpMax: 35, 403 gold: 29, 404 exp: 21, 405 attack: 18, 406 defense: 10 407 }; 408 } 409 else 410 { 411 mon = { 412 name: "Testalor", 413 src: "test.png", 414 hpMax: 25, 415 gold: 25, 416 exp: 25, 417 attack: 15, 418 defense: 15 419 }; 420 } 421 422 image.src = root + mon.src; 423 mon.hp = mon.hpMax; 424 text.innerHTML += "<h3>" + hero.name + " is attacked by " + mon.name + ".</h3>"; 425 return mon; 426 } 427 428