From 0750519b7290348dee09257f2ed9528ff53c0af7 Mon Sep 17 00:00:00 2001 From: Justin Oros Date: Wed, 20 May 2026 21:52:46 -0700 Subject: [PATCH] Fix NPC proximity to measure from player screen position not world origin --- game.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/game.js b/game.js index 1104ad1..c8db96b 100644 --- a/game.js +++ b/game.js @@ -425,11 +425,22 @@ class Game { } } + _playerScreenOffset() { + const {viewW, viewH} = this._getViewport(); + return { px: viewW / 2, py: viewH - (viewH * 0.35) - 32 }; + } + + _npcScreenDist() { + const {viewW, viewH} = this._getViewport(); + const {px, py} = this._playerScreenOffset(); + const npcSX = viewW / 2 + (this.state.npcWorldX - this.state.worldX); + const npcSY = viewH / 2 + (this.state.npcWorldY - this.state.worldY); + return Math.sqrt((npcSX - px) ** 2 + (npcSY - py) ** 2); + } + _checkNPCProximity() { const {viewW, viewH} = this._getViewport(); - const dx = this.state.npcWorldX - this.state.worldX; - const dy = this.state.npcWorldY - this.state.worldY; - const dist = Math.sqrt(dx * dx + dy * dy); + const dist = this._npcScreenDist(); const near = !(this.state.npcWorldX === 0 && this.state.npcWorldY === 0) && dist < 300; const hint = document.getElementById('map-talk-hint'); @@ -481,9 +492,7 @@ class Game { _nearNPC() { if (this.state.npcWorldX === 0 && this.state.npcWorldY === 0) return false; - const dx = this.state.npcWorldX - this.state.worldX; - const dy = this.state.npcWorldY - this.state.worldY; - return Math.sqrt(dx * dx + dy * dy) < 300; + return this._npcScreenDist() < 300; } _talkToNPC() {