aotds-docks/src/lib/shipDux/index.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-03-02 00:14:01 +00:00
import { Updux } from "updux";
2022-03-14 14:53:36 +00:00
import u from "updeep";
2022-03-01 22:13:05 +00:00
2022-03-02 18:42:42 +00:00
import propulsion from "./propulsion/index.js";
2022-03-02 00:14:01 +00:00
import identification from "./identification.js";
2022-03-14 14:53:36 +00:00
import { calculateDriveReqs } from "./propulsion/drive.js";
import { ftlReqsReaction } from "./propulsion/ftl.js";
import structure from "./structure/index.js";
import carrier from "./carrier.js";
import weaponry from "./weaponry/index.js";
import { screensReqsReaction } from "./structure/screens.js";
2022-03-01 22:13:05 +00:00
const dux = new Updux({
2022-03-15 14:59:33 +00:00
subduxes: {
2022-03-14 14:53:36 +00:00
identification,
propulsion,
structure,
carrier,
weaponry,
2022-03-15 14:59:33 +00:00
},
2022-03-14 14:53:36 +00:00
initial: {
reqs: { cost: 0, mass: 10, usedMass: 0 },
},
2022-03-14 16:16:32 +00:00
actions: {
setShipReqs: null,
},
2022-03-01 22:13:05 +00:00
});
2022-03-14 14:53:36 +00:00
dux.setMutation("setShipMass", (mass) => u({ reqs: { mass } }));
2022-03-14 16:16:32 +00:00
dux.setMutation('setShipReqs', reqs => u({reqs}));
2022-03-02 16:14:31 +00:00
2022-03-14 14:53:36 +00:00
dux.addReaction(calculateDriveReqs);
dux.addReaction(ftlReqsReaction);
dux.addReaction(screensReqsReaction);
2022-03-02 19:00:18 +00:00
2022-03-14 16:16:32 +00:00
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});
});
2022-03-01 22:13:05 +00:00
export default dux;