Enhance NPC collision detection: add checks for table objects in NPC behavior and log successful collisions for better debugging.

This commit is contained in:
Z. Cliffe Schreuders
2025-11-23 10:39:44 +00:00
parent 59f096fdba
commit 5a997e38f7
2 changed files with 34 additions and 13 deletions

View File

@@ -1134,22 +1134,42 @@ class NPCBehavior {
}
const room = window.rooms ? window.rooms[this.roomId] : null;
if (!room || !room.wallCollisionBoxes) {
return; // No walls to check
if (!room) {
return; // No room reference
}
// Check if NPC is overlapping with any wall collision box
// Check if NPC is overlapping with any wall collision box or table
let isOverlappingWall = false;
let overlappingWall = null;
for (const wallBox of room.wallCollisionBoxes) {
if (!wallBox.body) continue;
// Check walls
if (room.wallCollisionBoxes) {
for (const wallBox of room.wallCollisionBoxes) {
if (!wallBox.body) continue;
// Check if NPC body overlaps with wall using scene physics
if (this.scene.physics.overlap(this.sprite, wallBox)) {
isOverlappingWall = true;
overlappingWall = wallBox;
break;
// Check if NPC body overlaps with wall using scene physics
if (this.scene.physics.overlap(this.sprite, wallBox)) {
isOverlappingWall = true;
overlappingWall = wallBox;
break;
}
}
}
// Check tables (if not already stuck in a wall)
if (!isOverlappingWall && room.objects) {
for (const obj of Object.values(room.objects)) {
if (!obj || !obj.body) continue;
// Check if this is 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 && this.scene.physics.overlap(this.sprite, obj)) {
isOverlappingWall = true;
overlappingWall = obj;
break;
}
}
}

View File

@@ -526,9 +526,9 @@ 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) {
// Tables are identified by scenarioData.type === 'table' or name includes 'desk'
// Tables are static collision objects, so they should have a physics body
if (obj && obj.body) {
// 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'));
@@ -536,6 +536,7 @@ export function setupNPCTableCollisions(scene, npcSprite, roomId) {
if (isTable) {
game.physics.add.collider(npcSprite, obj);
tablesAdded++;
console.log(`✅ Added NPC collision with table: ${obj.name}`);
}
}
});