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.
This commit is contained in:
Claude
2025-11-20 01:31:40 +00:00
parent 642fc4c25b
commit 780fbb5e13
2 changed files with 23 additions and 14 deletions

View File

@@ -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);

View File

@@ -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'}`);
}
});