GameState

Copyright(c) 2012 Stefano Balietti MIT Licensed

Representation of the state of a game:

state: the higher-level building blocks of a game step: the sub-unit of a state round: the number of repetition for a state. Defaults round = 1 is: the load-lavel of the game as expressed in node.is paused: TRUE if the game is paused

See
GameLoop ---
(function (exports, node) {
  

Global scope

  
var JSUS = node.JSUS;

Expose constructor

exports.GameState = GameState;




GameState.defaults = {};

GameState.defaults.hash

Default hash string for game-states

@see GameState.toHash

GameState.defaults.hash = 'S.s.r.i.p';

GameState constructor

Creates an instance of a GameState

It accepts an object literal or an hash string as defined in GameState.defaults.hash.

If no parameter is passed, all the properties of the GameState object are set to 0

Params
gs object string An object literal | hash string representing the game state @see GameState.defaults.hash
function GameState (gs) {

Public properties

GameState.state

The N-th game-block (state) in the game-loop currently being executed

@see GameLoop

  this.state =  0;

GameState.step

The N-th game-block (step) nested in the current state

@see GameState.state

  this.step =   0;

GameState.round

The number of times the current state was repeated

  this.round =  0;
  

GameState.is

The state of the nodeGame engine

@see node.is

  this.is = node.is.UNKNOWN;
  

GameState.paused

TRUE if the game is paused

  this.paused =   false;
  
  if ('string' === typeof gs) {
    var tokens = gs.split('.');   
    this.state =  ('undefined' !== typeof tokens[0]) ? Number(tokens[0]) : undefined;
    this.step =   ('undefined' !== typeof tokens[1]) ? Number(tokens[1]) : undefined;
    this.round =  ('undefined' !== typeof tokens[2]) ? Number(tokens[2]) : undefined;
    this.is =     ('undefined' !== typeof tokens[3]) ? Number(tokens[3]) : node.is.UNKNOWN;
    this.paused =   (tokens[4] === '1') ? true : false;
  }
  else if ('object' === typeof gs) {  
    this.state =  gs.state;
    this.step =   gs.step;
    this.round =  gs.round;
    this.is =     (gs.is) ? gs.is : node.is.UNKNOWN;
    this.paused =   (gs.paused) ? gs.paused : false;
  }
  
}

GameState.toString

Converts the current instance of GameState to a string

Returns
string out The string representation of the state of the GameState
GameState.prototype.toString = function () {
  var out = this.toHash('(r) S.s');
  if (this.paused) {
    out += ' [P]';
  }
  return out;
};

GameState.toHash

Returns a simplified hash of the state of the GameState, according to the input string

Params
str string The hash code
Returns
string hash The hashed game states
See
GameState.toHash (static)
GameState.prototype.toHash = function (str) {
  return GameState.toHash(this, str);
};

GameState.toHash (static)

Returns a simplified hash of the state of the GameState, according to the input string.

The following characters are valid to determine the hash string

  • S: state
  • s: step
  • r: round
  • i: is
  • P: paused

E.g.

    var gs = new GameState({
              round: 1,
              state: 2,
              step: 1,
              is: 50,
              paused: false,
    });

    gs.toHash('(R) S.s'); // (1) 2.1
Params
gs GameState The game state to hash
str string The hash code
Returns
string hash The hashed game states
GameState.toHash = function (gs, str) {
  if (!gs || 'object' !== typeof gs) return false;
  if (!str || !str.length) return gs.toString();
  
  var hash = '',
    symbols = 'Ssrip',
    properties = ['state', 'step', 'round', 'is', 'paused'];
  
  for (var i = 0; i < str.length; i++) {
    var idx = symbols.indexOf(str[i]); 
    hash += (idx < 0) ? str[i] : Number(gs[properties[idx]]);
  }
  return hash;
};

GameState.compare (static)

Compares two GameState objects|hash strings and returns

  • 0 if they represent the same game state
  • a positive number if gs1 is ahead of gs2
  • a negative number if gs2 is ahead of gs1

If the strict parameter is set, also the is property is compared, otherwise only round, state, and step

The accepted hash string format is the following: 'S.s.r.i.p'. Refer to GameState.toHash for the semantic of the characters.

Params
gs1 GameState string The first GameState object|string to compare
gs2 GameState string The second GameState object|string to compare
strict Boolean If TRUE, also the is attribute is checked
Returns
Number result The result of the comparison
See
GameState.toHash (static)
GameState.compare = function (gs1, gs2, strict) {
  if (!gs1 && !gs2) return 0;
  if (!gs2) return 1;
  if (!gs1) return -1;

  strict = strict || false;

Convert the parameters to objects, if an hash string was passed

  if ('string' === typeof gs1) gs1 = new GameState(gs1);
  if ('string' === typeof gs2) gs2 = new GameState(gs2);
  
  
  var result = gs1.state - gs2.state;
  
  if (result === 0 && 'undefined' !== typeof gs1.round) {
    result = gs1.round - gs2.round;
    
    if (result === 0 && 'undefined' !== typeof gs1.step) {
      result = gs1.step - gs2.step;
      
      if (strict && result === 0 && 'undefined' !== typeof gs1.is) {
        result = gs1.is - gs2.is;
      }
    }
  }
  
  
  
  return result;
};

GameState.stringify (static)

Converts an object GameState-like to its string representation

Params
gs GameState The object to convert to string
Returns
string out The string representation of a GameState object
GameState.stringify = function (gs) {
  if (!gs) return;
  var out = new GameState(gs).toHash('(r) S.s_i');
  if (gs.paused) out += ' [P]';
  return out;
}; 

Closure

})(
  'undefined' != typeof node ? node : module.exports
  , 'undefined' != typeof node ? node : module.parent.exports
);