diff options
Diffstat (limited to 'Homework/cs5410/bbiy/src/systems/undo.js')
| -rw-r--r-- | Homework/cs5410/bbiy/src/systems/undo.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Homework/cs5410/bbiy/src/systems/undo.js b/Homework/cs5410/bbiy/src/systems/undo.js new file mode 100644 index 0000000..086d6b4 --- /dev/null +++ b/Homework/cs5410/bbiy/src/systems/undo.js @@ -0,0 +1,32 @@ +game.system.Undo = (entitiesGrid, logicSystem, gridSystem) => { + const states = []; + + const update = (elapsedTime, entities, changedIds) => { + if (changedIds.size) { + 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) => { + let state = states.slice(0, -1).pop(); + if (states.length > 1) { + states.pop(); + } + for (let id in state) { + for (let componentName in state[id]) { + entities[id].addComponent({name: componentName, ...state[id][componentName]}); + } + } + gridSystem.update(0, entities, new Set()); + logicSystem.parseRules(entities); + }; + + return { update, undo }; +}; |
