From 69b5f4448c1cbd00ebcb6f444f2434cc272b7e97 Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Sun, 10 Apr 2022 22:13:05 -0600 Subject: Unlimited undo; add spritename component instead of sprites on each entity; update canvas on game object --- src/systems/undo.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/systems/undo.js (limited to 'src/systems/undo.js') diff --git a/src/systems/undo.js b/src/systems/undo.js new file mode 100644 index 0000000..fa36c93 --- /dev/null +++ b/src/systems/undo.js @@ -0,0 +1,32 @@ +game.system.Undo = (entitiesGrid) => { + const states = []; + + const update = (elapsedTime, entities, changedIds) => { + if (changedIds.size) { + lastUndid = false; + const state = {}; + for (let id in entities) { + if (entities[id].hasComponent("gridPosition")) { + state[id] = JSON.parse(JSON.stringify(entities[id].components)); + } + } + states.push(state); + } + return new Set(); + } + + const undo = (entities) => { + states.map((state) => console.log(state[65].gridPosition)); + let state = states.slice(0, -1).pop(); + for (let id in state) { + for (let componentName in state[id]) { + entities[id].addComponent({name: componentName, ...state[id][componentName]}); + } + } + if (states.length > 1) { + states.pop(); + } + } + + return { update, undo }; +} \ No newline at end of file -- cgit v1.2.3-70-g09d2