@@ -56,53 +56,16 @@ class Npc extends Character {
5656 this . patrol ( ) ;
5757 }
5858 this . draw ( ) ;
59-
60- const nearPlayer = this . canInteractWithPlayer ( ) ;
61- if ( ! nearPlayer && this . isInteracting ) {
59+ // Check if player is still in collision - add null checks
60+ const players = this . gameEnv . gameObjects . filter (
61+ obj => obj && obj . state && obj . state . collisionEvents && obj . state . collisionEvents . includes ( this . spriteData . id )
62+ ) ;
63+ // Reset interaction state if player moved away
64+ if ( players . length === 0 && this . isInteracting ) {
6265 this . isInteracting = false ;
6366 }
6467 }
6568
66- findPlayer ( ) {
67- return this . gameEnv ?. gameObjects ?. find ( obj => obj && obj . constructor && obj . constructor . name === 'Player' ) ;
68- }
69-
70- canInteractWithPlayer ( interactDistance = 96 ) {
71- const player = this . findPlayer ( ) ;
72- if ( ! player ) return false ;
73- const distance = this . spriteData ?. interactDistance || interactDistance ;
74- return this . isNear ( player , distance ) ;
75- }
76-
77- isNearestNpcToPlayer ( ) {
78- const player = this . findPlayer ( ) ;
79- if ( ! player ) return false ;
80-
81- const myDist = Math . hypot (
82- ( this . position . x + this . width / 2 ) - ( player . position . x + player . width / 2 ) ,
83- ( this . position . y + this . height / 2 ) - ( player . position . y + player . height / 2 )
84- ) ;
85-
86- const allNpcs = this . gameEnv . gameObjects . filter (
87- obj => obj instanceof Npc && obj !== this && obj . canInteractWithPlayer ( )
88- ) ;
89-
90- // If no other NPCs are in range, we're automatically nearest
91- if ( allNpcs . length === 0 ) return true ;
92-
93- return allNpcs . every ( other => {
94- const otherDist = Math . hypot (
95- ( other . position . x + other . width / 2 ) - ( player . position . x + player . width / 2 ) ,
96- ( other . position . y + other . height / 2 ) - ( player . position . y + player . height / 2 )
97- ) ;
98- return myDist <= otherDist ;
99- } ) ;
100- }
101-
102- handleClick ( event ) {
103- this . handleKeyInteract ( ) ;
104- }
105-
10669 /**
10770 * General patrol movement within defined walking area (bouncing behavior)
10871 */
@@ -195,13 +158,12 @@ class Npc extends Character {
195158 obj => obj && obj . state && obj . state . collisionEvents && obj . state . collisionEvents . includes ( this . spriteData . id )
196159 ) ;
197160 const hasInteract = this . interact !== undefined ;
198- const nearPlayer = this . canInteractWithPlayer ( ) ;
199161
200162 // Only trigger interaction if:
201- // 1. Player is near this NPC (distance-based) or colliding with it
163+ // 1. Player is in collision with this NPC
202164 // 2. NPC has an interact function
203165 // 3. Not already interacting
204- if ( ( nearPlayer || players . length > 0 ) && hasInteract && ! this . isInteracting ) {
166+ if ( players . length > 0 && hasInteract && ! this . isInteracting ) {
205167 this . isInteracting = true ;
206168
207169 // Store a reference to this NPC's interact function
0 commit comments