Spaces:
Running
Running
the NPC talk function doesnt really work , I get close to other npc and my character gets pushed
Browse files- index.html +43 -22
index.html
CHANGED
|
@@ -654,23 +654,16 @@
|
|
| 654 |
player.rotationSpeed
|
| 655 |
);
|
| 656 |
}
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
if (gameState.keys.space && !gameState.isJumping) {
|
| 660 |
gameState.isJumping = true;
|
| 661 |
setPlayerAnimation('jump');
|
| 662 |
setTimeout(() => {
|
| 663 |
gameState.isJumping = false;
|
| 664 |
updatePlayerAnimation();
|
| 665 |
}, 1000);
|
| 666 |
-
|
| 667 |
-
// Check if we're near an NPC to talk
|
| 668 |
-
if (gameState.nearbyNpc) {
|
| 669 |
-
showDialog(gameState.nearbyNpc);
|
| 670 |
-
}
|
| 671 |
}
|
| 672 |
-
|
| 673 |
-
// Update animation based on movement
|
| 674 |
if (moving !== player.isMoving || gameState.keys.shift) {
|
| 675 |
player.isMoving = moving;
|
| 676 |
updatePlayerAnimation();
|
|
@@ -694,7 +687,6 @@
|
|
| 694 |
setPlayerAnimation('walk');
|
| 695 |
}
|
| 696 |
}
|
| 697 |
-
|
| 698 |
function checkForNearbyNPCs() {
|
| 699 |
if (!gameState.player) return;
|
| 700 |
|
|
@@ -703,16 +695,36 @@
|
|
| 703 |
|
| 704 |
gameState.npcs.forEach(npc => {
|
| 705 |
const distance = npc.model.position.distanceTo(gameState.player.model.position);
|
| 706 |
-
if (distance <
|
| 707 |
closestDistance = distance;
|
| 708 |
closestNpc = npc;
|
| 709 |
}
|
| 710 |
});
|
| 711 |
|
| 712 |
gameState.nearbyNpc = closestNpc;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
}
|
| 714 |
-
|
| 715 |
-
function showDialog(npc) {
|
| 716 |
if (!npc) return;
|
| 717 |
|
| 718 |
// Get next dialog line
|
|
@@ -723,16 +735,25 @@
|
|
| 723 |
document.getElementById('dialog-character').style.backgroundColor = npc.character.color;
|
| 724 |
document.getElementById('dialog-name').textContent = npc.character.name;
|
| 725 |
document.getElementById('dialog-text').textContent = dialog;
|
| 726 |
-
|
| 727 |
// Show dialog box
|
| 728 |
document.getElementById('dialog-box').classList.remove('translate-y-full');
|
| 729 |
-
|
| 730 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 731 |
function closeDialog() {
|
| 732 |
document.getElementById('dialog-box').classList.add('translate-y-full');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 733 |
}
|
| 734 |
-
|
| 735 |
-
function handleKeyDown(event) {
|
| 736 |
switch (event.key.toLowerCase()) {
|
| 737 |
case 'w': gameState.keys.w = true; break;
|
| 738 |
case 'a': gameState.keys.a = true; break;
|
|
@@ -741,14 +762,14 @@
|
|
| 741 |
case 'shift': gameState.keys.shift = true; break;
|
| 742 |
case ' ':
|
| 743 |
gameState.keys.space = true;
|
| 744 |
-
if (gameState.
|
| 745 |
showDialog(gameState.nearbyNpc);
|
|
|
|
| 746 |
}
|
| 747 |
break;
|
| 748 |
}
|
| 749 |
}
|
| 750 |
-
|
| 751 |
-
function handleKeyUp(event) {
|
| 752 |
switch (event.key.toLowerCase()) {
|
| 753 |
case 'w': gameState.keys.w = false; break;
|
| 754 |
case 'a': gameState.keys.a = false; break;
|
|
|
|
| 654 |
player.rotationSpeed
|
| 655 |
);
|
| 656 |
}
|
| 657 |
+
// Jumping - only trigger when not showing dialog and not already jumping
|
| 658 |
+
if (gameState.keys.space && !gameState.isJumping && !document.getElementById('dialog-box').classList.contains('translate-y-full')) {
|
|
|
|
| 659 |
gameState.isJumping = true;
|
| 660 |
setPlayerAnimation('jump');
|
| 661 |
setTimeout(() => {
|
| 662 |
gameState.isJumping = false;
|
| 663 |
updatePlayerAnimation();
|
| 664 |
}, 1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 665 |
}
|
| 666 |
+
// Update animation based on movement
|
|
|
|
| 667 |
if (moving !== player.isMoving || gameState.keys.shift) {
|
| 668 |
player.isMoving = moving;
|
| 669 |
updatePlayerAnimation();
|
|
|
|
| 687 |
setPlayerAnimation('walk');
|
| 688 |
}
|
| 689 |
}
|
|
|
|
| 690 |
function checkForNearbyNPCs() {
|
| 691 |
if (!gameState.player) return;
|
| 692 |
|
|
|
|
| 695 |
|
| 696 |
gameState.npcs.forEach(npc => {
|
| 697 |
const distance = npc.model.position.distanceTo(gameState.player.model.position);
|
| 698 |
+
if (distance < 3 && distance < closestDistance) { // Reduced distance from 5 to 3
|
| 699 |
closestDistance = distance;
|
| 700 |
closestNpc = npc;
|
| 701 |
}
|
| 702 |
});
|
| 703 |
|
| 704 |
gameState.nearbyNpc = closestNpc;
|
| 705 |
+
|
| 706 |
+
// Add visual feedback when near NPC
|
| 707 |
+
const dialogBox = document.getElementById('dialog-box');
|
| 708 |
+
if (closestNpc && !dialogBox.classList.contains('translate-y-full')) {
|
| 709 |
+
// Dialog is already open, no need to add feedback
|
| 710 |
+
} else if (closestNpc) {
|
| 711 |
+
// Show talk prompt
|
| 712 |
+
const gameUI = document.getElementById('game-ui');
|
| 713 |
+
if (!gameUI.querySelector('.npc-talk-prompt')) {
|
| 714 |
+
const prompt = document.createElement('div');
|
| 715 |
+
prompt.className = 'npc-talk-prompt absolute bottom-20 left-4 bg-yellow-500 text-black px-3 py-2 rounded-lg font-bold';
|
| 716 |
+
prompt.textContent = 'Press SPACE to talk';
|
| 717 |
+
gameUI.appendChild(prompt);
|
| 718 |
+
}
|
| 719 |
+
} else {
|
| 720 |
+
// Remove talk prompt if no NPC nearby
|
| 721 |
+
const existingPrompt = document.getElementById('game-ui')?.querySelector('.npc-talk-prompt');
|
| 722 |
+
if (existingPrompt) {
|
| 723 |
+
existingPrompt.remove();
|
| 724 |
+
}
|
| 725 |
+
}
|
| 726 |
}
|
| 727 |
+
function showDialog(npc) {
|
|
|
|
| 728 |
if (!npc) return;
|
| 729 |
|
| 730 |
// Get next dialog line
|
|
|
|
| 735 |
document.getElementById('dialog-character').style.backgroundColor = npc.character.color;
|
| 736 |
document.getElementById('dialog-name').textContent = npc.character.name;
|
| 737 |
document.getElementById('dialog-text').textContent = dialog;
|
|
|
|
| 738 |
// Show dialog box
|
| 739 |
document.getElementById('dialog-box').classList.remove('translate-y-full');
|
| 740 |
+
|
| 741 |
+
// Remove the talk prompt when dialog opens
|
| 742 |
+
const existingPrompt = document.getElementById('game-ui')?.querySelector('.npc-talk-prompt');
|
| 743 |
+
if (existingPrompt) {
|
| 744 |
+
existingPrompt.remove();
|
| 745 |
+
}
|
| 746 |
+
}
|
| 747 |
function closeDialog() {
|
| 748 |
document.getElementById('dialog-box').classList.add('translate-y-full');
|
| 749 |
+
|
| 750 |
+
// Remove the talk prompt when dialog closes
|
| 751 |
+
const existingPrompt = document.getElementById('game-ui')?.querySelector('.npc-talk-prompt');
|
| 752 |
+
if (existingPrompt) {
|
| 753 |
+
existingPrompt.remove();
|
| 754 |
+
}
|
| 755 |
}
|
| 756 |
+
function handleKeyDown(event) {
|
|
|
|
| 757 |
switch (event.key.toLowerCase()) {
|
| 758 |
case 'w': gameState.keys.w = true; break;
|
| 759 |
case 'a': gameState.keys.a = true; break;
|
|
|
|
| 762 |
case 'shift': gameState.keys.shift = true; break;
|
| 763 |
case ' ':
|
| 764 |
gameState.keys.space = true;
|
| 765 |
+
if (gameState.nearbyNpc && !gameState.isJumping) {
|
| 766 |
showDialog(gameState.nearbyNpc);
|
| 767 |
+
event.preventDefault(); // Prevent default space behavior
|
| 768 |
}
|
| 769 |
break;
|
| 770 |
}
|
| 771 |
}
|
| 772 |
+
function handleKeyUp(event) {
|
|
|
|
| 773 |
switch (event.key.toLowerCase()) {
|
| 774 |
case 'w': gameState.keys.w = false; break;
|
| 775 |
case 'a': gameState.keys.a = false; break;
|