Git Product home page Git Product logo

impactstorage's Introduction

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 null if 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(): Returns true if the browser is capable of using localStorage. false otherwise.
  • isSet(key): Returns true if key has been set in localStorage. false otherwise.
  • initUnset(key, value): Iff key has not been set, set key to value
  • get(key): Returns the value associated with key in localStorage as a string, or an object if parsable by JSON.
  • getInt(key): Returns the value associated with key in localStorage as an int.
  • getFloat(key): Returns the value associated with key in localStorage as a float.
  • getBool(key): Returns the value associated with key in localStorage as a bool. Returns null if value is not 0, 1, false or true.
  • key(n): Returns the value of the key stored at position n in localStorage.
  • set(key, value): Sets an item in localStorage with the key value pair. Overwrites the previous value of key if it existed previously. value is stored as either a string or an object.
  • setHighest(key, value): Sets an item in localStorage with the key value pair iff the currently stored value is smaller.
  • remove(key): Removes the item with the specified key.
  • 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);

More Information

impactstorage's People

Contributors

jsantell avatar

Watchers

Kevin Isom avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.