Skip to content

Commit

Permalink
feat(Ship Map): Initializes the ship map on the player ship when the …
Browse files Browse the repository at this point in the history
…flight starts. Closes #246
  • Loading branch information
alexanderson1993 committed Jun 14, 2022
1 parent f94f407 commit cc83b62
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/src/classes/FlightDataModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class FlightDataModel extends FSDataStore {
paused: this.paused,
date: this.date,
pluginIds: this.pluginIds,
entities: this.ecs.entities,
entities: this.ecs.entities.map(e => e.toJSON()),
clients: Object.fromEntries(
Object.entries(this.clients).map(([id, client]) => [id, client])
),
Expand Down
4 changes: 4 additions & 0 deletions server/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export type ComponentProperties = {
};

export {allComponents as components};

export function getComponentClassFromId(id: ComponentIDs): any {
return Object.values(allComponents).find(comp => comp.id === id);
}
1 change: 1 addition & 0 deletions server/src/components/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from "./solarSystem/isPlanet";
export * from "./satellite";
export * from "./temperature";
export * from "./population";
export * from "./shipMap";
42 changes: 42 additions & 0 deletions server/src/components/shipMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import DeckPlugin, {DeckEdge} from "../classes/Plugins/Ship/Deck";
import {Component} from "./utils";

const roundTo1000 = (num: number) => Math.round(num * 1000) / 1000;
export class ShipMapComponent extends Component {
static id = "shipMap" as const;
static serialize(data: ShipMapComponent) {
return {
decks: data.decks.map(deck => ({
...deck,
nodes: deck.nodes.map(
({flags, icon, id, isRoom, name, radius, x, y}) => {
const output: Partial<DeckPlugin["nodes"][0]> = {
id,
x: roundTo1000(x),
y: roundTo1000(y),
};
if (name) output.name = name;
if (icon) output.icon = icon;
if (isRoom) output.isRoom = isRoom;
if (radius) output.radius = radius;
if (flags?.length > 0) output.flags = flags;
return output;
}
),
})),
deckEdges: data.deckEdges.map(({id, flags, from, to, isOpen, weight}) => {
const output: Partial<DeckEdge> = {
id,
from,
to,
};
if (weight) output.weight = weight;
if (!isOpen) output.isOpen = isOpen;
if (flags?.length > 0) output.flags = flags;
return output;
}),
};
}
decks: DeckPlugin[] = [];
deckEdges: DeckEdge[] = [];
}
2 changes: 1 addition & 1 deletion server/src/inputs/flight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ export const flightInputs = {
name: ship.shipName,
position,
tags: ["player"],
playerShip: true,
},
context.server.plugins.filter(p =>
context.flight?.pluginIds.includes(p.id)
)
);

shipSystems.forEach(s => context.flight?.ecs.addEntity(s));
shipEntity.addComponent("isPlayerShip");
let theme = ship.theme || null;
if (!theme) {
theme = activePlugins.reduce(
Expand Down
12 changes: 12 additions & 0 deletions server/src/spawners/ship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function spawnShip(
position: Coordinates;
tags?: string[];
assets?: Partial<InstanceType<typeof ShipPlugin>["assets"]>;
playerShip?: boolean;
},
plugins: BasePlugin[]
) {
Expand Down Expand Up @@ -73,5 +74,16 @@ export function spawnShip(
],
});
});
if (params.playerShip) {
entity.addComponent("isPlayerShip");
}
// Initialize the ship map. For now, we'll just load the ship map onto a component of the ship.
// In the future, rooms themselves might become entities.
if (entity.components.isPlayerShip) {
entity.addComponent("shipMap", {
decks: template.decks || [],
deckEdges: template.deckEdges || [],
});
}
return {ship: entity, shipSystems};
}
3 changes: 2 additions & 1 deletion server/src/utils/ecs/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ComponentProperties as Components,
ComponentIDs,
components as allComponents,
getComponentClassFromId,
} from "../../components";

type DeepPartial<T> = Partial<{
Expand Down Expand Up @@ -108,7 +109,7 @@ class Entity {
components: Object.fromEntries(
Object.entries(this.components).map(([key, comp]) => {
let newValue =
allComponents[key as keyof typeof allComponents]?.serialize(
getComponentClassFromId(key as ComponentIDs)?.serialize(
comp as any
) || comp;
return [key, newValue];
Expand Down

0 comments on commit cc83b62

Please sign in to comment.