aotds-docks/src/lib/shipDux/propulsion/ftl.js

42 lines
943 B
JavaScript
Raw Normal View History

2022-03-03 16:06:51 +00:00
import { Updux } from "updux";
import u from "updeep";
import { createSelector } from "reselect";
import reqs from "../reqs.js";
export const ftlTypes = ["none", "standard", "advanced"];
const dux = new Updux({
subduxes: { reqs },
initial: {
type: "none",
2022-04-10 21:42:29 +00:00
uiTransform: "",
2022-03-03 16:06:51 +00:00
},
actions: {
2022-04-10 21:42:29 +00:00
setFtl: null,
setFtlReqs: null,
2022-03-03 16:06:51 +00:00
},
});
export default dux;
2022-04-10 21:42:29 +00:00
dux.setMutation("setFtl", (type) => u({ type }));
dux.setMutation("setFtlReqs", (reqs) => u({ reqs }));
2022-03-03 16:06:51 +00:00
2022-04-10 21:42:29 +00:00
export function calcFtlReqs(type, shipMass) {
if (type === "none") return { cost: 0, mass: 0 };
2022-03-03 16:06:51 +00:00
2022-04-10 21:42:29 +00:00
const mass = Math.ceil(shipMass / 10);
2022-03-03 16:06:51 +00:00
2022-04-10 21:42:29 +00:00
return {
mass,
cost: mass * (type === "advanced" ? 3 : 2),
};
2022-03-03 16:06:51 +00:00
}
// needs to be at the top level
2022-04-10 21:42:29 +00:00
export const ftlReqsReaction = (store) =>
2022-03-03 16:06:51 +00:00
createSelector(
2022-04-10 21:42:29 +00:00
[(ship) => ship.propulsion.ftl.type, (ship) => ship.reqs.mass],
(type, shipMass) => store.dispatch.setFtlReqs(calcFtlReqs(type, shipMass))
);