Skip to content

Commit f4f8499

Browse files
committed
updated NPC logic
1 parent 1dfcf28 commit f4f8499

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

index.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ const player = {
306306
speed: 4
307307
};
308308

309+
const SPAWN_POINT = { x: 400, y: 325 };
310+
311+
309312
const keys = {};
310313

311314
const objects = [
@@ -467,10 +470,13 @@ const npcEnterBtn = document.getElementById('npc-enter-btn');
467470
const npcCancelBtn = document.getElementById('npc-cancel-btn');
468471
let npcModalOpen = false;
469472
let npcDialogueIndex = 0;
470-
473+
let npcCooldown = false;
474+
let lastNPCInteractionTime = 0;
471475
let typewriterTimeout = null;
472476
let isTyping = false;
473477

478+
479+
474480
function typeDialogue(text, callback) {
475481
npcDialogue.textContent = "";
476482
let i = 0;
@@ -490,6 +496,23 @@ function typeDialogue(text, callback) {
490496
}
491497

492498
function showNPCModal(worldKey) {
499+
const now = Date.now();
500+
501+
// If cooldown active or interacting too soon, exit
502+
if (npcCooldown || now - lastNPCInteractionTime < 3000) {
503+
console.log("Please wait before talking to another NPC...");
504+
alert("Give the NPCs a moment before chatting again!");
505+
return;
506+
}
507+
508+
509+
510+
npcCooldown = true;
511+
lastNPCInteractionTime = now;
512+
setTimeout(() => {
513+
npcCooldown = false;
514+
}, 3000);
515+
493516
pendingWorld = worldKey;
494517
npcMessage.textContent = worldNPCs[worldKey].message;
495518
npcDialogue.textContent = "";
@@ -526,6 +549,7 @@ npcEnterBtn.onclick = function() {
526549
}
527550
};
528551

552+
529553
npcCancelBtn.onclick = function() {
530554
npcModal.style.display = 'none';
531555
npcModalOpen = false;
@@ -534,6 +558,20 @@ npcCancelBtn.onclick = function() {
534558
npcDialogueIndex = 0;
535559
};
536560

561+
npcCancelBtn.addEventListener('click', () => {
562+
npcModal.style.display = 'none';
563+
npcModalOpen = false;
564+
pendingWorld = null;
565+
npcDialogueIndex = 0;
566+
clearTimeout(typewriterTimeout);
567+
isTyping = false;
568+
569+
// Move player back to spawn point
570+
player.x = SPAWN_POINT.x;
571+
player.y = SPAWN_POINT.y;
572+
});
573+
574+
537575
// Prevent player from overlapping with world object
538576
function resolveTouch(player, obj) {
539577
// Simple axis-aligned separation

0 commit comments

Comments
 (0)