-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a simulation for calculating the power used by a ship system.
- Loading branch information
1 parent
44a9894
commit 8630ae6
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {Entity, System} from "../utils/ecs"; | ||
|
||
/** | ||
* There's a subtle distinction between powerDraw and requestedPower | ||
* - powerDraw is how much power the system is currently pulling based | ||
* on it's current workload. | ||
* - requestedPower is an artificial limit placed by the crew that keeps | ||
* the power draw at or below that limit. | ||
*/ | ||
|
||
export class PowerDrawSystem extends System { | ||
test(entity: Entity) { | ||
return !!entity.components.power && !!entity.components.isShipSystem; | ||
} | ||
update(entity: Entity) { | ||
const systemType = entity.components.isShipSystem; | ||
const power = entity.components.power; | ||
const efficiency = entity.components.efficiency?.efficiency || 1; | ||
const efficiencyMultiple = 1 / efficiency; | ||
if (!systemType?.type || !power) return; | ||
const {maxSafePower, requiredPower} = power; | ||
let powerDraw = 0; | ||
switch (systemType.type) { | ||
case "warpEngines": { | ||
if (!entity.components.isWarpEngines) return; | ||
const {currentWarpFactor, warpFactorCount} = | ||
entity.components.isWarpEngines; | ||
if (currentWarpFactor === 0) break; | ||
const warpEngineUse = currentWarpFactor / warpFactorCount; | ||
powerDraw = | ||
(maxSafePower - requiredPower) * warpEngineUse + requiredPower; | ||
break; | ||
} | ||
case "impulseEngines": { | ||
if (!entity.components.isImpulseEngines) return; | ||
const {cruisingSpeed, targetSpeed} = entity.components.isImpulseEngines; | ||
if (targetSpeed === 0) break; | ||
const impulseEngineUse = targetSpeed / cruisingSpeed; | ||
powerDraw = | ||
(maxSafePower - requiredPower) * impulseEngineUse + requiredPower; | ||
break; | ||
} | ||
case "thrusters": { | ||
if (!entity.components.isThrusters) return; | ||
const {direction, rotationDelta, thrusting} = | ||
entity.components.isThrusters; | ||
if (!thrusting) break; | ||
const directionOutput = Math.hypot( | ||
direction.x, | ||
direction.y, | ||
direction.z | ||
); | ||
const rotationOutput = Math.hypot( | ||
rotationDelta.x, | ||
rotationDelta.y, | ||
rotationDelta.z | ||
); | ||
const totalOutput = directionOutput + rotationOutput; | ||
powerDraw = | ||
(maxSafePower - requiredPower) * totalOutput + requiredPower; | ||
break; | ||
} | ||
case "generic": | ||
powerDraw = power.requestedPower; | ||
break; | ||
default: | ||
return; | ||
} | ||
|
||
// Limit the power draw to the requested power, so we never go over it. | ||
entity.updateComponent("power", { | ||
powerDraw: Math.min(power.requestedPower, powerDraw * efficiencyMultiple), | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters