Skip to content

Commit

Permalink
fix: Add some default power connections to the power grid.
Browse files Browse the repository at this point in the history
Closes #613
  • Loading branch information
alexanderson1993 committed Mar 19, 2024
1 parent abe697b commit 8640193
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions server/src/spawners/ship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,49 @@ export function spawnShip(
systemEntities.forEach((e) => {
entity.components.shipSystems?.shipSystems.set(e.id, {});
});

// And connect up the power nodes for good measure
// Every battery gets one Reactor
const batteries = systemEntities.filter((e) => e.components.isBattery);
const reactors = systemEntities.filter((e) => e.components.isReactor);
let reactorIndex = 0;
// Connect batteries to power nodes in this order
const powerNodeOrder = [
"internal",
"intel",
"defense",
"navigation",
"offense",
];
batteries.forEach((battery, i) => {
reactorIndex = i % reactors.length;
const reactor = reactors[reactorIndex];
reactor.updateComponent("isReactor", {
connectedEntities: [
...(reactor.components.isReactor?.connectedEntities || []),
battery.id,
],
});
const powerNode = powerNodes[powerNodeOrder[i % powerNodeOrder.length]];
battery.updateComponent("isBattery", {
connectedNodes: [
...(battery.components.isBattery?.connectedNodes || []),
powerNode.entity.id,
],
});
});
// Make sure every power node is connected to at least one reactor
Object.values(powerNodes).forEach((node, i) => {
const reactor = reactors[(reactorIndex + i) % reactors.length];
reactor.updateComponent("isReactor", {
connectedEntities: [
...(reactor.components.isReactor?.connectedEntities || []),
node.entity.id,
],
});
});

// Now we can add the ship to the ECS
if (params.playerShip) {
entity.addComponent("isPlayerShip");
entity.addComponent("physicsWorld");
Expand Down

0 comments on commit 8640193

Please sign in to comment.