From 218da95a4b387ee5ac25f168d1529419039f2e54 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sat, 19 Aug 2023 13:18:54 +0200 Subject: refactor: enemy -> opponent, State.monsters~ -> State.Trainer.monsters --- resources/js/game.js | 216 +++++++++++---------------------------------------- 1 file changed, 45 insertions(+), 171 deletions(-) (limited to 'resources/js/game.js') diff --git a/resources/js/game.js b/resources/js/game.js index f68e2fb..833265e 100644 --- a/resources/js/game.js +++ b/resources/js/game.js @@ -1,6 +1,3 @@ -const state = new State(); - - const Game = { phases: { preAction: [], @@ -9,140 +6,12 @@ const Game = { }, didTechniqueHit: false, - turn: 0, - - - /** - * @returns {string} - */ - save () { - const saveMonster = (monsterData, monsterState) => { - monsterData.level = monsterState.level; - monsterData.hp = monsterState.hp; - - if (monsterData.statusEffect && monsterData.statusEffect.slug === 'lifeleech') { - monsterData.statusEffect = null; - } - - return monsterData; - }; - - const saveData = JSON.parse(JSON.stringify(state)); - - for (const idx in saveData.monsters) { - saveData.monsters[idx] = saveMonster(saveData.monsters[idx], state.monsters[idx]); - } - - for (const idx in saveData.partyMonsters) { - saveData.partyMonsters[idx] = saveMonster(saveData.partyMonsters[idx], state.partyMonsters[idx]); - } - - saveData.activeMonsterIdx = state.partyMonsters.indexOf(state.activeMonster); - - saveData.enemy.monster = saveMonster(saveData.enemy.monster, state.enemy.monster); - - return btoa(JSON.stringify(saveData)); - }, - - /** - * @param {string} saveData - */ - async load (saveData) { - /** - * @param {Monster} monsterData - */ - const loadMonster = async (monsterData) => { - const monster = await fetchMonster(monsterData.slug); - - monster.level = monsterData.level; - monster.hp = monsterData.hp; - monster.exp = monsterData.exp; - monster.tasteWarm = monsterData.tasteWarm; - monster.tasteCold = monsterData.tasteCold; - monster.gender = monsterData.gender; - monster.heldItem = await loadItem(monsterData.heldItem); - monster.statusEffect = await loadStatusEffect(monsterData.statusEffect); - monster.statModifiers = monsterData.statModifiers; - monster.experienceModifier = monsterData.experienceModifier; - monster.moneyModifier = monsterData.moneyModifier; - monster.activeTechniques = await Promise.all(monsterData.activeTechniques.map(async (technique) => { - return loadTechnique(technique); - })); - - return monster; - }; - - /** - * @param {InventoryItem} inventoryItemData - */ - const loadInventoryItem = async (inventoryItemData) => { - const inventoryItem = new InventoryItem(await fetchItem(inventoryItemData.item.slug)); - - inventoryItem.quantity = inventoryItemData.quantity; - - return inventoryItem; - }; - - /** - * @param {Item} itemData - */ - const loadItem = async (itemData) => { - if (!itemData) { - return null; - } - - const item = await fetchItem(itemData.slug); - - return item; - }; - - /** - * @param {StatusEffect} statusEffectData - */ - const loadStatusEffect = async (statusEffectData) => { - if (!statusEffectData) { - return null; - } - - const statusEffect = await fetchStatusEffect(statusEffectData.slug); - - statusEffect.turnsLeft = statusEffectData.turnsLeft; - - return statusEffect; - }; - - /** - * @param {Technique} techniqueData - */ - const loadTechnique = async (techniqueData) => { - const technique = await fetchTechnique(techniqueData.slug); - - return technique; - }; - - /** - * @type {State} - */ - const loadedState = JSON.parse(atob(saveData)); - - state.money = loadedState.money; - state.monsters = await Promise.all(loadedState.monsters.map(async (monsterData) => await loadMonster(monsterData))); - state.inventory = await Promise.all(loadedState.inventory.map(async (itemData) => await loadInventoryItem(itemData))); - state.partyMonsters = await Promise.all(loadedState.partyMonsters.map(async (monsterData) => await loadMonster(monsterData))); - state.activeMonster = state.partyMonsters[loadedState.activeMonsterIdx]; - state.activeTechnique = await loadTechnique(loadedState.activeTechnique); - state.enemy.monster = await loadMonster(loadedState.enemy.monster); - - UI.drawEnemyMonster(); - UI.drawActiveMonster(); - UI.drawActiveTechniques(); - }, async progressTurn () { - Game.turn++; + Memory.state.turn++; - await Game.applyStatusEffect(state.enemy.monster); - await Game.applyStatusEffect(state.activeMonster); + await Game.applyStatusEffect(Memory.state.opponent.activeMonster); + await Game.applyStatusEffect(Memory.state.player.activeMonster); for (const event of Game.phases.preAction) { event(); @@ -157,30 +26,30 @@ const Game = { } Game.phases.postAction = []; - // enemy defeated - if (state.enemy.monster.hp <= 0) { + // opponent defeated + if (Memory.state.opponent.activeMonster.hp <= 0) { // money - state.money += state.enemy.monster.level * state.enemy.monster.moneyModifier; + Memory.state.money += Memory.state.opponent.activeMonster.level * Memory.state.opponent.activeMonster.moneyModifier; // exp - state.activeMonster.exp += calculateAwardedExperience(state.enemy.monster, [state.activeMonster])[0]; + Memory.state.player.activeMonster.exp += calculateAwardedExperience(Memory.state.opponent.activeMonster, [Memory.state.player.activeMonster])[0]; - if (state.activeMonster.canLevelUp()) { - state.activeMonster.levelUp(); + if (Memory.state.player.activeMonster.canLevelUp()) { + Memory.state.player.activeMonster.levelUp(); } - if (state.activeMonster.canEvolve()) { - await fetchMonster(state.activeMonster.evolutions[0].monster_slug); - state.activeMonster.evolve(); + if (Memory.state.player.activeMonster.canEvolve()) { + await fetchMonster(Memory.state.player.activeMonster.evolutions[0].monster_slug); + Memory.state.player.activeMonster.evolve(); } - await Game.spawnEnemyMonster(); + await Game.spawnOpponentMonster(); } - UI.drawEnemyMonster(); + UI.drawOpponentMonster(); UI.drawActiveMonster(); UI.drawActiveTechniques(); - UI.elements.money.textContent = `${state.money} €`; + UI.elements.money.textContent = `${Memory.state.money} €`; }, /** @@ -239,12 +108,12 @@ const Game = { // damage if (['damage', 'splash', 'area'].includes(techniqueEffect.type)) { Game.phases.action.push(() => { - const damage = simpleDamageCalculation(state.activeTechnique, state.activeMonster, state.enemy.monster); + const damage = simpleDamageCalculation(technique, user, target); - state.enemy.monster.hp -= damage; + target.hp -= damage; const damageNode = UI.createDamage(damage); - UI.applyMultiplierToDamage(damageNode, simpleDamageMultiplier(state.activeTechnique.types, state.enemy.monster.types)); - UI.applyTechniqueToDamage(damageNode, state.activeTechnique); + UI.applyMultiplierToDamage(damageNode, simpleDamageMultiplier(technique.types, target.types)); + UI.applyTechniqueToDamage(damageNode, technique); UI.drawDamage(damageNode); UI.drawTechniqueAnimation(technique); }); @@ -254,10 +123,10 @@ const Game = { else if (techniqueEffect.type === 'money') { Game.phases.action.push(() => { const money = Math.max(1, Math.floor(Math.random() * target.level)); - state.money += money; + Memory.state.money += money; const damageNode = UI.createDamage(`${money} €`); - UI.applyTechniqueToDamage(damageNode, state.activeTechnique); + UI.applyTechniqueToDamage(damageNode, technique); UI.drawDamage(damageNode); UI.drawTechniqueAnimation(technique); }); @@ -265,7 +134,10 @@ const Game = { // healing else if (techniqueEffect.type === 'healing') { - techniqueEffect.recipient.hp = techniqueEffect.recipient.stats.hp; + for (const recipient of techniqueEffect.recipients) { + console.log((user.level + 7) * technique.healingPower); + recipient.hp += (user.level + 7) * technique.healingPower; + } } // enhance @@ -287,10 +159,12 @@ const Game = { const success = technique.potency >= potency; if (success) { - // TODO: check replace - if (techniqueEffect.recipient.statusEffect) return; + for (const recipient of techniqueEffect.recipients) { + // TODO: check replace + if (recipient.statusEffect) continue; - techniqueEffect.recipient.statusEffect = statusEffect; + recipient.statusEffect = statusEffect; + } } }); @@ -426,14 +300,14 @@ const Game = { }); }, - async spawnEnemyMonster () { - state.enemy.monster = await fetchMonster(DB.allMonsters[Math.floor(Math.random() * DB.allMonsters.length)]); - state.enemy.monster.level = Math.ceil(Math.random() * state.activeMonster.level); + async spawnOpponentMonster () { + Memory.state.opponent.activeMonster = await fetchMonster(DB.allMonsters[Math.floor(Math.random() * DB.allMonsters.length)]); + Memory.state.opponent.activeMonster.level = Math.ceil(Math.random() * Memory.state.player.activeMonster.level); - // state.enemy.monster.experienceModifier = state.enemy.monster.level; - state.enemy.monster.moneyModifier = state.enemy.monster.level; + // Memory.state.opponent.activeMonster.experienceModifier = Memory.state.opponent.activeMonster.level; + Memory.state.opponent.activeMonster.moneyModifier = Memory.state.opponent.activeMonster.level; - UI.drawEnemyMonster(); + UI.drawOpponentMonster(); }, /** @@ -442,7 +316,7 @@ const Game = { async battleClick (event) { UI.battleClickEvent = event; - await Game.tryUseTechnique(state.activeTechnique, state.activeMonster, state.enemy.monster); + await Game.tryUseTechnique(Memory.state.activeTechnique, Memory.state.player.activeMonster, Memory.state.opponent.activeMonster); Game.progressTurn(); }, @@ -461,32 +335,32 @@ const Game = { } const idx = [...UI.elements.techniques.children].indexOf(target); - state.activeTechnique = state.activeMonster.activeTechniques[idx]; + Memory.state.activeTechnique = Memory.state.player.activeMonster.activeTechniques[idx]; // trigger battle click - const rect = UI.elements.battleEnemy.getBoundingClientRect(); + const rect = UI.elements.battleOpponent.getBoundingClientRect(); const xMin = rect.left + 64; const xMax = rect.right - 64; const yMin = rect.top + 32; const yMax = rect.bottom - 32; - UI.elements.battleEnemy.dispatchEvent(new MouseEvent('click', { + UI.elements.battleOpponent.dispatchEvent(new MouseEvent('click', { clientX: Math.random() * (xMax - xMin) + xMin, clientY: Math.random() * (yMax - yMin) + yMin, })); }, catchMonster () { - const caughtMonster = new Monster(state.enemy.monster.slug); + const caughtMonster = new Monster(Memory.state.opponent.activeMonster.slug); caughtMonster.initialize(); - caughtMonster.level = state.enemy.monster.level; + caughtMonster.level = Memory.state.opponent.activeMonster.level; - state.partyMonsters.push(caughtMonster); + Memory.state.player.monsters.push(caughtMonster); - Game.spawnEnemyMonster(); + Game.spawnOpponentMonster(); }, }; // Game click bindings -UI.elements.battleEnemy.addEventListener('click', Game.battleClick); +UI.elements.battleOpponent.addEventListener('click', Game.battleClick); UI.elements.techniques.addEventListener('click', Game.techniqueClick); UI.elements.menuCatch.addEventListener('click', Game.catchMonster); -- cgit v1.2.3