scrimba
Create an RPG game - Rob Sutcliffe
separate function and procedure
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

separate function and procedure
by
AboutCommentsNotes
separate function and procedure
by
Expand for more info
index.js
run
preview
console
import characterData from "./data";
import Character from "./Character";

let monstersArray = ["spider", "scorpion", "dragon"];
let isWaiting = false;

function getNextMonster() {
const monsterData = characterData[monstersArray.shift()];
return monsterData ? new Character(monsterData) : {};
}

function render() {
document.getElementById(hero.elementId).innerHTML = hero.getTemplate();
document.getElementById(monster.elementId).innerHTML = monster.getTemplate();
}

function endGame(reason) {
document.body.innerHTML = reason === "hero died" ?
'<h3 class="end-game">💀 Sorry, try again</h3>':
'<h3 class="end-game">🥳 Congratulations you completed the dungeon!!</h3>';
}


// sepereate out the coditionals into a new function called
// checkWinstate
function attackPhase() {
if(!isWaiting) {

const heroAttackScore = hero.rollDice();
const monsterAttackScore = monster.rollDice();

hero.takeDamage(monsterAttackScore);
monster.takeDamage(heroAttackScore);

if(hero.dead) {
isWaiting = true;
setTimeout(()=>endGame("hero died"),1000);
}
if(monster.dead) {
isWaiting = true;
setTimeout(()=>{
monster = getNextMonster();
monster.health ?
render():
endGame("won");
isWaiting = false;
}, 1000);

}

render();
}
}


const hero = new Character(characterData.hero);
let monster = getNextMonster();

render();

document.getElementById("attack-button").onclick = attackPhase;
Console
/index.html
-1:25