From 780fbb5e13b592e80b4396faac07927ffe3b998d Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 20 Nov 2025 01:31:40 +0000 Subject: [PATCH] fix: Ensure NPC-table collisions are properly initialized and maintained Fixed a timing issue where NPC-table collisions weren't being created because table physics bodies were initialized asynchronously (delayedCall) while NPC collision setup ran immediately. Changes: 1. Added 10ms delay to NPC environment collision setup to ensure table/chair physics bodies are fully initialized first (rooms.js:2678) 2. Improved table detection logic in setupNPCTableCollisions (npc-sprites.js): - Removed dependency on obj.body.static flag which may not be set yet - Now checks scenarioData.type === 'table' first (most reliable) - Also checks for 'desk' or 'table' in object name as fallback - Added detailed logging for each table collision created This ensures NPCs cannot walk through tables and maintains proper collision detection throughout the game. --- js/core/rooms.js | 12 +++++++++--- js/systems/npc-sprites.js | 25 ++++++++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/js/core/rooms.js b/js/core/rooms.js index 246bf26..b33ec0b 100644 --- a/js/core/rooms.js +++ b/js/core/rooms.js @@ -2671,9 +2671,15 @@ function createNPCSpritesForRoom(roomId, roomData) { if (window.player) { NPCSpriteManager.createNPCCollision(gameRef, sprite, window.player); } - - // Set up wall and chair collisions (same as player gets) - NPCSpriteManager.setupNPCEnvironmentCollisions(gameRef, sprite, roomId); + + // Set up environment collisions (walls, tables, chairs) after a delay + // This ensures table/chair physics bodies are fully initialized first + // (tables use delayedCall for physics body creation) + gameRef.time.delayedCall(10, () => { + if (sprite && sprite.body && !sprite.destroyed) { + NPCSpriteManager.setupNPCEnvironmentCollisions(gameRef, sprite, roomId); + } + }); // Set up NPC-to-NPC collisions with all other NPCs in this room NPCSpriteManager.setupNPCToNPCCollisions(gameRef, sprite, roomId, roomData.npcSprites); diff --git a/js/systems/npc-sprites.js b/js/systems/npc-sprites.js index a31c58d..257fca8 100644 --- a/js/systems/npc-sprites.js +++ b/js/systems/npc-sprites.js @@ -526,17 +526,20 @@ export function setupNPCTableCollisions(scene, npcSprite, roomId) { // Collision with all table objects in the room Object.values(room.objects).forEach(obj => { - // Tables are identified by their object name or by checking if they're static bodies - // Look for objects that came from the 'table' type in processObject - if (obj && obj.body && obj.body.static) { - // Check if this looks like a table (has scenarioData.type === 'table' or name includes 'desk') - const isTable = (obj.scenarioData && obj.scenarioData.type === 'table') || - (obj.name && obj.name.toLowerCase().includes('desk')); - - if (isTable) { - game.physics.add.collider(npcSprite, obj); - tablesAdded++; - } + // Tables are identified primarily by scenarioData.type === 'table' + // Also check for objects with 'desk' or 'table' in their name as fallback + if (!obj || !obj.body) { + return; // Skip objects without physics bodies + } + + const isTable = (obj.scenarioData && obj.scenarioData.type === 'table') || + (obj.name && (obj.name.toLowerCase().includes('desk') || + obj.name.toLowerCase().includes('table'))); + + if (isTable) { + game.physics.add.collider(npcSprite, obj); + tablesAdded++; + console.log(` Added table collision: ${npcSprite.npcId} <-> ${obj.name || 'table'}`); } });