Add Component Density Toggle

This commit is contained in:
Neshura 2024-05-20 18:42:28 +02:00
parent cebb36a76f
commit d081dedef9
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 60 additions and 15 deletions

View file

@ -79,13 +79,21 @@ export const inventorySizes = new Map([
]); ]);
export const metricModifiers: Map<number, string> = new Map([ export const metricModifiers: Map<number, string> = new Map([
/*[1000000000, "G"],*/ [1000000000, "G"],
[1000000, "M"], [1000000, "M"],
[1000, "k"], [1000, "k"],
[1, ""] [1, ""]
]) ]);
export const weightPerVolume = 1/0.37; export enum Density {
Ore = "ore",
Component = "component",
}
export const densityValues: Map<Density, number> = new Map([
[Density.Ore, 1/0.37],
[Density.Component, 1/0.047],
])
export const localization = new Map([ export const localization = new Map([
["space engineers", new Map([ ["space engineers", new Map([
@ -230,7 +238,19 @@ export const localization = new Map([
["separator", new Map([ ["separator", new Map([
["en-GB", ","], ["en-GB", ","],
["de-DE", "."] ["de-DE", "."]
])] ])],
["ore", new Map([
["en-GB", "Ore"],
["de-DE", "Erz"]
])],
["component", new Map([
["en-GB", "Component"],
["de-DE", "Komponente"]
])],
["density", new Map([
["en-GB", "Density"],
["de-DE", "Dichte"]
])],
]) ])
export type Thruster = { export type Thruster = {

View file

@ -10,7 +10,7 @@
localization, metricModifiers, type Thruster, localization, metricModifiers, type Thruster,
ThrusterSize, ThrusterSize,
ThrusterType, ThrusterType,
thrustValues, weightPerVolume thrustValues, Density, densityValues
} from "$lib/constants"; } from "$lib/constants";
import {Input} from "$lib/components/ui/input"; import {Input} from "$lib/components/ui/input";
import {Label} from "$lib/components/ui/label"; import {Label} from "$lib/components/ui/label";
@ -26,6 +26,7 @@
localStorage.setItem("thrusters", JSON.stringify(thrusters)); localStorage.setItem("thrusters", JSON.stringify(thrusters));
} }
}) })
let inventories: Array<InventoryType> = $state([]) let inventories: Array<InventoryType> = $state([])
$effect(() => { $effect(() => {
if (mounted) { if (mounted) {
@ -33,28 +34,34 @@
} }
}) })
let inventoryMultiplier = $state(1); let inventoryMultiplier: number = $state(3);
$effect(() => { $effect(() => {
if (mounted) { if (mounted) {
localStorage.setItem("inventoryMultiplier", inventoryMultiplier); localStorage.setItem("inventoryMultiplier", inventoryMultiplier);
} }
}) })
let gridSize = $state(Grids.Small); let gridSize: Grids = $state(Grids.Small);
$effect(() => { $effect(() => {
if (mounted) { if (mounted) {
localStorage.setItem("gridSize", gridSize); localStorage.setItem("gridSize", gridSize);
} }
}) })
let gravity = $state(1); let gravity: number = $state(1);
$effect(() => { $effect(() => {
if (mounted) { if (mounted) {
localStorage.setItem("gravity", gravity); localStorage.setItem("gravity", gravity);
} }
}) })
let density: Density = $state(Density.Ore);
$effect(() => {
if (mounted) {
localStorage.setItem("density", density);
}
})
let newThruster = $state({ let newThruster: Thruster = $state({
type: ThrusterType.Atmospheric, type: ThrusterType.Atmospheric,
size: ThrusterSize.Small, size: ThrusterSize.Small,
}) })
@ -64,21 +71,21 @@
} }
}) })
let newInventory = $state(InventoryType.CargoSmall) let newInventory: InventoryType = $state(InventoryType.CargoMedium)
$effect(() => { $effect(() => {
if (mounted) { if (mounted) {
localStorage.setItem("newInventory", JSON.stringify(newInventory)) localStorage.setItem("newInventory", JSON.stringify(newInventory))
} }
}) })
let totalThrust = $derived.by(() => { let totalThrust: number = $derived.by(() => {
let thrust = 0; let thrust = 0;
thrusters.forEach((thruster) => { thrusters.forEach((thruster) => {
thrust += getThrusterPower(thruster) thrust += getThrusterPower(thruster)
}) })
return thrust return thrust
}); });
let totalVolume = $derived.by(() => { let totalVolume: number = $derived.by(() => {
let volume = 0; let volume = 0;
inventories.forEach((inventory) => { inventories.forEach((inventory) => {
volume += getInventoryVolume(inventory); volume += getInventoryVolume(inventory);
@ -86,12 +93,12 @@
return volume; return volume;
}) })
let maxWeight = $derived(totalThrust / (gravity * 9.81)) let maxWeight: number = $derived(totalThrust / (gravity * 9.81))
let maxVehicleWeight = $derived(maxWeight - totalVolume * weightPerVolume) let maxVehicleWeight: number = $derived(maxWeight - totalVolume * densityValues.get(density))
function weightConversion(weight: number): string { function weightConversion(weight: number): string {
if (weight > 1000) { if (weight > 1000 || weight < -1000) {
return applyUnits(weight/1000, "t") return applyUnits(weight/1000, "t")
} }
else { else {
@ -148,6 +155,9 @@
} }
} }
} }
if (value < 0) {
return `-${applyUnits(value * -1, unit)}`
}
let rounded = Math.round(((value + Number.EPSILON) * 100) / 100) let rounded = Math.round(((value + Number.EPSILON) * 100) / 100)
return `${rounded}${unit}` return `${rounded}${unit}`
} }
@ -173,6 +183,10 @@
gravity = localStorage.getItem("gravity") gravity = localStorage.getItem("gravity")
} }
if (localStorage.getItem("density") !== null) {
density = localStorage.getItem("density")
}
if (localStorage.getItem("inventoryMultiplier") !== null) { if (localStorage.getItem("inventoryMultiplier") !== null) {
inventoryMultiplier = localStorage.getItem("inventoryMultiplier") inventoryMultiplier = localStorage.getItem("inventoryMultiplier")
} }
@ -215,6 +229,17 @@
{/each} {/each}
</div> </div>
<Separator /> <Separator />
<Label for="density">{localized("density")}</Label>
<div id="density" class="flex">
{#each Object.values(Density) as value}
{#if density === value}
<Button variant="primary">{localized(value)}</Button>
{:else}
<Button variant="secondary" onclick={() => density = value}>{localized(value)}</Button>
{/if}
{/each}
</div>
<Separator />
<p>{localized("liftableVehcileWeight")}: {weightConversion(maxVehicleWeight)}</p> <p>{localized("liftableVehcileWeight")}: {weightConversion(maxVehicleWeight)}</p>
<p>{localized("liftableWeight")}: {weightConversion(maxWeight)}</p> <p>{localized("liftableWeight")}: {weightConversion(maxWeight)}</p>
</Card.Content> </Card.Content>