Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement #155 #167

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/ts/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export type SerializedPlayer = {
name: string;
balance: number;
creationDate: string;

visitedSystemHistory: StarSystemCoordinates[];

currentItinerary: StarSystemCoordinates[];
systemBookmarks: StarSystemCoordinates[];

Expand All @@ -18,6 +21,8 @@ export class Player {
balance: number;
creationDate: Date;

visitedSystemHistory: StarSystemCoordinates[] = [];

currentItinerary: StarSystemCoordinates[];
systemBookmarks: StarSystemCoordinates[];

Expand All @@ -28,6 +33,7 @@ export class Player {
name: string,
balance: number,
creationDate: Date,
visitedSystemHistory: StarSystemCoordinates[],
currentItinerary: StarSystemCoordinates[],
systemBookmarks: StarSystemCoordinates[],
currentMissions: Mission[],
Expand All @@ -36,21 +42,23 @@ export class Player {
this.name = name;
this.balance = balance;
this.creationDate = creationDate;
this.visitedSystemHistory = visitedSystemHistory;
this.currentItinerary = currentItinerary;
this.systemBookmarks = systemBookmarks;
this.currentMissions = currentMissions;
this.completedMissions = completedMissions;
}

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 {
return new Player(
serializedPlayer.name,
serializedPlayer.balance,
new Date(serializedPlayer.creationDate),
serializedPlayer.visitedSystemHistory,
serializedPlayer.currentItinerary,
serializedPlayer.systemBookmarks,
serializedPlayer.currentMissions.map((mission) => Mission.Deserialize(mission)),
Expand All @@ -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()),
Expand All @@ -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;
Expand Down
30 changes: 27 additions & 3 deletions src/ts/starSystem/starSystemView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
Loading