@makefully/platypus 4.1.4 → 4.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +4 -2
- package/lib/platypus.js +1 -1
- package/package.json +7 -2
- package/src/AABB.js +685 -0
- package/src/ActionState.js +121 -0
- package/src/AssetManager.js +206 -0
- package/src/CollisionData.js +96 -0
- package/src/CollisionDataContainer.js +127 -0
- package/src/CollisionShape.js +315 -0
- package/src/Component.js +231 -0
- package/src/Data.js +64 -0
- package/src/DataMap.js +204 -0
- package/src/Entity.js +404 -0
- package/src/EntityLinker.js +113 -0
- package/src/EventHandler.js +115 -0
- package/src/EventHandlerList.js +251 -0
- package/src/Game.js +840 -0
- package/src/Gamepad.js +175 -0
- package/src/ID3CaptionPlayer.js +61 -0
- package/src/Messenger.js +365 -0
- package/src/PIXIAnimation.js +622 -0
- package/src/RandomSet.js +140 -0
- package/src/SFXPlayer.js +120 -0
- package/src/StateMap.js +252 -0
- package/src/Storage.js +152 -0
- package/src/TickerClient.js +48 -0
- package/src/TickerServer.js +23 -0
- package/src/TimeEvent.js +50 -0
- package/src/TimeEventList.js +100 -0
- package/src/VOPlayer.js +684 -0
- package/src/Vector.js +702 -0
- package/src/components/AIChaser.js +161 -0
- package/src/components/AIPacer.js +107 -0
- package/src/components/AudioMusic.js +151 -0
- package/src/components/AudioSFX.js +673 -0
- package/src/components/AudioVO.js +301 -0
- package/src/components/CollisionBasic.js +826 -0
- package/src/components/CollisionFilter.js +74 -0
- package/src/components/CollisionGroup.js +448 -0
- package/src/components/CollisionTiles.js +608 -0
- package/src/components/ComponentSwitcher.js +173 -0
- package/src/components/ControllerInput.js +65 -0
- package/src/components/Counter.js +165 -0
- package/src/components/EntityCamera.js +215 -0
- package/src/components/EntityContainer.js +549 -0
- package/src/components/EntityController.js +541 -0
- package/src/components/HandlerCamera.js +1052 -0
- package/src/components/HandlerCollision.js +1318 -0
- package/src/components/HandlerController.js +291 -0
- package/src/components/HandlerLogic.js +394 -0
- package/src/components/HandlerRender.js +217 -0
- package/src/components/Interactive.js +502 -0
- package/src/components/LevelBuilder.js +524 -0
- package/src/components/LogicAngularMovement.js +154 -0
- package/src/components/LogicAttachment.js +265 -0
- package/src/components/LogicButton.js +466 -0
- package/src/components/LogicCarrier.js +39 -0
- package/src/components/LogicDestroyMe.js +58 -0
- package/src/components/LogicDirectionalMovement.js +325 -0
- package/src/components/LogicDragDrop.js +371 -0
- package/src/components/LogicImpactLaunch.js +144 -0
- package/src/components/LogicPacingPlatform.js +64 -0
- package/src/components/LogicPortable.js +112 -0
- package/src/components/LogicPortal.js +115 -0
- package/src/components/LogicPushable.js +98 -0
- package/src/components/LogicRebounder.js +136 -0
- package/src/components/LogicRegionSpawner.js +139 -0
- package/src/components/LogicRotationalMovement.js +205 -0
- package/src/components/LogicSpawner.js +159 -0
- package/src/components/LogicStateMachine.js +382 -0
- package/src/components/LogicSwitch.js +77 -0
- package/src/components/LogicTeleportee.js +68 -0
- package/src/components/LogicTeleporter.js +93 -0
- package/src/components/LogicTimer.js +178 -0
- package/src/components/LogicWindUpRacer.js +179 -0
- package/src/components/Motion.js +371 -0
- package/src/components/Mover.js +566 -0
- package/src/components/Node.js +289 -0
- package/src/components/NodeMap.js +295 -0
- package/src/components/NodeResident.js +636 -0
- package/src/components/Orientation.js +697 -0
- package/src/components/RandomEvents.js +73 -0
- package/src/components/RelativePosition.js +204 -0
- package/src/components/RelayFamily.js +106 -0
- package/src/components/RelayGame.js +53 -0
- package/src/components/RelayLinker.js +94 -0
- package/src/components/RelayParent.js +57 -0
- package/src/components/RelaySelf.js +55 -0
- package/src/components/Render.js +178 -0
- package/src/components/RenderAnimator.js +275 -0
- package/src/components/RenderContainer.js +646 -0
- package/src/components/RenderDebug.js +319 -0
- package/src/components/RenderDestroyMe.js +59 -0
- package/src/components/RenderLottie.js +149 -0
- package/src/components/RenderProgress.js +120 -0
- package/src/components/RenderSpine.js +833 -0
- package/src/components/RenderSprite.js +527 -0
- package/src/components/RenderText.js +147 -0
- package/src/components/RenderTiles.js +1391 -0
- package/src/components/SceneChanger.js +128 -0
- package/src/components/TiledLoader.js +1451 -0
- package/src/components/Timeline.js +314 -0
- package/src/components/Tutorial.js +267 -0
- package/src/components/Tween.js +296 -0
- package/src/components/VoiceOver.js +373 -0
- package/src/components/XHR.js +129 -0
- package/src/components/index.js +79 -0
- package/src/factory.js +305 -0
- package/src/index.js +159 -0
- package/src/mixins/PIXI.Graphics.js +41 -0
- package/src/platypus.css +68 -0
- package/src/sayHello.js +50 -0
- package/src/shapes/Capsule.js +74 -0
- package/src/shapes/Circle.js +43 -0
- package/src/shapes/Ellipse.js +117 -0
- package/src/shapes/Point.js +191 -0
- package/src/shapes/Polygon.js +112 -0
- package/src/shapes/Polyline.js +97 -0
- package/src/shapes/Rectangle.js +78 -0
- package/src/shapes/RoundedRectangle.js +33 -0
- package/src/shapes/Segment.js +346 -0
- package/src/shapes/adjustedXY.js +19 -0
- package/src/shapes/castFromBox2D.js +92 -0
- package/src/shapes/castFromObject.js +53 -0
- package/src/shapes/castFromPixiGraphics.js +6 -0
- package/src/shapes/castFromPixiShape.js +89 -0
- package/src/shapes/castToBox2D.js +190 -0
- package/src/shapes/castToPixiGraphics.js +100 -0
- package/src/shapes/castToPixiShape.js +61 -0
- package/src/shapes/checkPointsMap.js +313 -0
- package/src/shapes/index.js +35 -0
- package/src/utils/array.js +73 -0
- package/src/utils/string.js +39 -0
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
/* global platypus */
|
|
2
|
+
import {arrayCache, greenSlice, greenSplice, union} from '../utils/array.js';
|
|
3
|
+
import Data from '../Data.js';
|
|
4
|
+
import Entity from '../Entity.js';
|
|
5
|
+
import Messenger from '../Messenger.js';
|
|
6
|
+
import createComponentClass from '../factory.js';
|
|
7
|
+
|
|
8
|
+
const
|
|
9
|
+
childBroadcast = function (event) {
|
|
10
|
+
return function (value, debug) {
|
|
11
|
+
this.triggerOnChildren(event, value, debug);
|
|
12
|
+
};
|
|
13
|
+
},
|
|
14
|
+
EntityContainer = createComponentClass(/** @lends platypus.components.EntityContainer.prototype */{
|
|
15
|
+
id: 'EntityContainer',
|
|
16
|
+
|
|
17
|
+
properties: {
|
|
18
|
+
/**
|
|
19
|
+
* An Array listing messages that are triggered on the entity and should be triggered on the children as well.
|
|
20
|
+
*
|
|
21
|
+
* @property childEvents
|
|
22
|
+
* @type Array
|
|
23
|
+
* @default []
|
|
24
|
+
*/
|
|
25
|
+
childEvents: []
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* This component allows the entity to contain child entities. It will add several methods to the entity to manage adding and removing entities.
|
|
30
|
+
*
|
|
31
|
+
* @memberof platypus.components
|
|
32
|
+
* @extends platypus.Messenger
|
|
33
|
+
* @uses platypus.Component
|
|
34
|
+
* @constructs
|
|
35
|
+
* @listens platypus.Entity#add-entity
|
|
36
|
+
* @listens platypus.Entity#child-entity-updated
|
|
37
|
+
* @listens platypus.Entity#handle-logic
|
|
38
|
+
* @listens platypus.Entity#remove-entity
|
|
39
|
+
*/
|
|
40
|
+
initialize: function (definition, callback) {
|
|
41
|
+
const
|
|
42
|
+
//combine component list and entity list into one if they both exist.
|
|
43
|
+
owner = this.owner,
|
|
44
|
+
entities = (definition.entities && owner.entities) ? definition.entities.concat(owner.entities) : (definition.entities ?? owner.entities ?? null),
|
|
45
|
+
events = this.childEvents;
|
|
46
|
+
|
|
47
|
+
Messenger.initialize(this);
|
|
48
|
+
|
|
49
|
+
this.newAdds = arrayCache.setUp();
|
|
50
|
+
this._childEvents = {};
|
|
51
|
+
|
|
52
|
+
owner.entities = this.entities = arrayCache.setUp();
|
|
53
|
+
|
|
54
|
+
this.childEvents = arrayCache.setUp();
|
|
55
|
+
for (let i = 0; i < events.length; i++) {
|
|
56
|
+
this.addNewPublicEvent(events[i]);
|
|
57
|
+
}
|
|
58
|
+
this.addNewPrivateEvent('peer-entity-added');
|
|
59
|
+
this.addNewPrivateEvent('peer-entity-removed');
|
|
60
|
+
|
|
61
|
+
if (entities) {
|
|
62
|
+
Promise.all(entities.map((entity) => new Promise((resolve) => this.addEntity(entity, resolve)))).then(callback);
|
|
63
|
+
return true; // notifies owner that this component is asynchronous.
|
|
64
|
+
} else {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
events: {
|
|
70
|
+
/**
|
|
71
|
+
* This message will added the given entity to this component's list of entities.
|
|
72
|
+
*
|
|
73
|
+
* @event platypus.Entity#add-entity
|
|
74
|
+
* @param entity {platypus.Entity} This is the entity to be added as a child.
|
|
75
|
+
* @param [callback] {Function} A function to run once all of the components on the Entity have been loaded.
|
|
76
|
+
*/
|
|
77
|
+
"add-entity": function (entity, callback) {
|
|
78
|
+
this.addEntity(entity, callback);
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* On receiving this message, the provided entity will be removed from the list of child entities.
|
|
83
|
+
*
|
|
84
|
+
* @method platypus.Entity#remove-entity
|
|
85
|
+
* @param entity {platypus.Entity} The entity to remove.
|
|
86
|
+
*/
|
|
87
|
+
"remove-entity": function (entity) {
|
|
88
|
+
this.removeEntity(entity);
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
"child-entity-updated": function (entity) {
|
|
92
|
+
this.updateChildEventListeners(entity);
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
"handle-logic": function () {
|
|
96
|
+
const
|
|
97
|
+
adds = this.newAdds,
|
|
98
|
+
l = adds.length;
|
|
99
|
+
|
|
100
|
+
if (l) {
|
|
101
|
+
const
|
|
102
|
+
removals = arrayCache.setUp();
|
|
103
|
+
let j = 0;
|
|
104
|
+
|
|
105
|
+
//must go in order so entities are added in the expected order.
|
|
106
|
+
for (let i = 0; i < l; i++) {
|
|
107
|
+
const
|
|
108
|
+
adding = adds[i];
|
|
109
|
+
|
|
110
|
+
if (adding.destroyed || !adding.loadingComponents) {
|
|
111
|
+
removals.push(i);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
j = removals.length;
|
|
116
|
+
while (j--) {
|
|
117
|
+
greenSplice(adds, removals[j]);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
arrayCache.recycle(removals);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
methods: {
|
|
126
|
+
addNewPublicEvent: function (event) {
|
|
127
|
+
this.addNewPrivateEvent(event);
|
|
128
|
+
|
|
129
|
+
for (let i = 0; i < this.childEvents.length; i++) {
|
|
130
|
+
if (this.childEvents[i] === event) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
this.childEvents.push(event);
|
|
135
|
+
// Listens for specified messages and on receiving them, re-triggers them on child entities.
|
|
136
|
+
this.addEventListener(event, childBroadcast(event));
|
|
137
|
+
|
|
138
|
+
return true;
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
addNewPrivateEvent (event) {
|
|
142
|
+
if (this._childEvents[event]) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this._childEvents[event] = true;
|
|
147
|
+
|
|
148
|
+
// Listen for message on children
|
|
149
|
+
for (let x = 0; x < this.entities.length; x++) {
|
|
150
|
+
const
|
|
151
|
+
listenerList = this.entities[x]._listeners[event];
|
|
152
|
+
|
|
153
|
+
if (listenerList) {
|
|
154
|
+
const
|
|
155
|
+
{handlers} = listenerList;
|
|
156
|
+
|
|
157
|
+
for (let y = 0; y < handlers.length; y++) {
|
|
158
|
+
this.addChildEventListener(
|
|
159
|
+
this.entities[x],
|
|
160
|
+
event,
|
|
161
|
+
handlers[y]
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return true;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
updateChildEventListeners: function (entity) {
|
|
171
|
+
this.removeChildEventListeners(entity);
|
|
172
|
+
this.addChildEventListeners(entity);
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
addChildEventListeners (entity) {
|
|
176
|
+
const
|
|
177
|
+
{_listeners} = entity,
|
|
178
|
+
keys = Object.keys(_listeners),
|
|
179
|
+
{length} = keys;
|
|
180
|
+
|
|
181
|
+
for (let i = 0; i < length; i++) {
|
|
182
|
+
const
|
|
183
|
+
key = keys[i],
|
|
184
|
+
listenerList = _listeners[key];
|
|
185
|
+
|
|
186
|
+
if (listenerList) {
|
|
187
|
+
const
|
|
188
|
+
{handlers} = listenerList;
|
|
189
|
+
|
|
190
|
+
for (let j = 0; j < handlers.length; j++) {
|
|
191
|
+
this.addChildEventListener(
|
|
192
|
+
entity,
|
|
193
|
+
key,
|
|
194
|
+
handlers[j]
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
removeChildEventListeners: function (entity) {
|
|
202
|
+
if (entity.containerListener?.events) {
|
|
203
|
+
const
|
|
204
|
+
{events, handlers} = entity.containerListener,
|
|
205
|
+
{length} = events;
|
|
206
|
+
|
|
207
|
+
for (let i = 0; i < length; i++) {
|
|
208
|
+
this.off(
|
|
209
|
+
events[i],
|
|
210
|
+
handlers[i].callback,
|
|
211
|
+
handlers[i].context
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
arrayCache.recycle(events);
|
|
216
|
+
arrayCache.recycle(handlers);
|
|
217
|
+
|
|
218
|
+
entity.containerListener.recycle();
|
|
219
|
+
entity.containerListener = null;
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
|
|
223
|
+
addChildEventListener: function (entity, event, handler) {
|
|
224
|
+
if (!entity.containerListener) {
|
|
225
|
+
entity.containerListener = Data.setUp(
|
|
226
|
+
"events", arrayCache.setUp(),
|
|
227
|
+
"handlers", arrayCache.setUp()
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const
|
|
232
|
+
newHandler = this.on(
|
|
233
|
+
event,
|
|
234
|
+
handler.callback,
|
|
235
|
+
handler.context,
|
|
236
|
+
handler.once,
|
|
237
|
+
handler.priority
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
if (newHandler) {
|
|
241
|
+
entity.containerListener.events.push(event);
|
|
242
|
+
entity.containerListener.handlers.push(newHandler);
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
removeChildEventListener: function (entity, event, callback) {
|
|
247
|
+
const
|
|
248
|
+
{events, handlers} = entity.containerListener;
|
|
249
|
+
let i = events.length;
|
|
250
|
+
|
|
251
|
+
while (i--) {
|
|
252
|
+
if (
|
|
253
|
+
events[i] === event &&
|
|
254
|
+
(
|
|
255
|
+
!callback ||
|
|
256
|
+
handlers[i].callback === callback
|
|
257
|
+
)
|
|
258
|
+
) {
|
|
259
|
+
this.off(
|
|
260
|
+
event,
|
|
261
|
+
handlers[i].callback,
|
|
262
|
+
handlers[i].context
|
|
263
|
+
);
|
|
264
|
+
greenSplice(events, i);
|
|
265
|
+
greenSplice(handlers, i);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
destroy: function () {
|
|
271
|
+
const
|
|
272
|
+
entities = greenSlice(this.entities); // Make a copy to handle entities being destroyed while processing list.
|
|
273
|
+
let i = entities.length;
|
|
274
|
+
|
|
275
|
+
while (i--) {
|
|
276
|
+
const
|
|
277
|
+
entity = entities[i];
|
|
278
|
+
|
|
279
|
+
this.removeChildEventListeners(entity);
|
|
280
|
+
entity.destroy();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
arrayCache.recycle(entities);
|
|
284
|
+
arrayCache.recycle(this.entities);
|
|
285
|
+
this.owner.entities = null;
|
|
286
|
+
arrayCache.recycle(this.childEvents);
|
|
287
|
+
this.childEvents = null;
|
|
288
|
+
arrayCache.recycle(this.newAdds);
|
|
289
|
+
this.newAdds = null;
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
|
|
293
|
+
publicMethods: {
|
|
294
|
+
/**
|
|
295
|
+
* Gets an entity in this layer by its Id. Returns `null` if not found.
|
|
296
|
+
*
|
|
297
|
+
* @method platypus.components.EntityContainer#getEntityById
|
|
298
|
+
* @param {String} id
|
|
299
|
+
* @return {Entity}
|
|
300
|
+
*/
|
|
301
|
+
getEntityById: function (id) {
|
|
302
|
+
const
|
|
303
|
+
entity = this.entities.filter((entity) => entity.id === id)[0] ?? this.newAdds.filter((entity) => entity.id === id)[0];
|
|
304
|
+
|
|
305
|
+
if (entity) {
|
|
306
|
+
return entity;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
for (let i = 0; i < this.entities.length; i++) {
|
|
310
|
+
const
|
|
311
|
+
selection = this.entities[i].getEntityById?.(id);
|
|
312
|
+
|
|
313
|
+
if (selection) {
|
|
314
|
+
return selection;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return null;
|
|
318
|
+
},
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Returns a list of entities of the requested type.
|
|
322
|
+
*
|
|
323
|
+
* @method platypus.components.EntityContainer#getEntitiesByType
|
|
324
|
+
* @param {String} type
|
|
325
|
+
* @return {Array}
|
|
326
|
+
*/
|
|
327
|
+
getEntitiesByType: function (type) {
|
|
328
|
+
const
|
|
329
|
+
entities = arrayCache.setUp();
|
|
330
|
+
|
|
331
|
+
for (let i = 0; i < this.entities.length; i++) {
|
|
332
|
+
if (this.entities[i].type === type) {
|
|
333
|
+
entities.push(this.entities[i]);
|
|
334
|
+
}
|
|
335
|
+
if (this.entities[i].getEntitiesByType) {
|
|
336
|
+
const
|
|
337
|
+
selection = this.entities[i].getEntitiesByType(type);
|
|
338
|
+
|
|
339
|
+
union(entities, selection);
|
|
340
|
+
arrayCache.recycle(selection);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return entities;
|
|
344
|
+
},
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* This method adds an entity to the owner's group. If an entity definition or a reference to an entity definition is provided, the entity is created and then added to the owner's group.
|
|
348
|
+
*
|
|
349
|
+
* @method platypus.components.EntityContainer#addEntity
|
|
350
|
+
* @param newEntity {platypus.Entity|Object|String} Specifies the entity to add. If an object with a "type" property is provided or a String is provided, this component looks up the entity definition to create the entity.
|
|
351
|
+
* @param [newEntity.type] {String} If an object with a "type" property is provided, this component looks up the entity definition to create the entity.
|
|
352
|
+
* @param [newEntity.properties] {Object} A list of key/value pairs that sets the initial properties on the new entity.
|
|
353
|
+
* @param [callback] {Function} A function to run once all of the components on the Entity have been loaded.
|
|
354
|
+
* @return {platypus.Entity} The entity that was just added.
|
|
355
|
+
* @fires platypus.Entity#child-entity-added
|
|
356
|
+
* @fires platypus.Entity#entity-created
|
|
357
|
+
* @fires platypus.Entity#peer-entity-added
|
|
358
|
+
*/
|
|
359
|
+
addEntity (newEntity, callback) {
|
|
360
|
+
const
|
|
361
|
+
{owner} = this,
|
|
362
|
+
whenReady = (entity) => {
|
|
363
|
+
const
|
|
364
|
+
{owner, entities} = this;
|
|
365
|
+
let i = entities.length;
|
|
366
|
+
|
|
367
|
+
entity.triggerEvent('adopted', entity);
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* This message is triggered when a new entity has been added to the parent's list of children entities.
|
|
371
|
+
*
|
|
372
|
+
* @event platypus.Entity#peer-entity-added
|
|
373
|
+
* @param {platypus.Entity} entity The entity that was just added.
|
|
374
|
+
*/
|
|
375
|
+
while (i--) {
|
|
376
|
+
if (!entity.triggerEvent('peer-entity-added', entities[i])) {
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
this.triggerEventOnChildren('peer-entity-added', entity);
|
|
381
|
+
|
|
382
|
+
this.addChildEventListeners(entity);
|
|
383
|
+
entities.push(entity);
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* This message is triggered when a new entity has been added to the list of children entities.
|
|
387
|
+
*
|
|
388
|
+
* @event platypus.Entity#child-entity-added
|
|
389
|
+
* @param {platypus.Entity} entity The entity that was just added.
|
|
390
|
+
*/
|
|
391
|
+
owner.triggerEvent('child-entity-added', entity);
|
|
392
|
+
|
|
393
|
+
if (callback) {
|
|
394
|
+
callback(entity);
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
let entity = null;
|
|
398
|
+
|
|
399
|
+
if (newEntity instanceof Entity) {
|
|
400
|
+
entity = newEntity;
|
|
401
|
+
entity.parent = owner;
|
|
402
|
+
whenReady(entity);
|
|
403
|
+
} else {
|
|
404
|
+
if (typeof newEntity === 'string') {
|
|
405
|
+
const
|
|
406
|
+
entityDefinition = platypus.game.settings.entities[newEntity];
|
|
407
|
+
|
|
408
|
+
if (entityDefinition) {
|
|
409
|
+
entity = new Entity(entityDefinition, {}, whenReady, owner);
|
|
410
|
+
} else {
|
|
411
|
+
throw new Error(`EntityContainer: There is no entity defined for type "${newEntity}".`);
|
|
412
|
+
}
|
|
413
|
+
} else if (newEntity.id) {
|
|
414
|
+
entity = new Entity(newEntity, {}, whenReady, owner);
|
|
415
|
+
} else {
|
|
416
|
+
const
|
|
417
|
+
entityDefinition = platypus.game.settings.entities[newEntity.type];
|
|
418
|
+
|
|
419
|
+
if (entityDefinition) {
|
|
420
|
+
entity = new Entity(entityDefinition, newEntity, whenReady, owner);
|
|
421
|
+
} else {
|
|
422
|
+
throw new Error(`EntityContainer: There is no entity defined for type "${newEntity.type}".`);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Called when this entity spawns a new entity, this event links the newly created entity to this entity.
|
|
428
|
+
*
|
|
429
|
+
* @event platypus.Entity#entity-created
|
|
430
|
+
* @param entity {platypus.Entity} The entity to link.
|
|
431
|
+
*/
|
|
432
|
+
this.owner.triggerEvent('entity-created', entity);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
this.newAdds.push(entity);
|
|
436
|
+
|
|
437
|
+
return entity;
|
|
438
|
+
},
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Removes the provided entity from the layer and destroys it. Returns `false` if the entity is not found in the layer.
|
|
442
|
+
*
|
|
443
|
+
* @method platypus.components.EntityContainer#removeEntity
|
|
444
|
+
* @param {Entity} entity
|
|
445
|
+
* @return {Entity}
|
|
446
|
+
* @fires platypus.Entity#child-entity-removed
|
|
447
|
+
* @fires platypus.Entity#peer-entity-removed
|
|
448
|
+
*/
|
|
449
|
+
removeEntity: function (entity) {
|
|
450
|
+
const
|
|
451
|
+
i = this.entities.indexOf(entity);
|
|
452
|
+
|
|
453
|
+
if (i >= 0) {
|
|
454
|
+
this.removeChildEventListeners(entity);
|
|
455
|
+
greenSplice(this.entities, i);
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* This message is triggered when an entity has been removed from the parent's list of children entities.
|
|
459
|
+
*
|
|
460
|
+
* @event platypus.Entity#peer-entity-removed
|
|
461
|
+
* @param {platypus.Entity} entity The entity that was just removed.
|
|
462
|
+
*/
|
|
463
|
+
this.triggerEventOnChildren('peer-entity-removed', entity);
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* This message is triggered when an entity has been removed from the list of children entities.
|
|
467
|
+
*
|
|
468
|
+
* @event platypus.Entity#child-entity-removed
|
|
469
|
+
* @param {platypus.Entity} entity The entity that was just added.
|
|
470
|
+
*/
|
|
471
|
+
this.owner.triggerEvent('child-entity-removed', entity);
|
|
472
|
+
entity.destroy();
|
|
473
|
+
entity.parent = null;
|
|
474
|
+
return entity;
|
|
475
|
+
}
|
|
476
|
+
return false;
|
|
477
|
+
},
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Triggers a single event on the child entities in the layer.
|
|
481
|
+
*
|
|
482
|
+
* @method platypus.components.EntityContainer#triggerEventOnChildren
|
|
483
|
+
* @param {*} event
|
|
484
|
+
* @param {*} message
|
|
485
|
+
* @param {*} debug
|
|
486
|
+
*/
|
|
487
|
+
triggerEventOnChildren: function (event, message, debug) {
|
|
488
|
+
if (this.destroyed) {
|
|
489
|
+
return 0;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (!this._childEvents[event]) {
|
|
493
|
+
this.addNewPrivateEvent(event);
|
|
494
|
+
}
|
|
495
|
+
return this.triggerEvent(event, message, debug);
|
|
496
|
+
},
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Triggers one or more events on the child entities in the layer. This is unique from `triggerEventOnChildren` in that it also accepts an `Array` to send multiple events.
|
|
500
|
+
*
|
|
501
|
+
* @method platypus.components.EntityContainer#triggerOnChildren
|
|
502
|
+
* @param {*} event
|
|
503
|
+
* @param {*} message
|
|
504
|
+
* @param {*} debug
|
|
505
|
+
*/
|
|
506
|
+
triggerOnChildren: function (event) {
|
|
507
|
+
if (this.destroyed) {
|
|
508
|
+
return 0;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
if (!this._childEvents[event]) {
|
|
512
|
+
this.addNewPrivateEvent(event);
|
|
513
|
+
}
|
|
514
|
+
return this.trigger.apply(this, arguments);
|
|
515
|
+
}
|
|
516
|
+
},
|
|
517
|
+
|
|
518
|
+
getAssetList: function (def, props, defaultProps, data) {
|
|
519
|
+
const
|
|
520
|
+
assets = arrayCache.setUp(),
|
|
521
|
+
entities = arrayCache.setUp();
|
|
522
|
+
|
|
523
|
+
if (def.entities) {
|
|
524
|
+
union(entities, def.entities);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (props && props.entities) {
|
|
528
|
+
union(entities, props.entities);
|
|
529
|
+
} else if (defaultProps && defaultProps.entities) {
|
|
530
|
+
union(entities, defaultProps.entities);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
for (let i = 0; i < entities.length; i++) {
|
|
534
|
+
const
|
|
535
|
+
arr = Entity.getAssetList(entities[i], null, data);
|
|
536
|
+
|
|
537
|
+
union(assets, arr);
|
|
538
|
+
arrayCache.recycle(arr);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
arrayCache.recycle(entities);
|
|
542
|
+
|
|
543
|
+
return assets;
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
Messenger.mixin(EntityContainer);
|
|
548
|
+
|
|
549
|
+
export default EntityContainer;
|