summaryrefslogtreecommitdiff
path: root/resources/js/game.js
diff options
context:
space:
mode:
Diffstat (limited to 'resources/js/game.js')
-rw-r--r--resources/js/game.js131
1 files changed, 120 insertions, 11 deletions
diff --git a/resources/js/game.js b/resources/js/game.js
index 1a91f06..b233ef2 100644
--- a/resources/js/game.js
+++ b/resources/js/game.js
@@ -5,6 +5,7 @@ const Game = {
postAction: [],
},
+ isLoadingArea: false,
isInBattle: false,
didTechniqueHit: false,
@@ -49,7 +50,12 @@ const Game = {
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));
+ Memory.state.currentArea.trainerProgress++;
+ if (Memory.state.currentArea.encounters.length > 0) {
+ await Game.encounterWildMonster();
+ } else {
+ await Game.progressToNextArea();
+ }
} else {
await Game.encounterNextTrainerMonster();
}
@@ -312,12 +318,16 @@ const Game = {
* @param {MouseEvent} event
*/
async battleClick (event) {
+ if (Game.isLoadingArea) {
+ return;
+ }
+
Game.isInBattle = true;
UI.battleClickEvent = event;
await Game.tryUseTechnique(Memory.state.activeTechnique, Memory.state.player.activeMonster, Memory.state.opponent.activeMonster);
- Game.progressTurn();
+ await Game.progressTurn();
},
/**
@@ -349,24 +359,94 @@ const Game = {
},
/**
+ * @param {InventoryItem} item
+ * @param {Monster} monster
+ *
+ * @returns {boolean}
+ */
+ canUseItem (item, monster = null) {
+ let isApplicable = true;
+
+ for (const itemConditionCode of item.conditions) {
+ const itemCondition = new ItemCondition(itemConditionCode);
+ let conditionIsApplicable = true;
+
+ if (itemCondition.what === 'current_hp') {
+ const value = parseInt(itemCondition.value) * monster.stats.hp;
+ conditionIsApplicable = eval(`${monster.hp} ${itemCondition.comparator} ${value}`);
+ }
+
+ else if (itemCondition.what === 'wild_monster') {
+ conditionIsApplicable = Game.isBattleType('monster');
+ }
+
+ else if (itemCondition.what === 'threat') {
+ conditionIsApplicable = Memory.state.opponent.activeMonster.category === 'threat';
+ }
+
+ else {
+ conditionIsApplicable = false;
+ }
+
+ if (itemCondition.is === 'not') {
+ conditionIsApplicable = !conditionIsApplicable;
+ }
+
+ isApplicable = isApplicable && conditionIsApplicable;
+ }
+
+ return isApplicable;
+ },
+
+ /**
* @param {InventoryItem}
+ * @param {Monster}
*/
- async useItem (item) {
+ async useItem (item, monster) {
for (const itemEffectCode of item.effects) {
const itemEffect = new ItemEffect(itemEffectCode);
- if (itemEffect.type === 'evolve') {
+ if (itemEffect.type === 'heal') {
+ monster.hp += itemEffect.amount;
+ item.quantity--;
+ }
+
+ else if (itemEffect.type === 'capture') {
+ Memory.state.activeBall = item;
+ UI.drawActiveBall();
+ }
+
+ else 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();
+
+ item.quantity--;
}
}
}
},
/**
+ * @param {Array} inventory
+ * @param {InventoryItem} item
+ */
+ removeItemFromInventory (inventory, item) {
+ inventory.splice(inventory.indexOf(item), 1);
+ },
+
+ /**
+ * @param {string} type
+ *
+ * @returns {boolean}
+ */
+ isBattleType (type) {
+ return Memory.state.opponent.type === type;
+ },
+
+ /**
* @param {Monster} monster
*/
async evolveMonster (monster) {
@@ -374,14 +454,29 @@ const Game = {
monster.evolve(monster.evolutions[0]);
},
+ /**
+ * @returns {boolean}
+ */
+ canCatchMonster () {
+ return Game.isBattleType('monster') && Memory.state.activeBall;
+ },
+
async catchMonster () {
- if (Memory.state.opponent.type === 'trainer') {
+ if (!Game.canCatchMonster()) {
return;
}
+ Memory.state.activeBall.quantity--;
+ if (Memory.state.activeBall.quantity === 0) {
+ Game.removeItemFromInventory(Memory.state.player.inventory, Memory.state.activeBall);
+ Memory.state.activeBall = null;
+ UI.drawActiveBall();
+ }
+
const caughtMonster = new Monster(Memory.state.opponent.activeMonster.slug);
caughtMonster.initialize();
caughtMonster.level = Memory.state.opponent.activeMonster.level;
+ caughtMonster.hp = Memory.state.opponent.activeMonster.hp;
Memory.state.player.monsters.push(caughtMonster);
@@ -401,17 +496,29 @@ const Game = {
/**
* @param {Area} area
*/
- async progressToArea (area) {
+ async jumpToArea (area) {
Memory.state.currentArea = area;
UI.drawArea(area);
- UI.drawStatus();
+ },
+
+ async progressToNextArea () {
+ Game.isLoadingArea = true;
- if (area.encounters.length > 0) {
+ const currentArea = Memory.state.currentArea;
+ const nextArea = await fetchArea(currentArea.nextArea);
+
+ await Game.jumpToArea(nextArea);
+
+ if (nextArea.encounters.length > 0) {
await Game.encounterWildMonster();
} else {
await Game.encounterTrainer();
}
+
+ UI.drawStatus();
+
+ Game.isLoadingArea = false;
},
async encounterWildMonster () {
@@ -421,7 +528,7 @@ const Game = {
const monster = await fetchMonster(randomMonster.monster);
monster.level = randomLevel;
- const wildMonster = new Trainer([monster]);
+ const wildMonster = new Trainer({ monsters: [monster] });
wildMonster.type = 'monster';
await wildMonster.initialize();
Memory.state.opponent = wildMonster;
@@ -432,7 +539,7 @@ const Game = {
async encounterTrainer () {
const nextTrainer = Memory.state.currentArea.trainers[Memory.state.currentArea.trainerProgress];
- const trainer = new Trainer(nextTrainer.monsters, nextTrainer.inventory);
+ const trainer = new Trainer(nextTrainer);
if (nextTrainer.name === 'Rival') {
trainer.monsters.push(Memory.state.rivalMonster);
}
@@ -440,6 +547,7 @@ const Game = {
Memory.state.opponent = trainer;
UI.drawOpponentMonster();
+ UI.drawStatus();
},
async encounterNextTrainerMonster () {
@@ -451,7 +559,8 @@ const Game = {
};
// Game click bindings
-UI.elements.status.querySelector('[data-template-slot="next-trainer"]').addEventListener('click', Game.encounterTrainer);
+UI.elements.nextTrainer.addEventListener('click', Game.encounterTrainer);
+UI.elements.nextArea.addEventListener('click', Game.progressToNextArea);
UI.elements.battleOpponent.addEventListener('click', Game.battleClick);
UI.elements.techniques.addEventListener('click', Game.techniqueClick);
UI.elements.menuCatch.addEventListener('click', Game.catchMonster);