From 32b19aa9504ce24fe0e98a5c9e2fa2be9612fa88 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sat, 2 Sep 2023 22:55:55 +0200 Subject: item drops! --- resources/js/game.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 7 deletions(-) (limited to 'resources/js/game.js') diff --git a/resources/js/game.js b/resources/js/game.js index 309d013..c2b20da 100644 --- a/resources/js/game.js +++ b/resources/js/game.js @@ -101,6 +101,32 @@ const Game = { Game.log(translate('game:battle:money:get').replace('{amount}', money)); }); + // item + if (Memory.state.currentArea.items.length > 0) { + const itemDropChance = Memory.state.currentArea.monsterProgress * 0.1; + const itemDropCheck = Math.random() * 100; + if (itemDropChance > itemDropCheck) { + const itemDropRatioMax = Memory.state.currentArea.items.reduce((accumulator, itemData) => accumulator + itemData.dropRatio, 0); + const itemDropRatioCheck = Math.random() * itemDropRatioMax; + let accumulator = 0; + for (const itemData of Memory.state.currentArea.items) { + const lowerBounds = accumulator; + accumulator += itemData.dropRatio; + const upperBounds = accumulator; + if (itemDropRatioCheck >= lowerBounds && itemDropRatioCheck <= upperBounds) { + await Game.addItemToInventory(Memory.state.player.inventory, itemData.slug, 1); + + const droppedItem = await fetchItem(itemData.slug); + Game.addPhaseEvent('postTurn', () => { + Game.log(translate('game:battle:item:get').replace('{name}', droppedItem.name)); + }); + + break; + } + } + } + } + // exp Memory.state.player.activeMonster.exp += calculateAwardedExperience(Memory.state.opponent.activeMonster, [Memory.state.player.activeMonster])[0]; @@ -114,12 +140,16 @@ const Game = { Memory.state.Game.isInBattle = false; if (Memory.state.opponent.type === 'monster') { - Memory.state.currentArea.monsterProgress++; + if ( + Memory.state.currentArea.monsterProgress < Memory.state.currentArea.requiredEncounters || + Memory.state.currentArea.isCompleted + ) { + 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]) { Memory.state.currentArea.trainerProgress++; - Memory.state.currentArea.monsterProgress = 0; if (Memory.state.currentArea.encounters.length > 0) { await Game.encounterWildMonster(); } else if (Memory.state.currentArea.trainers.length > 0) { @@ -783,6 +813,31 @@ const Game = { Game.isLoadingArea = true; Game.clearCurrentTurn(); + + // on leave + let onLeaveStoryIsDone = true; + if (Memory.state.currentArea.events?.onLeave?.length > 0) { + Game.isLoadingArea = false; + + for (const event of Memory.state.currentArea.events.onLeave) { + if (event.type === 'story') { + UI.showMap(); + UI.drawTown(); + + onLeaveStoryIsDone = await Story.progress(event.story); + } + } + } + + if (!onLeaveStoryIsDone) { + return; + } + + Game.isLoadingArea = true; + + + // after on leave events resolved + if (Memory.state.currentArea) { Memory.state.areaProgress[Memory.state.currentArea.slug] = Memory.state.currentArea; } @@ -791,8 +846,8 @@ const Game = { // on enter - let storyIsDone = true; - if (Memory.state.currentArea.events?.onEnter.length > 0) { + let onEnterStoryIsDone = true; + if (Memory.state.currentArea.events?.onEnter?.length > 0) { Game.isLoadingArea = false; for (const event of Memory.state.currentArea.events.onEnter) { @@ -800,19 +855,19 @@ const Game = { UI.showMap(); UI.drawTown(); - storyIsDone = await Story.progress(event.story); + onEnterStoryIsDone = await Story.progress(event.story); } } } - if (!storyIsDone) { + if (!onEnterStoryIsDone) { return; } Game.isLoadingArea = true; - // after events resolved + // after on enter events resolved if (Game.isTown(Memory.state.currentArea)) { if (Object.values(Memory.state.currentArea.locations).some((location) => location.type === 'healingCenter')) { @@ -948,11 +1003,27 @@ const Game = { /** * @param {InventoryItem[]} inventory * @param {ItemSlug} itemSlug + * + * @returns {(InventoryItem|undefined)} */ getItemFromInventory (inventory, itemSlug) { return inventory.find((inventoryItem) => inventoryItem.slug === itemSlug); }, + /** + * @param {InventoryItem[]} inventory + * @param {ItemSlug} itemSlug + * @param {number} quantity + */ + async addItemToInventory (inventory, itemSlug, quantity = 1) { + const inventoryItem = Game.getItemFromInventory(inventory, itemSlug); + if (inventoryItem) { + inventoryItem.quantity += quantity; + } else { + inventory.push(new InventoryItem(await fetchItem(itemSlug), quantity)); + } + }, + /** * @param {InventoryItem[]} inventory * @param {ItemSlug} itemSlug -- cgit v1.2.3