diff options
Diffstat (limited to 'html/tower/js/path.js')
-rw-r--r-- | html/tower/js/path.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/html/tower/js/path.js b/html/tower/js/path.js index f594ee2..46e4bfb 100644 --- a/html/tower/js/path.js +++ b/html/tower/js/path.js @@ -80,4 +80,42 @@ function generatePath(grid) { } return Promise.resolve(path); +} + +function getPathPosition(progress, path) { + // Ensure progress is between 0 and 1 + progress = Math.max(0, Math.min(1, progress)); + + // Get the total path length + let totalLength = 0; + for (let i = 1; i < path.length; i++) { + const dx = path[i].x - path[i-1].x; + const dy = path[i].y - path[i-1].y; + totalLength += Math.sqrt(dx * dx + dy * dy); + } + + // Find target distance along path + const targetDistance = progress * totalLength; + + // Find the segment where the target position lies + let currentDistance = 0; + for (let i = 1; i < path.length; i++) { + const dx = path[i].x - path[i-1].x; + const dy = path[i].y - path[i-1].y; + const segmentLength = Math.sqrt(dx * dx + dy * dy); + + if (currentDistance + segmentLength >= targetDistance) { + // Calculate position within this segment + const segmentProgress = (targetDistance - currentDistance) / segmentLength; + return { + x: path[i-1].x + dx * segmentProgress, + y: path[i-1].y + dy * segmentProgress + }; + } + + currentDistance += segmentLength; + } + + // If we somehow exceed the path length, return the last point + return { ...path[path.length - 1] }; } \ No newline at end of file |