Merge pull request #12 from cliffe/10-no-auto-collecting-items-from-container

Refactor object interaction handling and container content collection (no longer auto collect items)
This commit is contained in:
Damian Idzinski
2025-01-30 11:47:13 +00:00
committed by GitHub

View File

@@ -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');
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);
// 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;
// Add all contents to inventory
data.contents.forEach(item => {
const contentSprite = createInventorySprite({
...item,
type: item.type.toLowerCase()
});
if (contentSprite) {
addToInventory(contentSprite);
}
}
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>