-
-
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(Inertial Dampeners): Add the plugin definition and config UI for…
… inertial dampeners.
- Loading branch information
1 parent
48b254a
commit c5890ca
Showing
9 changed files
with
202 additions
and
4 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
59 changes: 59 additions & 0 deletions
59
client/src/pages/Config/ShipSystems/SystemConfigs/inertialDampeners.tsx
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,59 @@ | ||
import {Navigate, useParams, useNavigate} from "react-router-dom"; | ||
import {useNetRequest} from "client/src/context/useNetRequest"; | ||
import Input from "@thorium/ui/Input"; | ||
import {AllShipSystems} from "server/src/classes/Plugins/ShipSystems/shipSystemTypes"; | ||
import {netSend} from "client/src/context/netSend"; | ||
import {toast} from "client/src/context/ToastContext"; | ||
|
||
export default function InertialDampenersConfig() { | ||
const {pluginId, systemId} = useParams() as { | ||
pluginId: string; | ||
systemId: string; | ||
}; | ||
const system = useNetRequest("pluginShipSystem", { | ||
pluginId, | ||
type: "inertialDampeners", | ||
systemId, | ||
}) as AllShipSystems["inertialDampeners"]; | ||
if (!system) return <Navigate to={`/config/${pluginId}/systems`} />; | ||
|
||
return ( | ||
<fieldset key={systemId} className="flex-1 overflow-y-auto"> | ||
<div className="flex flex-wrap"> | ||
<div className="flex-1 pr-4"> | ||
<div className="pb-2"> | ||
<Input | ||
labelHidden={false} | ||
inputMode="numeric" | ||
pattern="[0-9]*" | ||
label="Dampening" | ||
placeholder={"1"} | ||
helperText={ | ||
"Number > 0 that affects how fast the ship slows. Lower numbers slow the ship faster." | ||
} | ||
defaultValue={system.dampening} | ||
onBlur={async e => { | ||
if (!e.target.value || isNaN(Number(e.target.value))) return; | ||
try { | ||
await netSend("pluginInertialDampenersUpdate", { | ||
pluginId, | ||
shipSystemId: systemId, | ||
dampening: Number(e.target.value), | ||
}); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
toast({ | ||
title: "Error changing dampening", | ||
body: err.message, | ||
color: "error", | ||
}); | ||
} | ||
} | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</fieldset> | ||
); | ||
} |
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
34 changes: 34 additions & 0 deletions
34
server/src/classes/Plugins/ShipSystems/InertialDampeners.ts
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,34 @@ | ||
import BasePlugin from ".."; | ||
import BaseShipSystemPlugin from "./BaseSystem"; | ||
import {ShipSystemFlags} from "./shipSystemTypes"; | ||
|
||
export default class InertialDampenersPlugin extends BaseShipSystemPlugin { | ||
static flags: ShipSystemFlags[] = []; | ||
type: "inertialDampeners" = "inertialDampeners"; | ||
/** | ||
* A number > 0. Pulls the ship's velocity to make it match the current heading | ||
* and affects how fast the ship slows down when engines are turned off. | ||
* Lower number means slow the ship down faster */ | ||
dampening: number; | ||
constructor(params: Partial<InertialDampenersPlugin>, plugin: BasePlugin) { | ||
super(params, plugin); | ||
this.dampening = params.dampening || 1; | ||
} | ||
} | ||
|
||
/** | ||
* Dampening works like such. | ||
* | ||
* An acceleration is applied to the ship equal to the current velocity | ||
* multiplied by the reciprocal of the dampening factor plus 1. So 1 / (d + 1). | ||
* This makes it so the dampening factor is just a positive number and we don't | ||
* have to deal with reciprocals of decimals less than 1. | ||
* If our dampening factor is 10 we're going 300 km/s and stop, in the first | ||
* frame, we will slow down by 27 km/s. , then 24 km/s, etc. It's a logarithmic | ||
* plateau which will eventually be cut off to 0 km/s. | ||
* | ||
* This will be based on the current velocity of the ship that is _not_ in the | ||
* current direction of acceleration. So if our acceleration is {x:1, y:0, z:0} | ||
* and our velocity is {x: 21, y:5, z:2}, our dampening acceleration will be | ||
* {x: 0, y: 0.45, z:0.18}. | ||
*/ |
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
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
49 changes: 49 additions & 0 deletions
49
server/src/inputs/plugins/shipSystems/inertialDampeners.test.ts
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,49 @@ | ||
import InertialDampenersPlugin from "server/src/classes/Plugins/ShipSystems/InertialDampeners"; | ||
import {createMockDataContext} from "server/src/utils/createMockDataContext"; | ||
import {shipSystemsPluginInput} from "."; | ||
import {inertialDampenersPluginInput} from "./inertialDampeners"; | ||
|
||
describe("inertial dampeners plugin input", () => { | ||
it("should create a new impulse engine system", async () => { | ||
const dataContext = createMockDataContext(); | ||
const created = await shipSystemsPluginInput.pluginShipSystemCreate( | ||
dataContext, | ||
{ | ||
pluginId: "Test Plugin", | ||
type: "inertialDampeners", | ||
name: "Test Inertial Dampeners", | ||
} | ||
); | ||
|
||
expect(created).toBeTruthy(); | ||
expect(created.shipSystemId).toEqual("Test Inertial Dampeners"); | ||
const system = dataContext.server.plugins[0].aspects.shipSystems[0]; | ||
if (!(system instanceof InertialDampenersPlugin)) | ||
throw new Error("Not inertial dampeners"); | ||
expect(system.type).toEqual("inertialDampeners"); | ||
expect(system.dampening).toEqual(1); | ||
}); | ||
it("should update an impulse engine system", async () => { | ||
const dataContext = createMockDataContext(); | ||
const created = await shipSystemsPluginInput.pluginShipSystemCreate( | ||
dataContext, | ||
{ | ||
pluginId: "Test Plugin", | ||
type: "inertialDampeners", | ||
name: "Test Inertial Dampeners", | ||
} | ||
); | ||
const system = dataContext.server.plugins[0].aspects.shipSystems[0]; | ||
|
||
if (!(system instanceof InertialDampenersPlugin)) | ||
throw new Error("Not inertial dampeners"); | ||
expect(system.dampening).toEqual(1); | ||
inertialDampenersPluginInput.pluginInertialDampenersUpdate(dataContext, { | ||
pluginId: "Test Plugin", | ||
shipSystemId: "Test Inertial Dampeners", | ||
dampening: 2000, | ||
}); | ||
|
||
expect(system.dampening).toEqual(2000); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
server/src/inputs/plugins/shipSystems/inertialDampeners.ts
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,47 @@ | ||
import InertialDampenersPlugin from "server/src/classes/Plugins/ShipSystems/InertialDampeners"; | ||
import {DataContext} from "server/src/utils/DataContext"; | ||
import inputAuth from "server/src/utils/inputAuth"; | ||
import {pubsub} from "server/src/utils/pubsub"; | ||
import {getPlugin} from "../utils"; | ||
|
||
function getShipSystem( | ||
context: DataContext, | ||
pluginId: string, | ||
shipSystemId: string | ||
): InertialDampenersPlugin { | ||
const plugin = getPlugin(context, pluginId); | ||
const shipSystem = plugin.aspects.shipSystems.find( | ||
s => s.name === shipSystemId | ||
) as InertialDampenersPlugin; | ||
if (!shipSystem || shipSystem.type !== "inertialDampeners") { | ||
throw new Error("Ship system not found"); | ||
} | ||
return shipSystem; | ||
} | ||
|
||
export const inertialDampenersPluginInput = { | ||
async pluginInertialDampenersUpdate( | ||
context: DataContext, | ||
params: { | ||
pluginId: string; | ||
shipSystemId: string; | ||
dampening: number; | ||
} | ||
) { | ||
inputAuth(context); | ||
const shipSystem = getShipSystem( | ||
context, | ||
params.pluginId, | ||
params.shipSystemId | ||
); | ||
if (typeof params.dampening === "number" && params.dampening > 0) { | ||
shipSystem.dampening = params.dampening; | ||
} | ||
pubsub.publish("pluginShipSystem", { | ||
pluginId: params.pluginId, | ||
systemId: params.shipSystemId, | ||
}); | ||
|
||
return shipSystem; | ||
}, | ||
}; |
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