feat: return-to-door system for map exits

When entering an interior (e.g. lab from town), the game saves the
player's current position as a return point. When exiting, if the
exit has no explicit targetX/targetY, the system pops the stored
return point and warps back to that exact position.

This means interior exits just need targetMap — the player always
returns to the specific door they entered from, not a hardcoded
position. Falls back to the destination map's spawn if no return
point is stored.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jose Luis
2026-03-20 17:02:51 +01:00
parent b60edc49af
commit 1d494d8ef3
3 changed files with 143 additions and 187 deletions

View File

@@ -113,8 +113,38 @@ function handleInteraction(event) {
case 'mapExit': {
const { targetMap, targetX, targetY } = event.data;
warpToMap(targetMap, targetX, targetY);
console.log(`[gameMode] warped to ${targetMap} (${targetX}, ${targetY})`);
const p = worldState.player;
// Save return point: where the player is NOW (one tile back from the exit)
// so when they leave the target map, they return here
worldState.returnPoints.push({
fromMap: worldState.currentMap,
fromX: p.x,
fromY: p.y
});
// If exit has explicit coordinates, use them
// Otherwise, check for a stored return point for the target map
if (targetX != null && targetY != null) {
warpToMap(targetMap, targetX, targetY);
} else {
// Pop the most recent return point for this map
const retIdx = worldState.returnPoints.findLastIndex(
rp => rp.fromMap === targetMap
);
if (retIdx >= 0) {
const ret = worldState.returnPoints[retIdx];
worldState.returnPoints.splice(retIdx, 1);
warpToMap(ret.fromMap, ret.fromX, ret.fromY);
} else {
// Fallback: use map spawn point
const destMap = getMap(targetMap);
const sx = destMap?.spawn?.x ?? 0;
const sy = destMap?.spawn?.y ?? 0;
warpToMap(targetMap, sx, sy);
}
}
console.log(`[gameMode] warped to ${worldState.currentMap} (${worldState.player.x}, ${worldState.player.y})`);
break;
}