Major Rewrite of underlying code
This commit is contained in:
parent
d031e2b7a1
commit
c7fa40b454
15 changed files with 923 additions and 437 deletions
src/lib
90
src/lib/ship.svelte.ts
Normal file
90
src/lib/ship.svelte.ts
Normal file
|
@ -0,0 +1,90 @@
|
|||
import {Thruster, THRUSTER_LIST} from "$lib/thruster.svelte";
|
||||
import {Grid} from "$lib/grid";
|
||||
import {INVENTORIES, Inventory} from "$lib/containers.svelte";
|
||||
|
||||
export class Ship {
|
||||
thrusters: Array<Thruster> = $state([]);
|
||||
inventories: Array<Inventory> = $state([]);
|
||||
grid: Grid = $state(Grid.Small);
|
||||
weight: number = $state(0);
|
||||
|
||||
constructor(grid: Grid = Grid.Small) {
|
||||
this.grid = grid;
|
||||
}
|
||||
|
||||
save() {
|
||||
return {
|
||||
thrusters: this.thrusters.map((thruster) => {
|
||||
return thruster.details.key;
|
||||
}),
|
||||
inventories: this.inventories.map((inventory) => {
|
||||
return inventory.details.key;
|
||||
}),
|
||||
grid: this.grid,
|
||||
weight: this.weight,
|
||||
}
|
||||
}
|
||||
|
||||
load(ship: {
|
||||
grid: Grid,
|
||||
thrusters: Array<string>,
|
||||
inventories: Array<string>,
|
||||
weight: number,
|
||||
}) {
|
||||
if (ship.grid !== undefined) {
|
||||
this.grid = ship.grid;
|
||||
}
|
||||
|
||||
if (ship.thrusters !== undefined) {
|
||||
this.thrusters = ship.thrusters.map((thruster) => {
|
||||
return new Thruster(THRUSTER_LIST[thruster]);
|
||||
});
|
||||
}
|
||||
|
||||
if (ship.inventories !== undefined) {
|
||||
this.inventories = ship.inventories.map((inventory) => {
|
||||
return new Inventory(INVENTORIES[inventory]);
|
||||
});
|
||||
}
|
||||
|
||||
if (ship.weight !== undefined) {
|
||||
this.weight = ship.weight;
|
||||
}
|
||||
}
|
||||
|
||||
addThruster(thruster: Thruster): void {
|
||||
this.thrusters.push(thruster);
|
||||
}
|
||||
|
||||
addInventory(inventory: Inventory): void {
|
||||
this.inventories.push(inventory);
|
||||
}
|
||||
|
||||
getTotalVolume(inventoryMultiplier: number) {
|
||||
let volume = 0;
|
||||
this.inventories.forEach((inventory) => {
|
||||
volume += inventory.getVolume(this.grid, inventoryMultiplier)
|
||||
});
|
||||
return volume;
|
||||
}
|
||||
|
||||
spliceInventories(index: number, length: number): void {
|
||||
this.inventories.splice(index, length);
|
||||
}
|
||||
|
||||
getTotalThrust(atmosphere: number): number {
|
||||
let thrust: number = 0;
|
||||
this.thrusters.forEach((thruster) => {
|
||||
thrust += thruster.getThrust(this.grid, atmosphere);
|
||||
});
|
||||
return thrust;
|
||||
}
|
||||
|
||||
getTotalMaxThrust(): number {
|
||||
let thrust = 0;
|
||||
this.thrusters.forEach((thruster) => {
|
||||
thrust += thruster.getMaxThrust(this.grid);
|
||||
});
|
||||
return thrust;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue