From 499ac6d72295f40b0befafca2bc8a0f0a587b313 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 9 Dec 2025 00:06:05 +0000 Subject: [PATCH] Fix door and room loading issues with multiple paths Fixes two critical issues when multiple doors lead to the same room: 1. Room reloading: Added checks in loadRoom() and createRoom() to prevent reloading/recreating rooms that are already loaded. This prevents the room from being unnecessarily recreated when opening a second door to the same room. 2. Door location detection: Updated removeMatchingDoorSprite() to use position and direction when finding the matching door. Previously it only checked connectedRoom, which caused it to remove the wrong door when multiple doors connected the same two rooms at different locations. Now it checks: - Opposite direction matches - Position matches within tolerance (X for N/S doors, Y for E/W doors) This ensures that opening different doors to the same room works correctly without causing room reloads or removing the wrong door sprites. --- public/break_escape/js/core/rooms.js | 14 +++++++- public/break_escape/js/systems/doors.js | 46 ++++++++++++++++++++----- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/public/break_escape/js/core/rooms.js b/public/break_escape/js/core/rooms.js index 64ee4f5..f4a7b97 100644 --- a/public/break_escape/js/core/rooms.js +++ b/public/break_escape/js/core/rooms.js @@ -614,6 +614,12 @@ async function loadRoom(roomId) { return; } + // Check if room is already loaded - prevent reloading + if (window.rooms && window.rooms[roomId]) { + console.log(`Room ${roomId} is already loaded, skipping reload`); + return; + } + let roomData; // Check if roomData is cached (from unlock API response) @@ -1609,9 +1615,15 @@ export function calculateRoomPositions(gameInstance) { export function createRoom(roomId, roomData, position) { try { + // Check if room already exists - prevent recreating + if (rooms[roomId]) { + console.log(`Room ${roomId} already exists, skipping recreation`); + return; + } + console.log(`Creating room ${roomId} of type ${roomData.type}`); const gameScenario = window.gameScenario; - + // Build a set of item types that are in startItemsInInventory // These should NOT be created as sprites in rooms const startInventoryTypes = new Set(); diff --git a/public/break_escape/js/systems/doors.js b/public/break_escape/js/systems/doors.js index 0b10fca..8fbd107 100644 --- a/public/break_escape/js/systems/doors.js +++ b/public/break_escape/js/systems/doors.js @@ -718,35 +718,65 @@ function openDoor(doorSprite) { // Function to remove the matching door sprite from the connected room function removeMatchingDoorSprite(roomId, fromRoomId, direction, doorWorldX, doorWorldY) { - console.log(`Removing matching door sprite in room ${roomId} for door from ${fromRoomId} (${direction})`); - + console.log(`Removing matching door sprite in room ${roomId} for door from ${fromRoomId} (${direction}) at (${doorWorldX}, ${doorWorldY})`); + // Use window.rooms to ensure we see the latest state const room = window.rooms ? window.rooms[roomId] : null; if (!room || !room.doorSprites) { console.log(`No door sprites found for room ${roomId}`); return; } - + + // Calculate the opposite direction to find the matching door + const oppositeDirection = getOppositeDirection(direction); + + // Position tolerance for matching doors (in pixels) + const POSITION_TOLERANCE = TILE_SIZE; + // Find the door sprite that connects to the fromRoomId + // For multiple doors between same rooms, also check position and direction const matchingDoorSprite = room.doorSprites.find(doorSprite => { const props = doorSprite.doorProperties; - return props && props.connectedRoom === fromRoomId; + if (!props || props.connectedRoom !== fromRoomId) { + return false; + } + + // Check if direction matches (opposite direction) + if (props.direction !== oppositeDirection) { + return false; + } + + // For N/S doors, check X position matches (within tolerance) + // For E/W doors, check Y position matches (within tolerance) + if (direction === 'north' || direction === 'south') { + const xDiff = Math.abs(props.worldX - doorWorldX); + if (xDiff > POSITION_TOLERANCE) { + return false; + } + } else if (direction === 'east' || direction === 'west') { + const yDiff = Math.abs(props.worldY - doorWorldY); + if (yDiff > POSITION_TOLERANCE) { + return false; + } + } + + return true; }); - + if (matchingDoorSprite) { - console.log(`Found matching door sprite in room ${roomId}, removing it`); + console.log(`Found matching door sprite in room ${roomId} at (${matchingDoorSprite.x}, ${matchingDoorSprite.y}), removing it`); matchingDoorSprite.destroy(); if (matchingDoorSprite.interactionZone) { matchingDoorSprite.interactionZone.destroy(); } - + // Remove from the doorSprites array const index = room.doorSprites.indexOf(matchingDoorSprite); if (index > -1) { room.doorSprites.splice(index, 1); } } else { - console.log(`No matching door sprite found in room ${roomId}`); + console.log(`No matching door sprite found in room ${roomId} for direction ${oppositeDirection} at position (${doorWorldX}, ${doorWorldY})`); } }