forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd.js
More file actions
89 lines (70 loc) · 2.5 KB
/
Copy pathAdd.js
File metadata and controls
89 lines (70 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var State = require('../State');
/**
* Adds a new State into the GlobalStateManager. You must give each State a unique key by which you'll identify it.
* The State can be either a Phaser.State object (or an object that extends it), a plain JavaScript object or a function.
* If a function is given a new state object will be created by calling it.
*
* @method Phaser.GlobalStateManager#add
* @param {string} key - A unique key you use to reference this state, i.e. "MainMenu", "Level1".
* @param {Phaser.State|object|function} state - The state you want to switch to.
* @param {boolean} [autoStart=false] - If true the State will be started immediately after adding it.
*/
var Add = function (key, stateConfig, autoStart)
{
if (autoStart === undefined) { autoStart = false; }
// if not booted, then put state into a holding pattern
if (!this.game.isBooted)
{
this._pending.push({
index: this._pending.length,
key: key,
state: stateConfig,
autoStart: autoStart
});
// console.log('GlobalStateManager not yet booted, adding to list', this._pending.length);
return;
}
// var ok = key;
key = this.getKey(key, stateConfig);
// console.group('GlobalStateManager.add');
// console.log('add key:', ok);
// console.log('config key:', key);
// console.log('config:', stateConfig);
// console.log('autoStart:', autoStart);
// console.groupEnd();
var newState;
if (stateConfig instanceof State)
{
// console.log('GlobalStateManager.add from instance:', key);
newState = this.createStateFromInstance(key, stateConfig);
}
else if (typeof stateConfig === 'object')
{
// console.log('GlobalStateManager.add from object:', key);
stateConfig.key = key;
newState = this.createStateFromObject(key, stateConfig);
}
else if (typeof stateConfig === 'function')
{
// console.log('GlobalStateManager.add from function:', key);
newState = this.createStateFromFunction(key, stateConfig);
}
// Replace key in case the state changed it
key = newState.sys.settings.key;
// console.log('replaced key', key);
this.keys[key] = newState;
this.states.push(newState);
if (autoStart || newState.sys.settings.active)
{
if (this.game.isBooted)
{
this.start(key);
}
else
{
this._start.push(key);
}
}
return newState;
};
module.exports = Add;