deal with the uiTransform in the dux

This commit is contained in:
Yanick Champoux 2022-04-10 17:42:29 -04:00
parent 012db36e65
commit 68fdda4d67
3 changed files with 90 additions and 55 deletions

View File

@ -11,48 +11,84 @@ import weaponry from "./weaponry/index.js";
import { screensReqsReaction } from "./structure/screens.js"; import { screensReqsReaction } from "./structure/screens.js";
const dux = new Updux({ const dux = new Updux({
subduxes: { subduxes: {
identification, identification,
propulsion, propulsion,
structure, structure,
carrier, carrier,
weaponry, weaponry,
}, },
initial: { initial: {
reqs: { cost: 0, mass: 10, usedMass: 0 }, reqs: { cost: 0, mass: 10, usedMass: 0 },
}, },
actions: { actions: {
setShipReqs: null, setShipReqs: null,
}, setUITransform: null,
},
}); });
dux.setMutation("setShipMass", (mass) => u({ reqs: { mass } })); dux.setMutation("setShipMass", (mass) => u({ reqs: { mass } }));
dux.setMutation('setShipReqs', reqs => u({reqs})); dux.setMutation("setShipReqs", (reqs) => u({ reqs }));
dux.setMutation("setUITransform", ({ system, systemId, translate }) => {
const transform = translate
? `translate(${translate[0]}px,${translate[1]}px)`
: "";
switch (system) {
case "firecons":
return u.updateIn("weaponry.firecons.uiTransform", transform);
case "weapon":
return u.updateIn(
"weaponry.weapons",
u.map(u.if(({ id }) => id === systemId, u({ uiTransform: transform })))
);
case "screens":
return u.updateIn("structure.screens.uiTransform", transform);
case "hull":
return u.updateIn("structure.hull.uiTransform", transform);
case "internalSystems":
const path = "structure.uiTransform";
return u.updateIn(path, transform);
case "ftl":
return u.updateIn("propulsion.ftl.uiTransform", transform);
case "drive":
return u.updateIn("propulsion.drive.uiTransform", transform);
default:
return (state) => state;
}
});
dux.addReaction(calculateDriveReqs); dux.addReaction(calculateDriveReqs);
dux.addReaction(ftlReqsReaction); dux.addReaction(ftlReqsReaction);
dux.addReaction(screensReqsReaction); dux.addReaction(screensReqsReaction);
dux.addReaction( (store) => (state) => { dux.addReaction((store) => (state) => {
let cost = 0; let cost = 0;
let mass = 0; let mass = 0;
let subsystems = Object.values(state); let subsystems = Object.values(state);
while(subsystems.length>0) { while (subsystems.length > 0) {
const subsystem = subsystems.shift(); const subsystem = subsystems.shift();
if( typeof subsystem !== 'object' ) continue; if (typeof subsystem !== "object") continue;
if( subsystem.reqs ) { if (subsystem.reqs) {
cost += subsystem.reqs.cost; cost += subsystem.reqs.cost;
mass += subsystem.reqs.mass; mass += subsystem.reqs.mass;
}
subsystems.push( ...Object.values(subsystem));
} }
store.dispatch.setShipReqs({cost,usedMass: mass}); subsystems.push(...Object.values(subsystem));
}
store.dispatch.setShipReqs({ cost, usedMass: mass });
}); });
export default dux; export default dux;

View File

@ -10,35 +10,32 @@ const dux = new Updux({
subduxes: { reqs }, subduxes: { reqs },
initial: { initial: {
type: "none", type: "none",
uiTransform: "",
}, },
actions: { actions: {
setFtl: null, setFtl: null,
setFtlReqs: null, setFtlReqs: null,
}, },
}); });
export default dux; export default dux;
dux.setMutation( 'setFtl', type => u({type}) ); dux.setMutation("setFtl", (type) => u({ type }));
dux.setMutation( 'setFtlReqs', reqs => u({reqs}) ); dux.setMutation("setFtlReqs", (reqs) => u({ reqs }));
export function calcFtlReqs(type,shipMass) { export function calcFtlReqs(type, shipMass) {
if(type==="none") return { cost: 0, mass: 0 }; if (type === "none") return { cost: 0, mass: 0 };
const mass = Math.ceil(shipMass / 10); const mass = Math.ceil(shipMass / 10);
return { return {
mass, mass,
cost: mass * ( type === 'advanced' ? 3 : 2 ), cost: mass * (type === "advanced" ? 3 : 2),
} };
} }
// needs to be at the top level // needs to be at the top level
export const ftlReqsReaction = store => export const ftlReqsReaction = (store) =>
createSelector( createSelector(
[ [(ship) => ship.propulsion.ftl.type, (ship) => ship.reqs.mass],
(ship) => ship.propulsion.ftl.type, (type, shipMass) => store.dispatch.setFtlReqs(calcFtlReqs(type, shipMass))
(ship) => ship.reqs.mass, );
],
(type,shipMass) =>
store.dispatch.setFtlReqs(calcFtlReqs(type,shipMass))
);

View File

@ -1,13 +1,15 @@
import { Updux } from 'updux'; 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 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, armor } subduxes: { hull, screens, cargo, streamlining, armor },
initial: {
uiTransform: "",
},
}); });
export default dux; export default dux;