This commit is contained in:
Yanick Champoux 2022-03-04 19:10:51 -05:00
parent 15495ee843
commit 9afbd45419
6 changed files with 118 additions and 2 deletions

View File

@ -0,0 +1,41 @@
<ShipItem {...reqs}>
<div>
<div class="nbr_layers">
<Field label="armour layers">
<input type="number" min="0" bind:value={nbr_layers} />
</Field>
</div>
<div class="layers">
{#each layers as rating,i (i)}
<Layer {rating} layer={i+1} />
{/each}
</div>
</div>
</ShipItem>
<script>
import { getContext } from 'svelte';
import ShipItem from "$lib/components/ShipItem/index.svelte";
import Field from "$lib/components/Field/index.svelte";
import Layer from "./Armor/Layer.svelte";
export let layers = [];
export let reqs = {};
let nbr_layers = layers.length;
const { dispatch } = getContext('ship');
$: dispatch.setArmorLayers(nbr_layers);
</script>
<style>
.layers {
display: flex;
}
input {
width: 5em;
}
</style>

View File

@ -0,0 +1,22 @@
<Field label={`layer ${layer}`}>
<input type="number" min="0" bind:value={rating} />
</Field>
<script>
import { getContext } from "svelte";
import Field from "$lib/components/Field/index.svelte";
export let layer = 1;
export let rating = 0;
const ship = getContext("ship");
$: ship.dispatch.setArmorRating({ layer, rating });
</script>
<style>
input {
width: 5em;
}
</style>

View File

@ -1,6 +1,7 @@
<Section label="structure"> <Section label="structure">
<Hull {...hull}/> <Hull {...hull}/>
<Screens {...screens} /> <Screens {...screens} />
<Armor {...armor} />
<Cargo {...cargo} /> <Cargo {...cargo} />
<Streamlining {...streamlining} /> <Streamlining {...streamlining} />
</Section> </Section>
@ -10,12 +11,14 @@
import Hull from './Hull.svelte'; import Hull from './Hull.svelte';
import Screens from './Screens.svelte'; import Screens from './Screens.svelte';
import Cargo from './Cargo.svelte'; import Cargo from './Cargo.svelte';
import Armor from './Armor.svelte';
import Streamlining from './Streamlining.svelte'; import Streamlining from './Streamlining.svelte';
export let hull = {}; export let hull = {};
export let screens = {}; export let screens = {};
export let cargo = {}; export let cargo = {};
export let streamlining = {}; export let streamlining = {};
export let armor = {};
</script> </script>

View File

@ -0,0 +1,50 @@
import { Updux } from "updux";
import u from "updeep";
import reqs from "../reqs.js";
const dux = new Updux({
subduxes: {
reqs,
},
initial: {
layers: [],
},
actions: {
setArmorLayers: null,
setArmorRating: null,
},
});
export default dux;
dux.setMutation('setArmorRating', ({layer, rating}) => state => {
let layers = [ ...state.layers ].map( (v,k) => k === layer-1 ? rating : v );
return { layers, reqs: calcArmorReqs(layers) }
} );
dux.setMutation( 'setArmorLayers', nbrLayers => state => {
let layers = [...state.layers];
if( nbrLayers < state.layers.length )
layers = [ ...state.layers ].slice(0,nbrLayers);
while( layers.length < nbrLayers ) {
layers.push(0);
}
return {
layers,
reqs: calcArmorReqs(layers),
}
});
function calcArmorReqs(layers) {
const mass = 2* layers.reduce( (a,b) => a+ b,0 );
const cost = 2* layers.map( (v,k) => v * (k+1) ).reduce( (a,b) => a+ b,0 );
return {
mass, cost
}
}

View File

@ -3,10 +3,11 @@ import { Updux } from 'updux';
import hull from './hull.js'; import hull from './hull.js';
import screens from './screens.js'; import screens from './screens.js';
import cargo from './cargo.js'; import cargo from './cargo.js';
import armor from './armor.js';
import streamlining from './streamlining.js'; import streamlining from './streamlining.js';
const dux = new Updux({ const dux = new Updux({
subduxes: { hull, screens, cargo, streamlining } subduxes: { hull, screens, cargo, streamlining, armor }
}); });
export default dux; export default dux;

View File

@ -2,7 +2,6 @@ import { Updux } from "updux";
import u from "updeep"; import u from "updeep";
import reqs from "../reqs.js"; import reqs from "../reqs.js";
import { calculateDriveReqs } from "../propulsion/drive.js";
const dux = new Updux({ const dux = new Updux({
subduxes: { subduxes: {