From 9afbd45419229faaa0f62f82830dc2bb5fe42ec5 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 4 Mar 2022 19:10:51 -0500 Subject: [PATCH] armor --- .../ShipEdit/Structure/Armor.svelte | 41 +++++++++++++++ .../ShipEdit/Structure/Armor/Layer.svelte | 22 ++++++++ .../ShipEdit/Structure/index.svelte | 3 ++ src/lib/shipDux/structure/armor.js | 50 +++++++++++++++++++ src/lib/shipDux/structure/index.js | 3 +- src/lib/shipDux/structure/streamlining.js | 1 - 6 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/lib/components/ShipEdit/Structure/Armor.svelte create mode 100644 src/lib/components/ShipEdit/Structure/Armor/Layer.svelte create mode 100644 src/lib/shipDux/structure/armor.js diff --git a/src/lib/components/ShipEdit/Structure/Armor.svelte b/src/lib/components/ShipEdit/Structure/Armor.svelte new file mode 100644 index 0000000..de581f2 --- /dev/null +++ b/src/lib/components/ShipEdit/Structure/Armor.svelte @@ -0,0 +1,41 @@ + +
+
+ + + +
+ +
+ {#each layers as rating,i (i)} + + {/each} +
+
+
+ + + + diff --git a/src/lib/components/ShipEdit/Structure/Armor/Layer.svelte b/src/lib/components/ShipEdit/Structure/Armor/Layer.svelte new file mode 100644 index 0000000..b6dab9e --- /dev/null +++ b/src/lib/components/ShipEdit/Structure/Armor/Layer.svelte @@ -0,0 +1,22 @@ + + + + + + + diff --git a/src/lib/components/ShipEdit/Structure/index.svelte b/src/lib/components/ShipEdit/Structure/index.svelte index b627975..f6c583e 100644 --- a/src/lib/components/ShipEdit/Structure/index.svelte +++ b/src/lib/components/ShipEdit/Structure/index.svelte @@ -1,6 +1,7 @@
+
@@ -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 = {}; diff --git a/src/lib/shipDux/structure/armor.js b/src/lib/shipDux/structure/armor.js new file mode 100644 index 0000000..9b43574 --- /dev/null +++ b/src/lib/shipDux/structure/armor.js @@ -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 + } +} diff --git a/src/lib/shipDux/structure/index.js b/src/lib/shipDux/structure/index.js index 4311ec4..c5fe3cb 100644 --- a/src/lib/shipDux/structure/index.js +++ b/src/lib/shipDux/structure/index.js @@ -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; diff --git a/src/lib/shipDux/structure/streamlining.js b/src/lib/shipDux/structure/streamlining.js index f05f09f..aca39e6 100644 --- a/src/lib/shipDux/structure/streamlining.js +++ b/src/lib/shipDux/structure/streamlining.js @@ -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: {