armor
This commit is contained in:
parent
15495ee843
commit
9afbd45419
41
src/lib/components/ShipEdit/Structure/Armor.svelte
Normal file
41
src/lib/components/ShipEdit/Structure/Armor.svelte
Normal 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>
|
22
src/lib/components/ShipEdit/Structure/Armor/Layer.svelte
Normal file
22
src/lib/components/ShipEdit/Structure/Armor/Layer.svelte
Normal 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>
|
@ -1,6 +1,7 @@
|
||||
<Section label="structure">
|
||||
<Hull {...hull}/>
|
||||
<Screens {...screens} />
|
||||
<Armor {...armor} />
|
||||
<Cargo {...cargo} />
|
||||
<Streamlining {...streamlining} />
|
||||
</Section>
|
||||
@ -10,12 +11,14 @@
|
||||
import Hull from './Hull.svelte';
|
||||
import Screens from './Screens.svelte';
|
||||
import Cargo from './Cargo.svelte';
|
||||
import Armor from './Armor.svelte';
|
||||
import Streamlining from './Streamlining.svelte';
|
||||
|
||||
export let hull = {};
|
||||
export let screens = {};
|
||||
export let cargo = {};
|
||||
export let streamlining = {};
|
||||
export let armor = {};
|
||||
|
||||
|
||||
</script>
|
||||
|
50
src/lib/shipDux/structure/armor.js
Normal file
50
src/lib/shipDux/structure/armor.js
Normal 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
|
||||
}
|
||||
}
|
@ -3,10 +3,11 @@ import { Updux } from 'updux';
|
||||
import hull from './hull.js';
|
||||
import screens from './screens.js';
|
||||
import cargo from './cargo.js';
|
||||
import armor from './armor.js';
|
||||
import streamlining from './streamlining.js';
|
||||
|
||||
const dux = new Updux({
|
||||
subduxes: { hull, screens, cargo, streamlining }
|
||||
subduxes: { hull, screens, cargo, streamlining, armor }
|
||||
});
|
||||
export default dux;
|
||||
|
||||
|
@ -2,7 +2,6 @@ import { Updux } from "updux";
|
||||
import u from "updeep";
|
||||
|
||||
import reqs from "../reqs.js";
|
||||
import { calculateDriveReqs } from "../propulsion/drive.js";
|
||||
|
||||
const dux = new Updux({
|
||||
subduxes: {
|
||||
|
Loading…
Reference in New Issue
Block a user