setShipReqs

This commit is contained in:
Yanick Champoux 2022-03-14 12:16:32 -04:00
parent 231b3547d5
commit ecda7a279c
3 changed files with 27 additions and 3 deletions

View File

@ -21,7 +21,6 @@ import { getContext } from 'svelte';
export let reqs = {}; export let reqs = {};
export let squadrons = []; export let squadrons = [];
$: console.log({squadrons})
const { dispatch } = getContext('ship'); const { dispatch } = getContext('ship');
$: dispatch.setCarrierBays(bays); $: dispatch.setCarrierBays(bays);

View File

@ -19,7 +19,6 @@
const ship = getContext("ship"); const ship = getContext("ship");
console.log( ship.dispatch )
$: ship.dispatch.setDrive({ rating, advanced }); $: ship.dispatch.setDrive({ rating, advanced });
</script> </script>

View File

@ -21,13 +21,39 @@ const dux = new Updux({
initial: { initial: {
reqs: { cost: 0, mass: 10, usedMass: 0 }, reqs: { cost: 0, mass: 10, usedMass: 0 },
}, },
actions: {}, actions: {
setShipReqs: null,
},
}); });
dux.setMutation("setShipMass", (mass) => u({ reqs: { mass } })); dux.setMutation("setShipMass", (mass) => u({ reqs: { mass } }));
dux.setMutation('setShipReqs', reqs => u({reqs}));
dux.addReaction(calculateDriveReqs); dux.addReaction(calculateDriveReqs);
dux.addReaction(ftlReqsReaction); dux.addReaction(ftlReqsReaction);
dux.addReaction(screensReqsReaction); dux.addReaction(screensReqsReaction);
dux.addReaction( (store) => (state) => {
let cost = 0;
let mass = 0;
let subsystems = Object.values(state);
while(subsystems.length>0) {
const subsystem = subsystems.shift();
if( typeof subsystem !== 'object' ) continue;
if( subsystem.reqs ) {
cost += subsystem.reqs.cost;
mass += subsystem.reqs.mass;
}
subsystems.push( ...Object.values(subsystem));
}
store.dispatch.setShipReqs({cost,usedMass: mass});
});
export default dux; export default dux;