Slot Action
by Sam Fereday
JavaScript
// The idea here is to allow for slottable states that will exit on generic conditions or be exchanged if another state is given. The main challenge here is to make things as functional as possible.
const GameLoop = ((G) => {
const main = (tFrame) => {
if (!G) return;
// TODO: May need to unsubscribe when a new method pushed.
G.stopMain = window.requestAnimationFrame(main);
G();
};
main(); // Start the cycle.
});
// Given a composition of functions, run them until the given condition is met
const compose = (...funcs) => {
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)));
};
//
const withState = (stateName, stateUpdater, initialState) => {
// ...
}
const withLoop = (fn, conditionToFinish) => {
// ...
}
//
const testState = compose(
withState('isComplete', 'setIsComplete', false),
withLoop(() => {
console.log("Updating");
})
);
//
class Entity {
}
//
// It's just a lot easier to run game loop and loop over a list of entities that need updating over time. Doing this functionaly would require some sort of push to a listing, and at least some form of state. That's okay to use state, it's just when it gets overkill.
const Shell = {
entities: [],
start: () => {
const ent = new Entity();
entities.push(ent);
},
update: () => {
entities.forEach(({ update }) => update());
},
onAction: (action, id) => {
entities[0].sendAction(actionId);
}
}
// start => update
GameLoop(Shell.update);