¶ GameStateCopyright(c) 2012 Stefano Balietti MIT Licensed Representation of the state of a game:
See
GameLoop
---
|
(function (exports, node) {
|
¶ Global scope |
var JSUS = node.JSUS; |
Expose constructor |
exports.GameState = GameState;
GameState.defaults = {}; |
¶ GameState.defaults.hashDefault hash string for game-states @see GameState.toHash |
GameState.defaults.hash = 'S.s.r.i.p'; |
¶ GameState constructorCreates an instance of a GameState It accepts an object literal or an hash string as defined in 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.stateThe N-th game-block (state) in the game-loop currently being executed @see GameLoop |
this.state = 0; |
¶ GameState.stepThe N-th game-block (step) nested in the current state @see GameState.state |
this.step = 0; |
¶ GameState.roundThe number of times the current state was repeated |
this.round = 0;
|
¶ GameState.isThe state of the nodeGame engine @see node.is |
this.is = node.is.UNKNOWN;
|
¶ GameState.pausedTRUE 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.toStringConverts 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.toHashReturns 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
E.g.
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
If the strict parameter is set, also the The accepted hash string format is the following: 'S.s.r.i.p'.
Refer to 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
);
|