Skip to content

Commit 94de74f

Browse files
authored
Merge pull request #884 from NithikaVivek/main
Revert Npc.js, initial friendly Npc
2 parents 9a9bb3d + 43211f4 commit 94de74f

File tree

3 files changed

+66
-52
lines changed

3 files changed

+66
-52
lines changed

assets/js/GameEnginev1.1/GameLevelCssePath.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Imports: Level objects and UI helpers.
22
import GamEnvBackground from './essentials/GameEnvBackground.js';
33
import Player from './essentials/Player.js';
4-
import Npc from './essentials/Npc.js';
54
import StatusPanel from './essentials/StatusPanel.js';
65
import FormPanel from './essentials/FormPanel.js';
76
import Picker from './essentials/Picker.js';
7+
import Npc from './essentials/Npc.js';
8+
import FriendlyNpc from './essentials/FriendlyNpc.js';
89
import DialogueSystem from './essentials/DialogueSystem.js';
910
import ProfileManager from '../pages/home-gamified/ProfileManager.js';
1011

@@ -163,7 +164,13 @@ class GameLevelCssePath {
163164
greeting,
164165
INIT_POSITION: { ...position },
165166
interactDistance: interactDistance || 120,
166-
...(reaction ? { reaction } : {}),
167+
reaction: function () {
168+
if (reaction) reaction.call(this);
169+
if (level?.showToast) {
170+
level.showToast("Press E to interact");
171+
}
172+
},
173+
167174
...(interact ? { interact } : {}),
168175
});
169176

@@ -1068,13 +1075,14 @@ class GameLevelCssePath {
10681075
*/
10691076

10701077
// Objects: Build level class list.
1078+
10711079
this.classes = [
10721080
{ class: GamEnvBackground, data: bg_data },
10731081
{ class: Player, data: player_data },
1074-
{ class: Npc, data: npc_data_startGatekeeper },
1075-
{ class: Npc, data: npc_data_identityGatekeeper },
1076-
{ class: Npc, data: npc_data_avatarGatekeeper },
1077-
{ class: Npc, data: npc_data_worldThemeGatekeeper },
1082+
{ class: FriendlyNpc, data: npc_data_startGatekeeper },
1083+
{ class: FriendlyNpc, data: npc_data_identityGatekeeper },
1084+
{ class: FriendlyNpc, data: npc_data_avatarGatekeeper },
1085+
{ class: FriendlyNpc, data: npc_data_worldThemeGatekeeper },
10781086
];
10791087
}
10801088
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Npc from "./Npc.js";
2+
3+
class FriendlyNpc extends Npc {
4+
constructor(data = {}, gameEnv = null) {
5+
super(data, gameEnv);
6+
7+
// NEW properties
8+
this.interactDistance = data.interactDistance || 120;
9+
this.hasAlerted = false;
10+
}
11+
12+
update() {
13+
super.update();
14+
15+
const player = this.gameEnv.gameObjects.find(obj => obj.constructor.name === "Player");
16+
if (!player) return;
17+
18+
const dx = player.position.x - this.position.x;
19+
const dy = player.position.y - this.position.y;
20+
const distance = Math.sqrt(dx * dx + dy * dy);
21+
22+
if (distance < this.interactDistance && !this.hasAlerted) {
23+
this.hasAlerted = true;
24+
25+
if (this.gameEnv?.currentLevel?.showToast) {
26+
this.gameEnv.currentLevel.showToast(
27+
this.spriteData?.greeting || "Hey there!"
28+
);
29+
}
30+
31+
// OPTIONAL: trigger reaction automatically
32+
if (this.reaction) {
33+
this.reaction();
34+
}
35+
}
36+
37+
// Reset when player walks away
38+
if (distance >= this.interactDistance) {
39+
this.hasAlerted = false;
40+
}
41+
}
42+
}
43+
44+
export default FriendlyNpc;

assets/js/GameEnginev1.1/essentials/Npc.js

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)