mirror of
https://github.com/cliffe/BreakEscape.git
synced 2026-02-20 13:50:46 +00:00
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.
This commit is contained in:
153
index.html
153
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
|
||||
};
|
||||
}
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
Reference in New Issue
Block a user