From 65887a9ed56cdc5531f9350d69d1cbc3909240c1 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Thu, 12 Jan 2023 16:48:18 +0100 Subject: [PATCH] added leveling function to the math wizard game --- scripts/game.js | 18 +++++++++++------- views/game.view.php | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/scripts/game.js b/scripts/game.js index 283463f..f33c111 100644 --- a/scripts/game.js +++ b/scripts/game.js @@ -368,7 +368,7 @@ character: "@", name: statsOfPlayer['username'].replace('"',''), // the player's stats - stats: { hp: 10, xp: Number(statsOfPlayer['xp']), gold: Number(statsOfPlayer['coins']) }, + stats: { hp: 10, xp: 0, gold: 0 }, // the ROT.js scheduler calls this method when it is time // for the player to act act: () => { @@ -418,7 +418,6 @@ // check if we've hit the monster const hitMonster = monsterAt(x, y); if (hitMonster) { - //combat(p, hitMonster); setTimeout(function () { Game.engine.unlock(); }, 250); @@ -601,7 +600,13 @@ let msg = []; const randomValue = (min, max) => Math.floor(Math.random() * (max - min)) + min; - let [num1, num2] = [randomValue(1, 10), randomValue(1, 10)]; + let num1, num2; + if (statsOfPlayer['level'] > 10) { + [num1, num2] = [randomValue(1, 10), randomValue(1, 10)]; + } else { + num1 = statsOfPlayer['level']; + num2 = randomValue(1, 10); + } const answerValue = eval(`${num1} * ${num2}`); document.getElementById("question").innerHTML = `${num1} * ${num2} = ? `; const clicked = await setupButtons(answerValue); @@ -744,16 +749,15 @@ function setEndScreenValues(xp, gold) { $$(".xp-stat").forEach((el) => (el.textContent = Math.floor(xp))); $$(".gold-stat").forEach((el) => (el.textContent = gold)); - statsOfPlayer["coins"] = gold; + statsOfPlayer["coins"] += gold; statsOfPlayer["username"] = Game.player.name.trim().replace('"',''); - statsOfPlayer["lesson_count"] = Number(statsOfPlayer["lesson_count"]); if (xp > 150) { statsOfPlayer["level"] = Number(statsOfPlayer["level"]) + 1; xp -= 150; - statsOfPlayer["xp"] = xp; + statsOfPlayer["xp"] += xp; } else { statsOfPlayer["level"] = Number(statsOfPlayer["level"]); - statsOfPlayer["xp"] = xp; + statsOfPlayer["xp"] += xp; } var json = JSON.stringify(statsOfPlayer); var xhr = new XMLHttpRequest(); diff --git a/views/game.view.php b/views/game.view.php index f88a5f0..cdc732f 100644 --- a/views/game.view.php +++ b/views/game.view.php @@ -110,7 +110,7 @@

Du hast das Spiel gewonnen.

Du hast Gold und XP erhalten.

- + @@ -122,7 +122,7 @@

Du bist tot.

Du hast Gold und XP erhalten.

- +