Skip to content

Commit

Permalink
feat: Have thrusters respond to the power supplied to them.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderson1993 committed Mar 18, 2023
1 parent a5b0fc4 commit 37e6b67
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
8 changes: 6 additions & 2 deletions server/src/systems/PowerDrawSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export class PowerDrawSystem extends System {
if (!entity.components.isThrusters) return;
const {direction, rotationDelta, thrusting} =
entity.components.isThrusters;
if (!thrusting) break;
const directionOutput = Math.hypot(
direction.x,
direction.y,
Expand All @@ -60,7 +59,12 @@ export class PowerDrawSystem extends System {
rotationDelta.y,
rotationDelta.z
);
const totalOutput = directionOutput + rotationOutput;
const overloadPercent = Math.min(
1,
power.requestedPower / maxSafePower
);
const totalOutput =
(directionOutput + rotationOutput) * overloadPercent;
powerDraw =
(maxSafePower - requiredPower) * totalOutput + requiredPower;
break;
Expand Down
15 changes: 11 additions & 4 deletions server/src/systems/RotationSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,20 @@ export class RotationSystem extends System {
rotationVelocity,
} = thrusters.components.isThrusters;

const currentPower = entity.components.power?.currentPower || 1;
const maxSafePower = entity.components.power?.maxSafePower || 1;
const requiredPower = entity.components.power?.requiredPower || 1;
const powerRatio = currentPower / maxSafePower;
let thrust =
currentPower > requiredPower ? rotationThrust * powerRatio : 0;

rotationAcceleration.set(
((rotationDelta.x * rotationThrust) / (mass * 20)) * elapsedRatio,
((rotationDelta.y * rotationThrust) / (mass * 20)) * elapsedRatio,
((rotationDelta.z * rotationThrust) / (mass * 20)) * elapsedRatio
((rotationDelta.x * thrust) / (mass * 20)) * elapsedRatio,
((rotationDelta.y * thrust) / (mass * 20)) * elapsedRatio,
((rotationDelta.z * thrust) / (mass * 20)) * elapsedRatio
);

const revolutionsPerSecond = rotationMaxSpeed / 60;
const revolutionsPerSecond = (rotationMaxSpeed * powerRatio) / 60;
const maxRadiansPerSecond = revolutionsPerSecond * (Math.PI * 2);

rotationVelocity.x = Math.min(
Expand Down
13 changes: 9 additions & 4 deletions server/src/systems/ThrusterSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ export class ThrusterSystem extends System {
const mass = ship.components.mass?.mass || 1;

const {direction, directionThrust} = entity.components.isThrusters;

const currentPower = entity.components.power?.currentPower || 1;
const maxSafePower = entity.components.power?.maxSafePower || 1;
const requiredPower = entity.components.power?.requiredPower || 1;
const powerRatio = currentPower / maxSafePower;
let thrust =
currentPower > requiredPower ? directionThrust * powerRatio : 0;
entity.updateComponent("isThrusters", {
directionAcceleration: {
x: (direction.x * directionThrust) / mass,
y: (direction.y * directionThrust) / mass,
z: (direction.z * directionThrust) / mass,
x: (direction.x * thrust) / mass,
y: (direction.y * thrust) / mass,
z: (direction.z * thrust) / mass,
},
});

Expand Down

0 comments on commit 37e6b67

Please sign in to comment.