diff --git a/src/ts/player/player.ts b/src/ts/player/player.ts index a464a8ad..2d51ce04 100644 --- a/src/ts/player/player.ts +++ b/src/ts/player/player.ts @@ -6,6 +6,9 @@ export type SerializedPlayer = { name: string; balance: number; creationDate: string; + + visitedSystemHistory: StarSystemCoordinates[]; + currentItinerary: StarSystemCoordinates[]; systemBookmarks: StarSystemCoordinates[]; @@ -18,6 +21,8 @@ export class Player { balance: number; creationDate: Date; + visitedSystemHistory: StarSystemCoordinates[] = []; + currentItinerary: StarSystemCoordinates[]; systemBookmarks: StarSystemCoordinates[]; @@ -28,6 +33,7 @@ export class Player { name: string, balance: number, creationDate: Date, + visitedSystemHistory: StarSystemCoordinates[], currentItinerary: StarSystemCoordinates[], systemBookmarks: StarSystemCoordinates[], currentMissions: Mission[], @@ -36,6 +42,7 @@ export class Player { this.name = name; this.balance = balance; this.creationDate = creationDate; + this.visitedSystemHistory = visitedSystemHistory; this.currentItinerary = currentItinerary; this.systemBookmarks = systemBookmarks; this.currentMissions = currentMissions; @@ -43,7 +50,7 @@ export class Player { } public static Default(): Player { - return new Player("Python", 10_000, new Date(), [], [], [], []); + return new Player("Python", 10_000, new Date(), [], [], [], [], []); } public static Deserialize(serializedPlayer: SerializedPlayer): Player { @@ -51,6 +58,7 @@ export class Player { serializedPlayer.name, serializedPlayer.balance, new Date(serializedPlayer.creationDate), + serializedPlayer.visitedSystemHistory, serializedPlayer.currentItinerary, serializedPlayer.systemBookmarks, serializedPlayer.currentMissions.map((mission) => Mission.Deserialize(mission)), @@ -63,6 +71,7 @@ export class Player { name: player.name, balance: player.balance, creationDate: player.creationDate.toISOString(), + visitedSystemHistory: player.visitedSystemHistory, currentItinerary: player.currentItinerary, systemBookmarks: player.systemBookmarks, currentMissions: player.currentMissions.map((mission) => mission.serialize()), @@ -74,6 +83,7 @@ export class Player { this.name = player.name; this.balance = player.balance; this.creationDate = player.creationDate; + this.visitedSystemHistory = player.visitedSystemHistory; this.currentItinerary = player.currentItinerary; this.systemBookmarks = player.systemBookmarks; this.currentMissions = player.currentMissions; diff --git a/src/ts/starSystem/starSystemView.ts b/src/ts/starSystem/starSystemView.ts index f4c554a2..6c6d5f0c 100644 --- a/src/ts/starSystem/starSystemView.ts +++ b/src/ts/starSystem/starSystemView.ts @@ -71,6 +71,7 @@ import { getSystemModelFromCoordinates } from "./modelFromCoordinates"; import { StarSystemModel } from "./starSystemModel"; import { OrbitalObjectType } from "../architecture/orbitalObject"; import { OrbitalFacility } from "../spacestation/orbitalFacility"; +import { getStarGalacticPosition } from "../utils/coordinates/starSystemCoordinatesUtils"; /** * The star system view is the part of Cosmos Journeyer responsible to display the current star system, along with the @@ -417,6 +418,8 @@ export class StarSystemView implements View { this.starSystem.dispose(); this.targetCursorLayer.reset(); this.spaceStationLayer.reset(); + + this.player.visitedSystemHistory.push(this.starSystem.model.coordinates); } this.starSystem = new StarSystemController(starSystemModel, this.scene); @@ -458,11 +461,32 @@ export class StarSystemView implements View { const firstBody = celestialBodies[0]; if (firstBody === undefined) throw new Error("No bodies in star system"); - const activeController = this.scene.getActiveControls(); - let controllerDistanceFactor = 7; + const activeControls = this.scene.getActiveControls(); + let controllerDistanceFactor = 4; if (firstBody instanceof BlackHole) controllerDistanceFactor = 5; else if (firstBody instanceof NeutronStar) controllerDistanceFactor = 100_000; - positionNearObjectBrightSide(activeController, firstBody, starSystem, controllerDistanceFactor); + if (this.player.visitedSystemHistory.length === 0) { + positionNearObjectBrightSide(activeControls, firstBody, starSystem, controllerDistanceFactor); + } else { + // place player in the direction of the previous system (where we came from) + const currentSystemPosition = getStarGalacticPosition(starSystem.model.coordinates); + const previousSystemPosition = getStarGalacticPosition(this.player.visitedSystemHistory[this.player.visitedSystemHistory.length - 1]); + + // compute direction from previous system to current system + const placementDirection = previousSystemPosition.subtract(currentSystemPosition).normalize(); + Vector3.TransformCoordinatesToRef(placementDirection, starSystem.starFieldBox.getRotationMatrix(), placementDirection); + + // offset the player from the first body + const positionOffset = placementDirection.scale(controllerDistanceFactor * firstBody.getBoundingRadius()); + activeControls.getTransform().setAbsolutePosition(firstBody.getTransform().getAbsolutePosition().add(positionOffset)); + + // put the player back to the origin of the star system + starSystem.translateEverythingNow(activeControls.getTransform().getAbsolutePosition().negate()); + activeControls.getTransform().setAbsolutePosition(Vector3.Zero()); + + // look at the first body + activeControls.getTransform().lookAt(firstBody.getTransform().getAbsolutePosition()); + } starSystem.initPostProcesses(this.postProcessManager);