summaryrefslogtreecommitdiff
path: root/resources/js/game.js
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-08-22 15:01:03 +0200
committerDaniel Weipert <code@drogueronin.de>2023-08-22 15:01:03 +0200
commitc0354b250f84d578b609a7f25d71dee7fc24e9ca (patch)
treeaf586e0a4c44a2f2c8df956ca3b992be15daaba3 /resources/js/game.js
parent54e5ffaeb79f989463c144e58dfcd677b752c5a9 (diff)
currency, save/load
Diffstat (limited to 'resources/js/game.js')
-rw-r--r--resources/js/game.js130
1 files changed, 80 insertions, 50 deletions
diff --git a/resources/js/game.js b/resources/js/game.js
index d5e8669..5a31c39 100644
--- a/resources/js/game.js
+++ b/resources/js/game.js
@@ -1,17 +1,21 @@
const Game = {
phases: {
- preAction: {
- opponent: [],
- player: [],
- },
- action: {
- opponent: [],
- player: [],
- },
- postAction: {
- opponent: [],
- player: [],
+ preTurn: [],
+ battle: {
+ preAction: {
+ opponent: [],
+ player: [],
+ },
+ action: {
+ opponent: [],
+ player: [],
+ },
+ postAction: {
+ opponent: [],
+ player: [],
+ },
},
+ postTurn: [],
},
logMessages: [],
@@ -37,26 +41,36 @@ const Game = {
Game.logTurn('begin');
// Phases
- for (const phaseKey of Object.keys(Game.phases)) {
- for (const event of Game.phases[phaseKey].player) {
+ for (const event of Game.phases.preTurn) {
+ event();
+ }
+ Game.phases.preTurn = [];
+
+ for (const phaseKey of Object.keys(Game.phases.battle)) {
+ for (const event of Game.phases.battle[phaseKey].player) {
event();
await Game.handleDefeatOpponent();
if (!Game.playerIsChoosingNextMonster) await Game.handleDefeatPlayer();
}
- Game.phases[phaseKey].player = [];
+ Game.phases.battle[phaseKey].player = [];
Game.doBattleAnimation = false;
- for (const event of Game.phases[phaseKey].opponent) {
+ for (const event of Game.phases.battle[phaseKey].opponent) {
event();
await Game.handleDefeatOpponent();
if (!Game.playerIsChoosingNextMonster) await Game.handleDefeatPlayer();
}
Game.doBattleAnimation = true;
- Game.phases[phaseKey].opponent = [];
+ Game.phases.battle[phaseKey].opponent = [];
}
+ for (const event of Game.phases.postTurn) {
+ event();
+ }
+ Game.phases.postTurn = [];
+
Game.logTurn('end');
UI.progressTurn();
@@ -67,13 +81,17 @@ const Game = {
if (Memory.state.opponent.activeMonster.hp <= 0) {
clearTimeout(Game.opponentActionTimeout);
Game.opponentActionTimeout = null;
- for (const phase of Object.keys(Game.phases)) {
- Game.removePhaseEvents(phase, 'opponent');
+ for (const phase of Object.keys(Game.phases.battle)) {
+ Game.removeBattlePhaseEvents(phase, 'opponent');
}
- Game.removePhaseEvents('action', 'player');
+ Game.removeBattlePhaseEvents('action', 'player');
// money
- Memory.state.money += Memory.state.opponent.activeMonster.level * Memory.state.opponent.activeMonster.moneyModifier;
+ const money = calculateAwardedMoney(Memory.state.opponent.activeMonster);
+ Memory.state.money += money;
+ Game.addPhaseEvent('postTurn', () => {
+ Game.log(`Got ${money} money!`);
+ });
// exp
Memory.state.player.activeMonster.exp += calculateAwardedExperience(Memory.state.opponent.activeMonster, [Memory.state.player.activeMonster])[0];
@@ -109,10 +127,10 @@ const Game = {
if (Memory.state.player.activeMonster.hp <= 0) {
clearTimeout(Game.opponentActionTimeout);
Game.opponentActionTimeout = null;
- for (const phase of Object.keys(Game.phases)) {
- Game.removePhaseEvents(phase, 'player');
+ for (const phase of Object.keys(Game.phases.battle)) {
+ Game.removeBattlePhaseEvents(phase, 'player');
}
- Game.removePhaseEvents('action', 'opponent');
+ Game.removeBattlePhaseEvents('action', 'opponent');
// whole party defeated
if (!Memory.state.player.monsters.some((monster) => monster.hp > 0)) {
@@ -149,15 +167,15 @@ const Game = {
},
/**
- * @param {Object} phase
+ * @param {('preAction' | 'action' | 'postAction')} phase
* @param {Monster} monster
* @param {Function} event
*/
- addPhaseEvent (phase, monster, event) {
+ addBattlePhaseEvent (phase, monster, event) {
if (monster === Memory.state.player.activeMonster) {
- phase.player.push(event);
+ Game.phases.battle[phase].player.push(event);
} else {
- phase.opponent.push(event);
+ Game.phases.battle[phase].opponent.push(event);
}
},
@@ -165,17 +183,25 @@ const Game = {
* @param {('preAction' | 'action' | 'postAction')} phase
* @param {('player' | 'opponent')} type
*/
- removePhaseEvents (phase, type) {
- Game.phases[phase][type] = [];
+ removeBattlePhaseEvents (phase, type) {
+ Game.phases.battle[phase][type] = [];
+ },
+
+ /**
+ * @param {('preTurn' | 'postTurn')} phase
+ * @param {Function} event
+ */
+ addPhaseEvent (phase, event) {
+ Game.phases[phase].push(event);
},
clearAllPhaseEvents () {
- Game.removePhaseEvents('preAction', 'player');
- Game.removePhaseEvents('preAction', 'opponent');
- Game.removePhaseEvents('action', 'player');
- Game.removePhaseEvents('action', 'opponent');
- Game.removePhaseEvents('postAction', 'player');
- Game.removePhaseEvents('postAction', 'opponent');
+ Game.removeBattlePhaseEvents('preAction', 'player');
+ Game.removeBattlePhaseEvents('preAction', 'opponent');
+ Game.removeBattlePhaseEvents('action', 'player');
+ Game.removeBattlePhaseEvents('action', 'opponent');
+ Game.removeBattlePhaseEvents('postAction', 'player');
+ Game.removeBattlePhaseEvents('postAction', 'opponent');
},
clearCurrentTurn () {
@@ -193,7 +219,7 @@ const Game = {
let canUse = true;
const log = (message, indentation) => {
- Game.addPhaseEvent(Game.phases.preAction, user, () => {
+ Game.addBattlePhaseEvent('preAction', user, () => {
Game.log(message, indentation);
});
};
@@ -251,7 +277,7 @@ const Game = {
async useTechnique (technique, user, target) {
technique.use();
- Game.addPhaseEvent(Game.phases.action, user, () => {
+ Game.addBattlePhaseEvent('action', user, () => {
Game.log(`${user.name} is using ${technique.name}!`);
});
@@ -262,7 +288,7 @@ const Game = {
// damage
if (['damage', 'splash', 'area'].includes(techniqueEffect.type)) {
- Game.addPhaseEvent(Game.phases.action, user, () => {
+ Game.addBattlePhaseEvent('action', user, () => {
const damage = simpleDamageCalculation(technique, user, target);
target.hp -= damage;
@@ -281,7 +307,7 @@ const Game = {
// money
else if (techniqueEffect.type === 'money') {
- Game.addPhaseEvent(Game.phases.action, user, () => {
+ Game.addBattlePhaseEvent('action', user, () => {
const money = Math.max(1, Math.floor(Math.random() * target.level));
Memory.state.money += money;
@@ -297,7 +323,7 @@ const Game = {
// healing
else if (techniqueEffect.type === 'healing') {
for (const recipient of techniqueEffect.recipients) {
- Game.addPhaseEvent(Game.phases.action, user, () => {
+ Game.addBattlePhaseEvent('action', user, () => {
const heal = (user.level + 7) * technique.healingPower;
recipient.hp += heal;
@@ -324,7 +350,7 @@ const Game = {
statusEffect.issuer = user;
}
- Game.addPhaseEvent(Game.phases.action, user, () => {
+ Game.addBattlePhaseEvent('action', user, () => {
// add status effect
const potency = Math.random();
const success = technique.potency >= potency;
@@ -355,7 +381,7 @@ const Game = {
}
if (monster.statusEffect.turnsLeft === 0) {
- Game.addPhaseEvent(Game.phases.preAction, monster, () => {
+ Game.addBattlePhaseEvent('preAction', monster, () => {
monster.statusEffect.onRemove && monster.statusEffect.onRemove();
// if still 0 turns left after remove action
@@ -379,7 +405,7 @@ const Game = {
if (monster.statusEffect.slug === 'poison' || monster.statusEffect.slug === 'burn') {
const statusEffectDamage = Math.floor(monster.stats.hp / 8);
- Game.addPhaseEvent(Game.phases.postAction, monster, () => {
+ Game.addBattlePhaseEvent('postAction', monster, () => {
monster.hp -= statusEffectDamage;
if (Game.doBattleAnimation) {
@@ -396,7 +422,7 @@ const Game = {
else if (monster.statusEffect.slug === 'lifeleech') {
const statusEffectLeech = Math.floor(monster.stats.hp / 16);
- Game.addPhaseEvent(Game.phases.postAction, monster, () => {
+ Game.addBattlePhaseEvent('postAction', monster, () => {
// if issuer is defeated => don't
if (monster.statusEffect.issuer.hp <= 0) {
return;
@@ -419,7 +445,7 @@ const Game = {
else if (monster.statusEffect.slug === 'recover') {
const statusEffectHeal = Math.floor(monster.stats.hp / 16);
- Game.addPhaseEvent(Game.phases.postAction, monster, () => {
+ Game.addBattlePhaseEvent('postAction', monster, () => {
monster.hp += statusEffectHeal;
if (Game.doBattleAnimation) {
@@ -436,7 +462,7 @@ const Game = {
else if (monster.statusEffect.slug === 'stuck') {
for (const technique of monster.activeTechniques) {
if ([TechniqueRange.melee, TechniqueRange.touch].includes(technique.range)) {
- Game.addPhaseEvent(Game.phases.preAction, monster, () => {
+ Game.addBattlePhaseEvent('preAction', monster, () => {
technique.potency = technique.stats.potency * 0.5;
technique.power = technique.stats.power * 0.5;
@@ -454,7 +480,7 @@ const Game = {
else if (monster.statusEffect.slug === 'grabbed') {
for (const technique of monster.activeTechniques) {
if ([TechniqueRange.ranged, TechniqueRange.reach].includes(technique.range)) {
- Game.addPhaseEvent(Game.phases.preAction, monster, () => {
+ Game.addBattlePhaseEvent('preAction', monster, () => {
technique.potency = technique.stats.potency * 0.5;
technique.power = technique.stats.power * 0.5;
@@ -472,7 +498,7 @@ const Game = {
else if (monster.statusEffect.slug === 'charging') {
const nextStatusEffect = await fetchStatusEffect('chargedup');
- Game.addPhaseEvent(Game.phases.preAction, monster, () => {
+ Game.addBattlePhaseEvent('preAction', monster, () => {
logStatusIs();
});
@@ -489,7 +515,7 @@ const Game = {
const statChange = monster.statusEffect.stats[statType];
const modifiedValue = Math.floor(eval(`${monster.stats[statType]} ${statChange.operation} ${statChange.value}`));
- Game.addPhaseEvent(Game.phases.preAction, monster, () => {
+ Game.addBattlePhaseEvent('preAction', monster, () => {
monster.setStatModifier(statType, modifiedValue);
logStatusIs();
@@ -501,7 +527,7 @@ const Game = {
};
}
- Game.addPhaseEvent(Game.phases.postAction, monster, () => {
+ Game.addBattlePhaseEvent('postAction', monster, () => {
monster.statusEffect.turnsLeft--;
});
},
@@ -682,6 +708,9 @@ const Game = {
async jumpToArea (area) {
Game.clearCurrentTurn();
+ if (Memory.state.currentArea) {
+ Memory.state.areaProgress[Memory.state.currentArea.slug] = Memory.state.currentArea;
+ }
Memory.state.currentArea = area;
UI.drawArea(area);
@@ -741,6 +770,7 @@ const Game = {
if (itemEffect.type === 'heal') {
monster.hp += itemEffect.amount;
item.quantity--;
+ UI.drawActiveMonster();
}
else if (itemEffect.type === 'capture') {