carrier component

This commit is contained in:
Yanick Champoux 2023-04-22 10:37:49 -04:00
parent 7120f410a3
commit 4a69641120
8 changed files with 76 additions and 44 deletions

View File

@ -3,17 +3,23 @@
<Structure {...structure} />
<Weaponry {...weaponry} />
{#if identification.isCarrier}
<Carrier {...carrier} />
{/if}
<script>
import Identification from "./ShipEdit/Identification.svelte";
import Propulsion from "./ShipEdit/Propulsion.svelte";
import shipDux from "$lib/store/ship";
import Structure from "./ShipEdit/Structure.svelte";
import Weaponry from "./ShipEdit/Weaponry.svelte";
import Carrier from "./ShipEdit/Carrier.svelte";
export let identification = {};
export let propulsion = {};
export let structure = {};
export let weaponry = {};
export let carrier = {};
</script>
<style>

View File

@ -1,16 +1,18 @@
<Section label="carrier">
<div>
<ShipItem {...reqs}>
<Field label="bays">
<input class="short" type="number" min="0" bind:value={bays} />
<input class="short" type="number" min="1" bind:value={bays} />
</Field>
</ShipItem>
</div>
{#each squadrons as squadron, id (id)}
<Squadron {...squadron} id={id + 1} />
{/each}
</Section>
<script lang="ts">
<script>
import { getContext } from "svelte";
import Field from "$lib/components/Field.svelte";
@ -18,13 +20,15 @@
import Section from "$lib/components/Section.svelte";
import Squadron from "./Carrier/Squadron.svelte";
export let bays = 0;
export let bays = 1;
export let reqs = { cost: 0, mass: 0 };
export let squadrons = [];
if (bays < 1) bays = 1;
export let api = getContext("api");
$: api?.dispatch?.setCarrierBays?.(bays);
$: api?.dispatch.setNbrCarrierBays(bays);
</script>
<style>

View File

@ -1,12 +1,14 @@
<ShipItem {...reqs}>
<Field label={`squadron ${id}`}>
<div class="field label suffix">
<select bind:value={type}>
{#each types as type (type)}
<option>{type}</option>
{/each}
</select>
</Field>
</ShipItem>
<label class="active">squadron {id}</label>
<i>arrow_drop_down</i>
</div></ShipItem
>
<script>
import { getContext } from "svelte";
@ -14,7 +16,7 @@
import Section from "$lib/components/Section.svelte";
import Field from "$lib/components/Field.svelte";
import ShipItem from "$lib/components/ShipItem.svelte";
// import { squadronTypes } from "$lib/shipDux/carrier";
import { squadronTypes } from "$lib/store/ship/carrier.ts";
const types = squadronTypes.map(({ type }) => type);
@ -30,6 +32,9 @@
<style>
select {
width: inherit;
/* width: inherit; */
}
div {
display: inline-block;
}
</style>

View File

@ -34,6 +34,8 @@
export let isCarrier = false;
export let reqs = {};
$: console.log("in the comp", isCarrier, shipClass);
export let api = getContext("api");
$: shipTypes = candidateShipTypes(reqs.mass, isCarrier).map(
@ -45,7 +47,6 @@
$: api?.dispatch?.updateIdentification?.({ shipType, shipClass });
$: console.log(isCarrier);
$: api?.dispatch.setCarrier?.(isCarrier);
</script>

View File

@ -11,7 +11,9 @@
<Weaponry {...$shipState.weaponry} />
{#if $shipState.identification.isCarrier}
<Carrier {...$shipState.carrier} />
{/if}
</main>
<script>

View File

@ -8,10 +8,15 @@ export const createApi = () => {
? JSON.parse(localStorage.getItem("ship") || "null")
: undefined;
const api = ship.createStore(state || undefined);
const api = ship.createStore({
preloadedState: state,
});
api.dispatch.restore(state);
if (browser) {
api.subscribe(() => {
console.log("saving...", api.getState());
localStorage.setItem("ship", JSON.stringify(api.getState()));
});
}

View File

@ -1,5 +1,5 @@
import { createSelector } from "@reduxjs/toolkit";
import Updux from "updux";
import Updux, { createPayloadAction } from "updux";
import * as R from "remeda";
import memoize from "memoize-one";
@ -51,7 +51,12 @@ const weaponry = new Updux({
},
});
const restore = createPayloadAction<typeof shipDux.initialState>("restore");
const shipDux = new Updux({
actions: {
restore,
},
initialState: {
schemaVersion: "1",
},
@ -64,6 +69,8 @@ const shipDux = new Updux({
},
});
shipDux.addMutation(restore, (state) => () => state);
shipDux.addReaction((api) => {
return createSelector(
api.selectors.getFtlType,

View File

@ -1,4 +1,6 @@
{#if ship}
<ShipEdit {...ship} />
{/if}
<script>
import { getContext } from "svelte";
@ -7,7 +9,7 @@
export let api = getContext("api");
let ship = {};
let ship = api?.getState() ?? {};
api?.subscribe(() => {
ship = api.getState();
});