From bd19b9de6ba69dfbd9c227b84a5f9bba5cf25d22 Mon Sep 17 00:00:00 2001 From: Damian-I Date: Thu, 19 Dec 2024 18:57:26 +0000 Subject: [PATCH 1/2] Refactor object interaction handling and container content collection - Simplified the handleObjectInteraction function by removing redundant checks and improving readability. - Enhanced the logic for handling container contents, ensuring items are only collected if the container is unlocked and not previously collected. - Introduced a new function, collectContainerContents, to streamline the process of collecting items from unlocked containers. - Updated the state management for locked items and their contents to improve gameplay flow. --- index.html | 153 +++++++++++++++++++++++++++++------------------------ 1 file changed, 83 insertions(+), 70 deletions(-) diff --git a/index.html b/index.html index 2af6d24..0a58bf0 100644 --- a/index.html +++ b/index.html @@ -1213,65 +1213,63 @@ // handles interactions with objects // displays the object's data in an alert function handleObjectInteraction(sprite) { - if (!sprite || !sprite.scenarioData) { - console.warn('Invalid sprite or missing scenario data'); - return; + if (!sprite || !sprite.scenarioData) { + console.warn('Invalid sprite or missing scenario data'); + return; + } + + // Skip range check for inventory items + const isInventoryItem = inventory.items.includes(sprite); + if (!isInventoryItem) { + // Check if player is in range + const dx = player.x - sprite.x; + const dy = player.y - sprite.y; + const distanceSq = dx * dx + dy * dy; + + if (distanceSq > INTERACTION_RANGE_SQ) { + return; + } + } + + const data = sprite.scenarioData; + let message = `${data.name}\n\n`; + message += `Observations: ${data.observations}\n\n`; + + // Check for locked state in scenarioData + if (data.locked === true) { + console.log('Item is locked:', data); + handleUnlock(sprite, 'item'); + return; + } + + if (data.readable && data.text) { + message += `Text: ${data.text}\n\n`; + } + + // Only handle contents if they exist and haven't been collected yet + if (data.contents && data.contents.length > 0) { + message += `You found the following items:\n`; + data.contents.forEach(item => { + message += `- ${item.name}\n`; + }); + alert(message); + + // Add all contents to inventory + data.contents.forEach(item => { + const contentSprite = createInventorySprite({ + ...item, + type: item.type.toLowerCase() + }); + if (contentSprite) { + addToInventory(contentSprite); } - - // Skip range check for inventory items - const isInventoryItem = inventory.items.includes(sprite); - if (!isInventoryItem) { - // Check if player is in range - const dx = player.x - sprite.x; - const dy = player.y - sprite.y; - const distanceSq = dx * dx + dy * dy; - - if (distanceSq > INTERACTION_RANGE_SQ) { - // alert("Too far away to interact with this object."); - return; - } - } - - const data = sprite.scenarioData; - let message = `${data.name}\n\n`; - message += `Observations: ${data.observations}\n\n`; - - // Check for locked state in scenarioData - if (data.locked === true) { - console.log('Item is locked:', data); - handleUnlock(sprite, 'item'); - return; - } - - if (data.readable && data.text) { - message += `Text: ${data.text}\n\n`; - } - - if (data.contents && !data.locked) { - // Handle container contents - message += `You found the following items:\n`; - data.contents.forEach(item => { - message += `- ${item.name}\n`; - }); - alert(message); - - // Add all contents to inventory - data.contents.forEach(item => { - // Ensure the item type matches the preloaded asset name - const contentSprite = createInventorySprite({ - ...item, - type: item.type.toLowerCase() // Ensure type matches asset name - }); - if (contentSprite) { - addToInventory(contentSprite); - } - }); - - // Clear contents after adding to inventory - data.contents = []; - return; - } - + }); + + // Clear contents after adding to inventory + data.contents = []; + return; + } + if (data.takeable) { message += `This item can be taken\n\n`; @@ -1675,22 +1673,36 @@ // Handle item unlocking if (lockable.scenarioData) { lockable.scenarioData.locked = false; + // Set new state for containers with contents + if (lockable.scenarioData.contents) { + lockable.scenarioData.isUnlockedButNotCollected = true; + } } else { lockable.locked = false; - } - - // If the item has contents, make them accessible - if (lockable.scenarioData?.contents) { - lockable.scenarioData.contents.forEach(item => { - const sprite = createInventorySprite(item); - if (sprite) { - addToInventory(sprite); - } - }); + if (lockable.contents) { + lockable.isUnlockedButNotCollected = true; + } } } } + function collectContainerContents(container) { + if (!container.scenarioData?.contents || + !container.scenarioData?.isUnlockedButNotCollected) { + return; + } + + container.scenarioData.contents.forEach(item => { + const sprite = createInventorySprite(item); + if (sprite) { + addToInventory(sprite); + } + }); + + container.scenarioData.isUnlockedButNotCollected = false; + alert('You collected the items from the container.'); + } + // Helper function to create inventory sprites for unlocked container contents function createInventorySprite(itemData) { const scene = game.scene.scenes[0]; // Get the main scene @@ -1862,10 +1874,11 @@ function getLockRequirementsForItem(item) { return { lockType: item.lockType || item.scenarioData?.lockType, - requires: item.requires || item.scenarioData?.requires + requires: item.requires || item.scenarioData?.requires, + isUnlockedButNotCollected: false }; } - + \ No newline at end of file From 9d449b88464fd70fc784421d12b44f2baef48656 Mon Sep 17 00:00:00 2001 From: Damian Idzinski <45018439+Damian-I@users.noreply.github.com> Date: Thu, 30 Jan 2025 11:36:00 +0000 Subject: [PATCH 2/2] Corrected whitespace --- index.html | 102 ++++++++++++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/index.html b/index.html index 0a58bf0..7d27232 100644 --- a/index.html +++ b/index.html @@ -1213,62 +1213,62 @@ // handles interactions with objects // displays the object's data in an alert function handleObjectInteraction(sprite) { - if (!sprite || !sprite.scenarioData) { - console.warn('Invalid sprite or missing scenario data'); - return; - } - - // Skip range check for inventory items - const isInventoryItem = inventory.items.includes(sprite); - if (!isInventoryItem) { - // Check if player is in range - const dx = player.x - sprite.x; - const dy = player.y - sprite.y; - const distanceSq = dx * dx + dy * dy; - - if (distanceSq > INTERACTION_RANGE_SQ) { + if (!sprite || !sprite.scenarioData) { + console.warn('Invalid sprite or missing scenario data'); return; } - } - - const data = sprite.scenarioData; - let message = `${data.name}\n\n`; - message += `Observations: ${data.observations}\n\n`; - - // Check for locked state in scenarioData - if (data.locked === true) { - console.log('Item is locked:', data); - handleUnlock(sprite, 'item'); - return; - } - - if (data.readable && data.text) { - message += `Text: ${data.text}\n\n`; - } - - // Only handle contents if they exist and haven't been collected yet - if (data.contents && data.contents.length > 0) { - message += `You found the following items:\n`; - data.contents.forEach(item => { - message += `- ${item.name}\n`; - }); - alert(message); - // Add all contents to inventory - data.contents.forEach(item => { - const contentSprite = createInventorySprite({ - ...item, - type: item.type.toLowerCase() - }); - if (contentSprite) { - addToInventory(contentSprite); + // Skip range check for inventory items + const isInventoryItem = inventory.items.includes(sprite); + if (!isInventoryItem) { + // Check if player is in range + const dx = player.x - sprite.x; + const dy = player.y - sprite.y; + const distanceSq = dx * dx + dy * dy; + + if (distanceSq > INTERACTION_RANGE_SQ) { + return; } - }); + } - // Clear contents after adding to inventory - data.contents = []; - return; - } + const data = sprite.scenarioData; + let message = `${data.name}\n\n`; + message += `Observations: ${data.observations}\n\n`; + + // Check for locked state in scenarioData + if (data.locked === true) { + console.log('Item is locked:', data); + handleUnlock(sprite, 'item'); + return; + } + + if (data.readable && data.text) { + message += `Text: ${data.text}\n\n`; + } + + // Only handle contents if they exist and haven't been collected yet + if (data.contents && data.contents.length > 0) { + message += `You found the following items:\n`; + data.contents.forEach(item => { + message += `- ${item.name}\n`; + }); + alert(message); + + // Add all contents to inventory + data.contents.forEach(item => { + const contentSprite = createInventorySprite({ + ...item, + type: item.type.toLowerCase() + }); + if (contentSprite) { + addToInventory(contentSprite); + } + }); + + // Clear contents after adding to inventory + data.contents = []; + return; + } if (data.takeable) { message += `This item can be taken\n\n`;