Removed workaround an added working solution for bug in combat system

This commit is contained in:
Patryk Hegenberg 2023-01-07 19:59:05 +01:00
parent fa41049853
commit a22e9e250a

View file

@ -533,29 +533,16 @@
*** combat/win/lose events *** *** combat/win/lose events ***
******************************/ ******************************/
// this is how the player fights a monster // this is how the player fights a monster
function checkSolution(solution, answer, hitter, receiver) { function checkSolution(solution, answer) {
console.log("Click: " + solution + " Antwort: " + answer); console.log("Click: " + solution + " Antwort: " + answer);
if (solution == answer) { if (solution == answer) {
hitter.stats.hp -= 1; return true;
sfx["hit"].play();
if (checkDeath(hitter)) {
Game.player.stats.xp += 1;
//showScreen("game");
//Game.playerAllowedToMove = true;
//Game.engine.unlock();
} else {
combat(hitter, receiver);
}
checkDeath(hitter);
} else { } else {
sfx["miss"].play(); return false;
//showScreen("game");
//Game.playerAllowedToMove = true;
//Game.engine.unlock();
} }
} }
function setupButtons(answerValue, hitter, receiver) { async function setupButtons(answerValue) {
const randomValue = (min, max) => const randomValue = (min, max) =>
Math.floor(Math.random() * (max - min)) + min; Math.floor(Math.random() * (max - min)) + min;
let randomVar = randomValue(1, 4); let randomVar = randomValue(1, 4);
@ -584,34 +571,52 @@
}`; }`;
document.getElementById("answer3").innerHTML = `${answerValue}`; document.getElementById("answer3").innerHTML = `${answerValue}`;
} }
document.getElementById("answer1").addEventListener("click", async() => { showScreen("combat");
checkSolution(document.getElementById("answer1").innerText, answerValue, hitter, receiver); return new Promise((resolve) => {
clickCount += 1; const button1 = document.getElementById("answer1");
console.log(clickCount); const button2 = document.getElementById("answer2");
}, {once: true}); const button3 = document.getElementById("answer3");
document.getElementById("answer2").addEventListener("click", async() => { button1.addEventListener('click', () => {
checkSolution(document.getElementById("answer2").innerText, answerValue, hitter, receiver); resolve(button1.innerText);
clickCount += 1; });
console.log(clickCount); button2.addEventListener('click', () => {
}, {once: true}); resolve(button2.innerText);
document.getElementById("answer3").addEventListener("click", async() => { });
checkSolution(document.getElementById("answer3").innerText, answerValue, hitter, receiver); button3.addEventListener('click', () => {
clickCount += 1; resolve(button3.innerText);
console.log(clickCount); });
}, {once: true}); });
} }
function combat(hitter, receiver) { async function combat(hitter, receiver) {
console.log("hitter: " + hitter.name);
console.log("receiver: " + receiver.name);
console.log(hitter.stats.hp);
let msg = [];
const randomValue = (min, max) => const randomValue = (min, max) =>
Math.floor(Math.random() * (max - min)) + min; Math.floor(Math.random() * (max - min)) + min;
let [num1, num2] = [randomValue(1, 10), randomValue(1, 10)]; let [num1, num2] = [randomValue(1, 10), randomValue(1, 10)];
const answerValue = eval(`${num1} * ${num2}`); const answerValue = eval(`${num1} * ${num2}`);
test = prompt(`${num1} * ${num2} = ?`); document.getElementById("question").innerHTML = `${num1} * ${num2} = ? `;
checkSolution(test, answerValue, hitter, receiver); const clicked = await setupButtons(answerValue);
//document.getElementById("question").innerHTML = `${num1} * ${num2} = ? `; let fight = checkSolution(clicked, answerValue);
//setupButtons(answerValue, hitter, receiver); if(fight) {
//showScreen("combat"); console.log(fight);
checkDeath(receiver); msg.push("You hit the monster");
hitter.stats.hp -= 1;
sfx["hit"].play();
Game.player.stats.xp += 1;
} else {
console.log(false);
sfx["miss"].play();
msg.push("The monster hit you.");
Game.player.stats.hp -= 1;
}
if(msg) {
toast(battleMessage(msg));
}
checkDeath(hitter);
showScreen("game");
renderStats(Game.player.stats); renderStats(Game.player.stats);
} }