/
GameObjectCreator.js
197 lines (175 loc) · 5.74 KB
/
GameObjectCreator.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var PluginCache = require('../plugins/PluginCache');
var SceneEvents = require('../scene/events');
/**
* @classdesc
* The Game Object Creator is a Scene plugin that allows you to quickly create many common
* types of Game Objects and return them. Unlike the Game Object Factory, they are not automatically
* added to the Scene.
*
* Game Objects directly register themselves with the Creator and inject their own creation
* methods into the class.
*
* @class GameObjectCreator
* @memberof Phaser.GameObjects
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object Factory belongs.
*/
var GameObjectCreator = new Class({
initialize:
function GameObjectCreator (scene)
{
/**
* The Scene to which this Game Object Creator belongs.
*
* @name Phaser.GameObjects.GameObjectCreator#scene
* @type {Phaser.Scene}
* @protected
* @since 3.0.0
*/
this.scene = scene;
/**
* A reference to the Scene.Systems.
*
* @name Phaser.GameObjects.GameObjectCreator#systems
* @type {Phaser.Scenes.Systems}
* @protected
* @since 3.0.0
*/
this.systems = scene.sys;
/**
* A reference to the Scene Event Emitter.
*
* @name Phaser.GameObjects.GameObjectCreator#events
* @type {Phaser.Events.EventEmitter}
* @protected
* @since 3.50.0
*/
this.events = scene.sys.events;
/**
* A reference to the Scene Display List.
*
* @name Phaser.GameObjects.GameObjectCreator#displayList
* @type {Phaser.GameObjects.DisplayList}
* @protected
* @since 3.0.0
*/
this.displayList;
/**
* A reference to the Scene Update List.
*
* @name Phaser.GameObjects.GameObjectCreator#updateList
* @type {Phaser.GameObjects.UpdateList}
* @protected
* @since 3.0.0
*/
this.updateList;
this.events.once(SceneEvents.BOOT, this.boot, this);
this.events.on(SceneEvents.START, this.start, this);
},
/**
* This method is called automatically, only once, when the Scene is first created.
* Do not invoke it directly.
*
* @method Phaser.GameObjects.GameObjectCreator#boot
* @private
* @since 3.5.1
*/
boot: function ()
{
this.displayList = this.systems.displayList;
this.updateList = this.systems.updateList;
this.events.once(SceneEvents.DESTROY, this.destroy, this);
},
/**
* This method is called automatically by the Scene when it is starting up.
* It is responsible for creating local systems, properties and listening for Scene events.
* Do not invoke it directly.
*
* @method Phaser.GameObjects.GameObjectCreator#start
* @private
* @since 3.5.0
*/
start: function ()
{
this.events.once(SceneEvents.SHUTDOWN, this.shutdown, this);
},
/**
* The Scene that owns this plugin is shutting down.
* We need to kill and reset all internal properties as well as stop listening to Scene events.
*
* @method Phaser.GameObjects.GameObjectCreator#shutdown
* @private
* @since 3.0.0
*/
shutdown: function ()
{
this.events.off(SceneEvents.SHUTDOWN, this.shutdown, this);
},
/**
* The Scene that owns this plugin is being destroyed.
* We need to shutdown and then kill off all external references.
*
* @method Phaser.GameObjects.GameObjectCreator#destroy
* @private
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
this.events.off(SceneEvents.START, this.start, this);
this.scene = null;
this.systems = null;
this.events = null;
this.displayList = null;
this.updateList = null;
}
});
/**
* Static method called directly by the Game Object creator functions.
* With this method you can register a custom GameObject factory in the GameObjectCreator,
* providing a name (`factoryType`) and the constructor (`factoryFunction`) in order
* to be called when you invoke Phaser.Scene.make[ factoryType ] method.
*
* @method Phaser.GameObjects.GameObjectCreator.register
* @static
* @since 3.0.0
*
* @param {string} factoryType - The key of the factory that you will use to call to Phaser.Scene.make[ factoryType ] method.
* @param {function} factoryFunction - The constructor function to be called when you invoke to the Phaser.Scene.make method.
*/
GameObjectCreator.register = function (factoryType, factoryFunction)
{
if (!GameObjectCreator.prototype.hasOwnProperty(factoryType))
{
GameObjectCreator.prototype[factoryType] = factoryFunction;
}
};
/**
* Static method called directly by the Game Object Creator functions.
*
* With this method you can remove a custom Game Object Creator that has been previously
* registered in the Game Object Creator. Pass in its `factoryType` in order to remove it.
*
* @method Phaser.GameObjects.GameObjectCreator.remove
* @static
* @since 3.0.0
*
* @param {string} factoryType - The key of the factory that you want to remove from the GameObjectCreator.
*/
GameObjectCreator.remove = function (factoryType)
{
if (GameObjectCreator.prototype.hasOwnProperty(factoryType))
{
delete GameObjectCreator.prototype[factoryType];
}
};
PluginCache.register('GameObjectCreator', GameObjectCreator, 'make');
module.exports = GameObjectCreator;