Skip to content

Commit

Permalink
Step 4.11: Add 'Restorable' class
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB committed Aug 29, 2018
1 parent f1574f0 commit 5951647
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions resources/scripts/engine/restorable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Engine.Restorable = class Restorable {
// Acts the same as canvas's save() and restore() API.
// 'restorable' props are defined in the constructor
constructor(...restorableProps) {
this._restorableProps = restorableProps;
this._restorableStates = [];
}

// Save current state in the stack
save() {
this._restorableStates.push(this._restorableProps.reduce((state, prop) => {
state[prop] = this[prop];
return state;
}, {}));
}

// Pop most recent state and apply it
restore() {
_.extend(this, this._restorableStates.pop());
}
};
1 change: 1 addition & 0 deletions views/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<!-- Scripts -->
<script type="text/javascript" src="/scripts/namespaces.js"></script>
<script type="text/javascript" src="/scripts/engine/restorable.js"></script>
<script type="text/javascript" src="/scripts/engine/sprite.js"></script>
<script type="text/javascript" src="/scripts/engine/animations/keyframe.js"></script>
<script type="text/javascript" src="/scripts/engine/key_states.js"></script>
Expand Down

0 comments on commit 5951647

Please sign in to comment.