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