ImpactStorage
ImpactStorage copied to clipboard
Local Storage plugin for ImpactJS
trafficstars
ImpactStorage - Local Storage plugin for ImpactJS
ImpactStorage is a plugin for HTML5/js game framework ImpactJS, giving developers an easy-to-use interface to localStorage for their projects. Mostly a facade for localStorage, ImpactStorage is set up to bake with your ImpactJS project, take care of some error handling, some convenience methods, as well as support objects in addition to strings.
Created by Jordan Santell for Duck It! (oh god, I need to finish this); enjoy!
Change Log
1.01
- Cleaned up a lot of redundant syntax
- Methods return
nullif the browser doesn't support localStorage --isCapable()should still be used - Added a minified version
Installation
- Move impact-storage.js to your ImpactJS project's plugin folder:
[PROJECT]/lib/plugins/impact-storage.js - Add the plugin to your main.js file
ig.module(
'game.main'
)
.requires(
'impact-game',
...
'plugins.impact-storage',
...
)
- Create a local storage object and check out the examples and methods below
storage: new ig.Storage(),
Methods
isCapable(): Returnstrueif the browser is capable of using localStorage.falseotherwise.isSet(key): Returnstrueifkeyhas been set in localStorage.falseotherwise.initUnset(key, value): Iffkeyhas not been set, setkeytovalueget(key): Returns the value associated withkeyin localStorage as astring, or anobjectif parsable by JSON.getInt(key): Returns the value associated withkeyin localStorage as anint.getFloat(key): Returns the value associated withkeyin localStorage as afloat.getBool(key): Returns the value associated withkeyin localStorage as abool. Returnsnullifvalueis not0,1,falseortrue.key(n): Returns the value of the key stored at positionnin localStorage.set(key, value): Sets an item in localStorage with thekeyvaluepair. Overwrites the previous value ofkeyif it existed previously.valueis stored as either astringor anobject.setHighest(key, value): Sets an item in localStorage with thekeyvaluepair iff the currently storedvalueis smaller, then returns true. Otherwise returns false.remove(key): Removes the item with the specifiedkey.clear(): Clears all localStorage data associated with this origin.
Examples
High Score
this.storage = new ig.Storage();
// Initialize high score as 0 if 'highScore' does not exist
this.storage.initUnset('highScore', 0);
During the update loop that determines whether or not the current score should override the score in localStorage:
var player = this.getEntitiesByType(EntityPlayer)[0];
/*
Updates the value of 'highScore' if and only
if player.score > this.storage.get('highScore')
*/
this.storage.setHighest('highScore',player.score);
Storing JSON Objects
localStorage stores all data as strings, but the ImpactStorage .set(key, value) and .get(key) methods convert objects to and from strings in order to be saved to localStorage.
this.storage = new ig.Storage();
/*
Player's velocity is an object stored as
vel: {x: 200, y: 100}
And that data is now being stored with key playerVel in localStorage
*/
var player = this.getEntitiesByType(EntityPlayer)[0];
this.storage.set('playerVel',player.vel)
// And let's output it for fun
alert("Player's x velocity: "+this.storage.get('playerVel').x);
alert("Player's y velocity: "+this.storage.get('playerVel').y);