2017-07-11 15:48:25 +00:00
|
|
|
var Components = require('./components/');
|
2017-07-04 12:58:45 +00:00
|
|
|
var Class = require('../utils/Class');
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-07-04 12:58:45 +00:00
|
|
|
var GlobalStateManager = new Class({
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-07-04 12:58:45 +00:00
|
|
|
initialize:
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-07-04 12:58:45 +00:00
|
|
|
function GlobalStateManager (game, stateConfig)
|
|
|
|
{
|
|
|
|
this.game = game;
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-07-04 12:58:45 +00:00
|
|
|
// Everything kept in here
|
|
|
|
this.keys = {};
|
|
|
|
this.states = [];
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-07-11 15:48:25 +00:00
|
|
|
// Only active states are kept in here. They are moved here when started, and moved out when not.
|
|
|
|
// All states are stored in the states array, regardless of being active or not.
|
2017-07-04 12:58:45 +00:00
|
|
|
this.active = [];
|
|
|
|
|
2017-07-11 15:48:25 +00:00
|
|
|
// A state pending to be added to the State Manager is stored in here until the manager has time to add it.
|
2017-07-04 12:58:45 +00:00
|
|
|
this._pending = [];
|
|
|
|
|
2017-07-11 15:48:25 +00:00
|
|
|
// An array of states waiting to be started once the game has booted
|
|
|
|
this._start = [];
|
|
|
|
|
2017-07-04 12:58:45 +00:00
|
|
|
if (stateConfig)
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2017-07-04 12:58:45 +00:00
|
|
|
if (Array.isArray(stateConfig))
|
|
|
|
{
|
|
|
|
for (var i = 0; i < stateConfig.length; i++)
|
|
|
|
{
|
|
|
|
// The i === 0 part just starts the first State given
|
|
|
|
this._pending.push({
|
|
|
|
index: i,
|
|
|
|
key: 'default',
|
|
|
|
state: stateConfig[i],
|
|
|
|
autoStart: (i === 0),
|
|
|
|
data: {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-11-29 13:01:16 +00:00
|
|
|
{
|
2016-11-29 15:25:14 +00:00
|
|
|
this._pending.push({
|
2017-07-04 12:58:45 +00:00
|
|
|
index: 0,
|
2016-11-29 15:25:14 +00:00
|
|
|
key: 'default',
|
2017-07-04 12:58:45 +00:00
|
|
|
state: stateConfig,
|
|
|
|
autoStart: true,
|
2017-02-16 17:18:50 +00:00
|
|
|
data: {}
|
2016-11-29 15:25:14 +00:00
|
|
|
});
|
2016-11-29 13:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-04 12:58:45 +00:00
|
|
|
},
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-07-11 15:48:25 +00:00
|
|
|
add: Components.Add,
|
2017-07-11 17:18:31 +00:00
|
|
|
boot: Components.Boot,
|
|
|
|
bootState: Components.BootState,
|
|
|
|
bringToTop: Components.BringToTop,
|
|
|
|
create: Components.Create,
|
|
|
|
createStateDisplay: Components.CreateStateDisplay,
|
|
|
|
createStateFromFunction: Components.CreateStateFromFunction,
|
2017-07-11 15:48:25 +00:00
|
|
|
createStateFromInstance: Components.CreateStateFromInstance,
|
|
|
|
createStateFromObject: Components.CreateStateFromObject,
|
2017-07-11 17:18:31 +00:00
|
|
|
getActiveState: Components.GetActiveState,
|
|
|
|
getActiveStateIndex: Components.GetActiveStateIndex,
|
|
|
|
getActiveStateIndexByKey: Components.GetActiveStateIndexByKey,
|
|
|
|
getKey: Components.GetKey,
|
2017-07-11 15:48:25 +00:00
|
|
|
getState: Components.GetState,
|
|
|
|
getStateAt: Components.GetStateAt,
|
|
|
|
getStateIndex: Components.GetStateIndex,
|
|
|
|
getStateIndexByKey: Components.GetStateIndexByKey,
|
|
|
|
isActive: Components.IsActive,
|
2017-07-11 17:18:31 +00:00
|
|
|
isSleeping: Components.IsSleeping,
|
2017-07-11 15:48:25 +00:00
|
|
|
loadComplete: Components.LoadComplete,
|
2017-07-11 17:18:31 +00:00
|
|
|
moveDown: Components.MoveDown,
|
|
|
|
moveUp: Components.MoveUp,
|
2017-07-11 15:48:25 +00:00
|
|
|
pause: Components.Pause,
|
2017-07-11 17:18:31 +00:00
|
|
|
payloadComplete: Components.PayloadComplete,
|
2017-07-11 15:48:25 +00:00
|
|
|
resume: Components.Resume,
|
2017-07-11 17:18:31 +00:00
|
|
|
sendToBack: Components.SendToBack,
|
|
|
|
setupCallbacks: Components.SetupCallbacks,
|
2017-07-11 15:48:25 +00:00
|
|
|
sleep: Components.Sleep,
|
2017-07-11 17:18:31 +00:00
|
|
|
start: Components.Start,
|
2017-07-11 15:48:25 +00:00
|
|
|
stop: Components.Stop,
|
2017-07-11 17:18:31 +00:00
|
|
|
swap: Components.Swap,
|
2017-07-11 15:48:25 +00:00
|
|
|
swapPosition: Components.SwapPosition,
|
2017-07-11 17:18:31 +00:00
|
|
|
wake: Components.Wake
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-07-04 12:58:45 +00:00
|
|
|
});
|
2016-11-29 13:01:16 +00:00
|
|
|
|
2017-02-08 01:07:01 +00:00
|
|
|
module.exports = GlobalStateManager;
|