forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObjectFactory.js
More file actions
160 lines (139 loc) · 4.04 KB
/
GameObjectFactory.js
File metadata and controls
160 lines (139 loc) · 4.04 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
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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../utils/Class');
var PluginManager = require('../boot/PluginManager');
/**
* @classdesc
* The Game Object Factory is a Scene plugin that allows you to quickly create many common
* types of Game Objects and have them automatically registered with the Scene.
*
* Game Objects directly register themselves with the Factory and inject their own creation
* methods into the class.
*
* @class GameObjectFactory
* @memberOf Phaser.GameObjects
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object Factory belongs.
*/
var GameObjectFactory = new Class({
initialize:
function GameObjectFactory (scene)
{
/**
* The Scene to which this Game Object Factory belongs.
*
* @name Phaser.GameObjects.GameObjectFactory#scene
* @type {Phaser.Scene}
* @protected
* @since 3.0.0
*/
this.scene = scene;
/**
* A reference to the Scene.Systems.
*
* @name Phaser.GameObjects.GameObjectFactory#systems
* @type {Phaser.Scenes.Systems}
* @protected
* @since 3.0.0
*/
this.systems = scene.sys;
if (!scene.sys.settings.isBooted)
{
scene.sys.events.once('boot', this.boot, this);
}
/**
* A reference to the Scene Display List.
*
* @name Phaser.GameObjects.GameObjectFactory#displayList
* @type {Phaser.GameObjects.DisplayList}
* @protected
* @since 3.0.0
*/
this.displayList;
/**
* A reference to the Scene Update List.
*
* @name Phaser.GameObjects.GameObjectFactory#updateList;
* @type {Phaser.GameObjects.UpdateList}
* @protected
* @since 3.0.0
*/
this.updateList;
},
/**
* Boots the plugin.
*
* @method Phaser.GameObjects.GameObjectFactory#boot
* @private
* @since 3.0.0
*/
boot: function ()
{
this.displayList = this.systems.displayList;
this.updateList = this.systems.updateList;
var eventEmitter = this.systems.events;
eventEmitter.on('shutdown', this.shutdown, this);
eventEmitter.on('destroy', this.destroy, this);
},
/**
* Adds an existing Game Object to this Scene.
*
* If the Game Object renders, it will be added to the Display List.
* If it has a `preUpdate` method, it will be added to the Update List.
*
* @method Phaser.GameObjects.GameObjectFactory#existing
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} child - The child to be added to this Scene.
*
* @return {Phaser.GameObjects.GameObject} The Game Object that was added.
*/
existing: function (child)
{
if (child.renderCanvas || child.renderWebGL)
{
this.displayList.add(child);
}
if (child.preUpdate)
{
this.updateList.add(child);
}
return child;
},
/**
* Shuts this plugin down.
*
* @method Phaser.GameObjects.GameObjectFactory#shutdown
* @since 3.0.0
*/
shutdown: function ()
{
},
/**
* Destroys this plugin.
*
* @method Phaser.GameObjects.GameObjectFactory#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.scene = null;
this.displayList = null;
this.updateList = null;
}
});
// Static method called directly by the Game Object factory functions
GameObjectFactory.register = function (type, factoryFunction)
{
if (!GameObjectFactory.prototype.hasOwnProperty(type))
{
GameObjectFactory.prototype[type] = factoryFunction;
}
};
PluginManager.register('GameObjectFactory', GameObjectFactory, 'add');
module.exports = GameObjectFactory;