diff --git a/index.html b/index.html index 290b0f8..409d981 100644 --- a/index.html +++ b/index.html @@ -90,6 +90,26 @@

How to Move:

Inventory

+ + diff --git a/js/app.js b/js/app.js index 167c32e..19e5823 100644 --- a/js/app.js +++ b/js/app.js @@ -159,6 +159,8 @@ class PwshLeafmapGame { const durMin = Math.round(data.duration / 60); pathDisplay.textContent = `${distKm}km • ~${durMin}min • ${data.travelMode}`; } + + this.renderRouteCard(data?.routeCard || []); } hidePathInfo() { @@ -166,6 +168,50 @@ class PwshLeafmapGame { if (pathPanel) { pathPanel.style.display = 'none'; } + + const routeCardPanel = document.getElementById('routeCardPanel'); + if (routeCardPanel) { + routeCardPanel.style.display = 'none'; + } + } + + renderRouteCard(routeCardRows) { + const panel = document.getElementById('routeCardPanel'); + const body = document.getElementById('routeCardBody'); + + if (!panel || !body) { + return; + } + + body.innerHTML = ''; + + if (!Array.isArray(routeCardRows) || routeCardRows.length === 0) { + panel.style.display = 'none'; + return; + } + + routeCardRows.forEach(row => { + const tr = document.createElement('tr'); + tr.innerHTML = ` + ${row.serial ?? ''} + ${row.from ?? ''} + ${row.to ?? ''} + ${row.directionText ?? ''} + ${this.formatDistance(row.segmentDistance)} + ${this.formatDistance(row.cumulativeDistance)} + `; + body.appendChild(tr); + }); + + panel.style.display = 'block'; + } + + formatDistance(distanceMeters) { + const meters = Number(distanceMeters) || 0; + if (meters < 1000) { + return `${Math.round(meters)} m`; + } + return `${(meters / 1000).toFixed(2)} km`; } async autoLoadGameData() { diff --git a/js/map.js b/js/map.js index 9368ffb..e243b93 100644 --- a/js/map.js +++ b/js/map.js @@ -80,7 +80,8 @@ class GameMap { distance: path.distance, duration: path.duration, travelMode: path.travelMode, - pathType: path.type + pathType: path.type, + routeCard: path.routeCard || [] }); } @@ -497,7 +498,9 @@ class GameMap { this.game.updatePathInfo({ distance: pathData.distance, duration: pathData.duration, - type: pathData.type + type: pathData.type, + travelMode: pathData.travelMode, + routeCard: pathData.routeCard || [] }); } diff --git a/js/pathfinding.js b/js/pathfinding.js index b9e1983..1520365 100644 --- a/js/pathfinding.js +++ b/js/pathfinding.js @@ -63,7 +63,7 @@ class PathfindingManager { const profile = travelMode === 'foot' ? 'foot' : 'driving'; // Build OSRM URL - const url = `${this.osrmUrl}/${profile}/${start.lng},${start.lat};${destination.lng},${destination.lat}?overview=full&geometries=geojson`; + const url = `${this.osrmUrl}/${profile}/${start.lng},${start.lat};${destination.lng},${destination.lat}?overview=full&geometries=geojson&steps=true`; console.log('Fetching OSRM route:', url); @@ -83,7 +83,8 @@ class PathfindingManager { distance: route.distance, // meters duration: route.duration, // seconds type: 'road', - travelMode: travelMode + travelMode: travelMode, + routeCard: this.normalizeOSRMRouteToRouteCard(route, start, destination, travelMode, 'road') }; // Cache the route @@ -106,7 +107,8 @@ class PathfindingManager { distance: distance, duration: duration, type: 'direct', - travelMode: travelMode + travelMode: travelMode, + routeCard: this.createDirectRouteCard(start, destination, distance, travelMode, 'direct') }; console.log(`Direct path: ${path.distance}m, ${path.duration}s`); @@ -242,6 +244,154 @@ class PathfindingManager { this.routeCache.clear(); console.log('Route cache cleared'); } + + /** + * Normalize OSRM legs/steps into reusable route-card rows + */ + normalizeOSRMRouteToRouteCard(route, start, destination, travelMode, pathType) { + if (!route || !route.legs || route.legs.length === 0) { + return this.createDirectRouteCard(start, destination, start.distanceTo(destination), travelMode, pathType); + } + + const rows = []; + let serial = 1; + let cumulativeDistance = 0; + let previousLabel = this.formatCoordinateLabel(start); + + route.legs.forEach((leg, legIndex) => { + const steps = Array.isArray(leg.steps) ? leg.steps : []; + steps.forEach((step, stepIndex) => { + const segmentDistance = Number(step.distance) || 0; + cumulativeDistance += segmentDistance; + + const currentLabel = this.getStepLabel(step, legIndex, stepIndex); + const nextStep = steps[stepIndex + 1]; + const isLastStep = legIndex === route.legs.length - 1 && stepIndex === steps.length - 1; + const toLabel = isLastStep + ? this.formatCoordinateLabel(destination) + : (nextStep ? this.getStepLabel(nextStep, legIndex, stepIndex + 1) : currentLabel); + + rows.push({ + serial: serial++, + from: previousLabel, + to: toLabel, + directionText: this.getStepDirectionText(step, toLabel), + segmentDistance: segmentDistance, + cumulativeDistance: cumulativeDistance, + travelMode: travelMode, + pathType: pathType, + roadName: step.name || '', + maneuverType: step.maneuver?.type || '', + modifier: step.maneuver?.modifier || '' + }); + + previousLabel = currentLabel; + }); + }); + + if (rows.length === 0) { + return this.createDirectRouteCard(start, destination, start.distanceTo(destination), travelMode, pathType); + } + + return rows; + } + + /** + * Build a synthetic route-card row for direct-line travel + */ + createDirectRouteCard(start, destination, distance, travelMode, pathType) { + const bearing = this.getBearing(start, destination); + const cardinal = this.bearingToCardinal(bearing); + + return [{ + serial: 1, + from: this.formatCoordinateLabel(start), + to: this.formatCoordinateLabel(destination), + directionText: `Head ${cardinal} to destination`, + segmentDistance: distance, + cumulativeDistance: distance, + travelMode: travelMode, + pathType: pathType, + roadName: '', + maneuverType: 'direct', + modifier: cardinal + }]; + } + + getStepLabel(step, legIndex, stepIndex) { + if (step && step.name && step.name.trim().length > 0) { + return step.name.trim(); + } + + return `Segment ${legIndex + 1}.${stepIndex + 1}`; + } + + getStepDirectionText(step, toLabel) { + const maneuver = step?.maneuver || {}; + const type = maneuver.type || 'continue'; + const modifier = maneuver.modifier || ''; + const roadName = (step?.name || '').trim(); + + if (type === 'roundabout') { + const exitText = maneuver.exit ? ` (exit ${maneuver.exit})` : ''; + return `Enter roundabout${exitText}${roadName ? ` onto ${roadName}` : ''}`; + } + + if (type === 'arrive') { + return `Arrive at ${toLabel}`; + } + + if (modifier) { + return `${this.capitalize(modifier)} ${type}${roadName ? ` on ${roadName}` : ''}`; + } + + return `${this.capitalize(type)}${roadName ? ` on ${roadName}` : ''}`; + } + + formatCoordinateLabel(latLng) { + if (!latLng || typeof latLng.lat !== 'number' || typeof latLng.lng !== 'number') { + return 'Unknown'; + } + + return `[${latLng.lat.toFixed(5)}, ${latLng.lng.toFixed(5)}]`; + } + + getBearing(start, destination) { + const startLat = this.toRadians(start.lat); + const startLng = this.toRadians(start.lng); + const endLat = this.toRadians(destination.lat); + const endLng = this.toRadians(destination.lng); + + const y = Math.sin(endLng - startLng) * Math.cos(endLat); + const x = + Math.cos(startLat) * Math.sin(endLat) - + Math.sin(startLat) * Math.cos(endLat) * Math.cos(endLng - startLng); + + const bearing = (this.toDegrees(Math.atan2(y, x)) + 360) % 360; + return bearing; + } + + bearingToCardinal(bearing) { + const directions = ['north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'northwest']; + const index = Math.round(bearing / 45) % 8; + return directions[index]; + } + + toRadians(degrees) { + return degrees * (Math.PI / 180); + } + + toDegrees(radians) { + return radians * (180 / Math.PI); + } + + capitalize(value) { + if (!value || typeof value !== 'string') { + return ''; + } + + return value.charAt(0).toUpperCase() + value.slice(1); + } } // Export for use in other modules