const Game = { phases: { preAction: [], action: [], postAction: [], }, isInBattle: false, didTechniqueHit: false, async progressTurn () { Memory.state.turn++; await Game.applyStatusEffect(Memory.state.opponent.activeMonster); await Game.applyStatusEffect(Memory.state.player.activeMonster); for (const event of Game.phases.preAction) { event(); } Game.phases.preAction = []; for (const event of Game.phases.action) { event(); } Game.phases.action = []; for (const event of Game.phases.postAction) { event(); } Game.phases.postAction = []; // opponent defeated if (Memory.state.opponent.activeMonster.hp <= 0) { // money Memory.state.money += Memory.state.opponent.activeMonster.level * Memory.state.opponent.activeMonster.moneyModifier; // exp Memory.state.player.activeMonster.exp += calculateAwardedExperience(Memory.state.opponent.activeMonster, [Memory.state.player.activeMonster])[0]; if (Memory.state.player.activeMonster.canLevelUp()) { Memory.state.player.activeMonster.levelUp(); } if (Memory.state.player.activeMonster.canEvolve()) { await Game.evolveMonster(Memory.state.player.activeMonster); } Game.isInBattle = false; if (Memory.state.opponent.type === 'monster') { Memory.state.currentArea.monsterProgress++; await Game.encounterWildMonster(); } else if (Memory.state.opponent.type === 'trainer') { if (Memory.state.opponent.activeMonster === Memory.state.opponent.monsters[Memory.state.opponent.monsters.length - 1]) { await Game.progressToArea(await fetchArea(Memory.state.currentArea.nextArea)); } else { await Game.encounterNextTrainerMonster(); } } } UI.progressTurn(); }, /** * @param {Technique} technique * @param {Monster} user * @param {Monster} target */ async tryUseTechnique (technique, user, target) { let canUse = true; // recharge if (technique.isRecharging()) { const feedbackNode = UI.createActionFeedback('recharge'); feedbackNode.classList.add('recharge'); UI.drawActionFeedback(feedbackNode); canUse = false; } // noddingoff if (user.statusEffect && user.statusEffect.slug === StatusEffectType.noddingoff) { const feedbackNode = UI.createActionFeedback('noddingoff'); UI.drawActionFeedback(feedbackNode); canUse = false; } if (canUse) { // hit? const accuracy = Math.random(); Game.didTechniqueHit = technique.accuracy >= accuracy; if (!Game.didTechniqueHit) { technique.use(); UI.drawDamageMiss(UI.createDamageMiss()); return; } await Game.useTechnique(technique, user, target); } }, /** * @param {Technique} technique * @param {Monster} user * @param {Monster} target */ async useTechnique (technique, user, target) { technique.use(); for (const techniqueEffectCode of technique.effects) { const techniqueEffect = new TechniqueEffect(techniqueEffectCode); techniqueEffect.setUser(user); techniqueEffect.setTarget(target); // damage if (['damage', 'splash', 'area'].includes(techniqueEffect.type)) { Game.phases.action.push(() => { const damage = simpleDamageCalculation(technique, user, target); target.hp -= damage; const damageNode = UI.createDamage(damage); UI.applyMultiplierToDamage(damageNode, simpleDamageMultiplier(technique.types, target.types)); UI.applyTechniqueToDamage(damageNode, technique); UI.drawDamage(damageNode); UI.drawTechniqueAnimation(technique); }); } // money else if (techniqueEffect.type === 'money') { Game.phases.action.push(() => { const money = Math.max(1, Math.floor(Math.random() * target.level)); Memory.state.money += money; const damageNode = UI.createDamage(`${money} €`); UI.applyTechniqueToDamage(damageNode, technique); UI.drawDamage(damageNode); UI.drawTechniqueAnimation(technique); }); } // healing else if (techniqueEffect.type === 'healing') { for (const recipient of techniqueEffect.recipients) { console.log((user.level + 7) * technique.healingPower); recipient.hp += (user.level + 7) * technique.healingPower; } } // enhance else if (techniqueEffect.type === 'enhance') { UI.drawTechniqueAnimation(technique); } // status effect else if (techniqueEffect.type === 'status') { const statusEffect = await fetchStatusEffect(techniqueEffect.statusEffect); if (statusEffect.slug === 'lifeleech') { statusEffect.issuer = user; } Game.phases.postAction.push(() => { // add status effect const potency = Math.random(); const success = technique.potency >= potency; if (success) { for (const recipient of techniqueEffect.recipients) { // TODO: check replace if (recipient.statusEffect) continue; recipient.statusEffect = statusEffect; } } }); UI.drawTechniqueAnimation(technique); } } }, /** * @param {Monster} monster */ async applyStatusEffect (monster) { if (!monster.statusEffect) { return; } if (monster.statusEffect.turnsLeft === 0) { Game.phases.preAction.push(() => { monster.statusEffect.onRemove && monster.statusEffect.onRemove(); // if still 0 turns left after remove action if (monster.statusEffect.turnsLeft === 0) { monster.statusEffect = null; } else { Game.applyStatusEffect(monster); } }); return; } // poison / burn if (monster.statusEffect.slug === 'poison' || monster.statusEffect.slug === 'burn') { const statusEffectDamage = Math.floor(monster.stats.hp / 8); Game.phases.postAction.push(() => { monster.hp -= statusEffectDamage; const damageNode = UI.createDamage(statusEffectDamage); UI.applyStatusEffectToDamage(damageNode, monster.statusEffect); UI.drawDamage(damageNode); }); } // lifeleech else if (monster.statusEffect.slug === 'lifeleech') { const statusEffectLeech = Math.floor(monster.stats.hp / 16); Game.phases.postAction.push(() => { monster.hp -= statusEffectLeech; monster.statusEffect.issuer.hp += statusEffectLeech; const damageNode = UI.createDamage(statusEffectLeech); UI.applyStatusEffectToDamage(damageNode, monster.statusEffect); UI.drawDamage(damageNode); }); } // recover else if (monster.statusEffect.slug === 'recover') { const statusEffectHeal = Math.floor(monster.stats.hp / 16); Game.phases.postAction.push(() => { monster.hp += statusEffectHeal; const feedbackNode = UI.createActionFeedback(statusEffectHeal); UI.applyStatusEffectToDamage(feedbackNode, monster.statusEffect); UI.drawActionFeedback(feedbackNode); }); } // stuck else if (monster.statusEffect.slug === 'stuck') { for (const technique of monster.activeTechniques) { if ([TechniqueRange.melee, TechniqueRange.touch].includes(technique.range)) { Game.phases.preAction.push(() => { technique.potency = technique.stats.potency * 0.5; technique.power = technique.stats.power * 0.5; }); monster.statusEffect.onRemove = () => { technique.resetStats(); }; } } } // grabbed else if (monster.statusEffect.slug === 'grabbed') { for (const technique of monster.activeTechniques) { if ([TechniqueRange.ranged, TechniqueRange.reach].includes(technique.range)) { Game.phases.preAction.push(() => { technique.potency = technique.stats.potency * 0.5; technique.power = technique.stats.power * 0.5; }); monster.statusEffect.onRemove = () => { technique.resetStats(); }; } } } // charging else if (monster.statusEffect.slug === 'charging') { const nextStatusEffect = await fetchStatusEffect('chargedup'); monster.statusEffect.onRemove = () => { monster.statusEffect = nextStatusEffect; }; } // statchange else if (monster.statusEffect.effects.includes('statchange')) { monster.resetStatModifiers(); for (const statType in monster.statusEffect.stats) { const statChange = monster.statusEffect.stats[statType]; const modifiedValue = Math.floor(eval(`${monster.stats[statType]} ${statChange.operation} ${statChange.value}`)); Game.phases.preAction.push(() => { monster.setStatModifier(statType, modifiedValue); }); } monster.statusEffect.onRemove = () => { monster.resetStatModifiers(); }; } Game.phases.postAction.push(() => { monster.statusEffect.turnsLeft--; }); }, /** * @param {MouseEvent} event */ async battleClick (event) { Game.isInBattle = true; UI.battleClickEvent = event; await Game.tryUseTechnique(Memory.state.activeTechnique, Memory.state.player.activeMonster, Memory.state.opponent.activeMonster); Game.progressTurn(); }, /** * @param {MouseEvent} event */ techniqueClick (event) { if (event.target === UI.elements.techniques) { return; } let target = event.target; while (!target.classList.contains('techniques__technique')) { target = target.parentNode; } const idx = [...UI.elements.techniques.children].indexOf(target); Memory.state.activeTechnique = Memory.state.player.activeMonster.activeTechniques[idx]; // trigger battle click 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.battleOpponent.dispatchEvent(new MouseEvent('click', { clientX: Math.random() * (xMax - xMin) + xMin, clientY: Math.random() * (yMax - yMin) + yMin, })); }, /** * @param {InventoryItem} */ async useItem (item) { for (const itemEffectCode of item.effects) { const itemEffect = new ItemEffect(itemEffectCode); if (itemEffect.type === 'evolve') { const evolution = Memory.state.player.activeMonster.getPossibleEvolutions('item')[0]; if (evolution) { await fetchMonster(evolution.monster_slug); Memory.state.player.activeMonster.evolve(evolution); UI.drawActiveMonster(); } } } }, /** * @param {Monster} monster */ async evolveMonster (monster) { await fetchMonster(monster.evolutions[0].monster_slug); monster.evolve(monster.evolutions[0]); }, async catchMonster () { if (Memory.state.opponent.type === 'trainer') { return; } const caughtMonster = new Monster(Memory.state.opponent.activeMonster.slug); caughtMonster.initialize(); caughtMonster.level = Memory.state.opponent.activeMonster.level; Memory.state.player.monsters.push(caughtMonster); await Game.encounterWildMonster(); await Game.progressTurn(); }, getStageOfDaySimple () { const hours = (new Date()).getHours(); if (hours >= 6 && hours < 18) { return 'day'; } else { return 'night'; } }, /** * @param {Area} area */ async progressToArea (area) { Memory.state.currentArea = area; UI.drawArea(area); UI.drawStatus(); if (area.encounters.length > 0) { await Game.encounterWildMonster(); } else { await Game.encounterTrainer(); } }, async encounterWildMonster () { const randomMonster = Memory.state.currentArea.encounters[Math.floor(Math.random() * Memory.state.currentArea.encounters.length)]; const randomLevel = Math.floor(Math.random() * (randomMonster.level_range[1] - randomMonster.level_range[0]) + randomMonster.level_range[0]); const monster = await fetchMonster(randomMonster.monster); monster.level = randomLevel; const wildMonster = new Trainer([monster]); wildMonster.type = 'monster'; await wildMonster.initialize(); Memory.state.opponent = wildMonster; UI.drawOpponentMonster(); }, async encounterTrainer () { const nextTrainer = Memory.state.currentArea.trainers[Memory.state.currentArea.trainerProgress]; const trainer = new Trainer(nextTrainer.monsters, nextTrainer.inventory); if (nextTrainer.name === 'Rival') { trainer.monsters.push(Memory.state.rivalMonster); } await trainer.initialize() Memory.state.opponent = trainer; UI.drawOpponentMonster(); }, async encounterNextTrainerMonster () { const activeMonsterIdx = Memory.state.opponent.monsters.indexOf(Memory.state.opponent.activeMonster); Memory.state.opponent.activeMonster = Memory.state.opponent.monsters[activeMonsterIdx + 1]; UI.drawOpponentMonster(); }, }; // Game click bindings UI.elements.status.querySelector('[data-template-slot="next-trainer"]').addEventListener('click', Game.encounterTrainer); UI.elements.battleOpponent.addEventListener('click', Game.battleClick); UI.elements.techniques.addEventListener('click', Game.techniqueClick); UI.elements.menuCatch.addEventListener('click', Game.catchMonster);