mirror of
https://github.com/cliffe/BreakEscape.git
synced 2026-02-20 13:50:46 +00:00
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:
@@ -2672,8 +2672,14 @@ function createNPCSpritesForRoom(roomId, roomData) {
|
||||
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);
|
||||
|
||||
@@ -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'));
|
||||
// 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
|
||||
}
|
||||
|
||||
if (isTable) {
|
||||
game.physics.add.collider(npcSprite, obj);
|
||||
tablesAdded++;
|
||||
}
|
||||
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'}`);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user