-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
PhysicsGroup.js
326 lines (291 loc) · 11.3 KB
/
PhysicsGroup.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var ArcadeSprite = require('./ArcadeSprite');
var Class = require('../../utils/Class');
var CollisionComponent = require('./components/Collision');
var CONST = require('./const');
var GetFastValue = require('../../utils/object/GetFastValue');
var Group = require('../../gameobjects/group/Group');
var IsPlainObject = require('../../utils/object/IsPlainObject');
/**
* @classdesc
* An Arcade Physics Group object.
*
* The primary use of a Physics Group is a way to collect together physics enable objects
* that share the same intrinsic structure into a single pool. They can they be easily
* compared against other Groups, or Game Objects.
*
* All Game Objects created by, or added to this Group will automatically be given **dynamic**
* Arcade Physics bodies (if they have no body already) and the bodies will receive the
* Groups {@link Phaser.Physics.Arcade.Group#defaults default values}.
*
* You should not pass objects into this Group that should not receive a body. For example,
* do not add basic Geometry or Tilemap Layers into a Group, as they will not behave in the
* way you may expect. Groups should all ideally have objects of the same type in them.
*
* If you wish to create a Group filled with Static Bodies, please see {@link Phaser.Physics.Arcade.StaticGroup}.
*
* @class Group
* @extends Phaser.GameObjects.Group
* @memberof Phaser.Physics.Arcade
* @constructor
* @since 3.0.0
*
* @extends Phaser.Physics.Arcade.Components.Collision
*
* @param {Phaser.Physics.Arcade.World} world - The physics simulation.
* @param {Phaser.Scene} scene - The scene this group belongs to.
* @param {(Phaser.GameObjects.GameObject[]|Phaser.Types.Physics.Arcade.PhysicsGroupConfig|Phaser.Types.GameObjects.Group.GroupCreateConfig)} [children] - Game Objects to add to this group; or the `config` argument.
* @param {Phaser.Types.Physics.Arcade.PhysicsGroupConfig|Phaser.Types.GameObjects.Group.GroupCreateConfig} [config] - Settings for this group.
*/
var PhysicsGroup = new Class({
Extends: Group,
Mixins: [
CollisionComponent
],
initialize:
function PhysicsGroup (world, scene, children, config)
{
if (!children && !config)
{
config = {
internalCreateCallback: this.createCallbackHandler,
internalRemoveCallback: this.removeCallbackHandler
};
}
else if (IsPlainObject(children))
{
// children is a plain object, so swizzle them:
config = children;
children = null;
config.internalCreateCallback = this.createCallbackHandler;
config.internalRemoveCallback = this.removeCallbackHandler;
}
else if (Array.isArray(children) && IsPlainObject(children[0]))
{
// children is an array of plain objects (i.e., configs)
var _this = this;
children.forEach(function (singleConfig)
{
singleConfig.internalCreateCallback = _this.createCallbackHandler;
singleConfig.internalRemoveCallback = _this.removeCallbackHandler;
singleConfig.classType = GetFastValue(singleConfig, 'classType', ArcadeSprite);
});
config = null;
}
else
{
// config is not defined and children is not a plain object nor an array of plain objects
config = {
internalCreateCallback: this.createCallbackHandler,
internalRemoveCallback: this.removeCallbackHandler
};
}
/**
* The physics simulation.
*
* @name Phaser.Physics.Arcade.Group#world
* @type {Phaser.Physics.Arcade.World}
* @since 3.0.0
*/
this.world = world;
/**
* The class to create new Group members from.
*
* This should be either `Phaser.Physics.Arcade.Image`, `Phaser.Physics.Arcade.Sprite`, or a class extending one of those.
*
* @name Phaser.Physics.Arcade.Group#classType
* @type {function}
* @default ArcadeSprite
* @since 3.0.0
* @see Phaser.Types.GameObjects.Group.GroupClassTypeConstructor
*/
if (config)
{
config.classType = GetFastValue(config, 'classType', ArcadeSprite);
}
/**
* The physics type of the Group's members.
*
* @name Phaser.Physics.Arcade.Group#physicsType
* @type {number}
* @default Phaser.Physics.Arcade.DYNAMIC_BODY
* @since 3.0.0
*/
this.physicsType = CONST.DYNAMIC_BODY;
/**
* The Arcade Physics Group Collision Category.
*
* This can be set to any valid collision bitfield value.
*
* See the `setCollisionCategory` method for more details.
*
* @name Phaser.Physics.Arcade.Group#collisionCategory
* @type {number}
* @since 3.70.0
*/
this.collisionCategory = 0x0001;
/**
* The Arcade Physics Group Collision Mask.
*
* See the `setCollidesWith` method for more details.
*
* @name Phaser.Physics.Arcade.Group#collisionMask
* @type {number}
* @since 3.70.0
*/
this.collisionMask = 1;
/**
* Default physics properties applied to Game Objects added to the Group or created by the Group. Derived from the `config` argument.
*
* You can remove the default values by setting this property to `{}`.
*
* @name Phaser.Physics.Arcade.Group#defaults
* @type {Phaser.Types.Physics.Arcade.PhysicsGroupDefaults}
* @since 3.0.0
*/
this.defaults = {
setCollideWorldBounds: GetFastValue(config, 'collideWorldBounds', false),
setBoundsRectangle: GetFastValue(config, 'customBoundsRectangle', null),
setAccelerationX: GetFastValue(config, 'accelerationX', 0),
setAccelerationY: GetFastValue(config, 'accelerationY', 0),
setAllowDrag: GetFastValue(config, 'allowDrag', true),
setAllowGravity: GetFastValue(config, 'allowGravity', true),
setAllowRotation: GetFastValue(config, 'allowRotation', true),
setDamping: GetFastValue(config, 'useDamping', false),
setBounceX: GetFastValue(config, 'bounceX', 0),
setBounceY: GetFastValue(config, 'bounceY', 0),
setDragX: GetFastValue(config, 'dragX', 0),
setDragY: GetFastValue(config, 'dragY', 0),
setEnable: GetFastValue(config, 'enable', true),
setGravityX: GetFastValue(config, 'gravityX', 0),
setGravityY: GetFastValue(config, 'gravityY', 0),
setFrictionX: GetFastValue(config, 'frictionX', 0),
setFrictionY: GetFastValue(config, 'frictionY', 0),
setMaxSpeed: GetFastValue(config, 'maxSpeed', -1),
setMaxVelocityX: GetFastValue(config, 'maxVelocityX', 10000),
setMaxVelocityY: GetFastValue(config, 'maxVelocityY', 10000),
setVelocityX: GetFastValue(config, 'velocityX', 0),
setVelocityY: GetFastValue(config, 'velocityY', 0),
setAngularVelocity: GetFastValue(config, 'angularVelocity', 0),
setAngularAcceleration: GetFastValue(config, 'angularAcceleration', 0),
setAngularDrag: GetFastValue(config, 'angularDrag', 0),
setMass: GetFastValue(config, 'mass', 1),
setImmovable: GetFastValue(config, 'immovable', false)
};
Group.call(this, scene, children, config);
/**
* A textual representation of this Game Object.
* Used internally by Phaser but is available for your own custom classes to populate.
*
* @name Phaser.Physics.Arcade.Group#type
* @type {string}
* @default 'PhysicsGroup'
* @since 3.21.0
*/
this.type = 'PhysicsGroup';
},
/**
* Enables a Game Object's Body and assigns `defaults`. Called when a Group member is added or created.
*
* @method Phaser.Physics.Arcade.Group#createCallbackHandler
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} child - The Game Object being added.
*/
createCallbackHandler: function (child)
{
if (!child.body)
{
this.world.enableBody(child, CONST.DYNAMIC_BODY);
}
var body = child.body;
for (var key in this.defaults)
{
body[key](this.defaults[key]);
}
},
/**
* Disables a Game Object's Body. Called when a Group member is removed.
*
* @method Phaser.Physics.Arcade.Group#removeCallbackHandler
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} child - The Game Object being removed.
*/
removeCallbackHandler: function (child)
{
if (child.body)
{
this.world.disableBody(child);
}
},
/**
* Sets the velocity of each Group member.
*
* @method Phaser.Physics.Arcade.Group#setVelocity
* @since 3.0.0
*
* @param {number} x - The horizontal velocity.
* @param {number} y - The vertical velocity.
* @param {number} [step=0] - The velocity increment. When set, the first member receives velocity (x, y), the second (x + step, y + step), and so on.
*
* @return {Phaser.Physics.Arcade.Group} This Physics Group object.
*/
setVelocity: function (x, y, step)
{
if (step === undefined) { step = 0; }
var items = this.getChildren();
for (var i = 0; i < items.length; i++)
{
items[i].body.velocity.set(x + (i * step), y + (i * step));
}
return this;
},
/**
* Sets the horizontal velocity of each Group member.
*
* @method Phaser.Physics.Arcade.Group#setVelocityX
* @since 3.0.0
*
* @param {number} value - The velocity value.
* @param {number} [step=0] - The velocity increment. When set, the first member receives velocity (x), the second (x + step), and so on.
*
* @return {Phaser.Physics.Arcade.Group} This Physics Group object.
*/
setVelocityX: function (value, step)
{
if (step === undefined) { step = 0; }
var items = this.getChildren();
for (var i = 0; i < items.length; i++)
{
items[i].body.velocity.x = value + (i * step);
}
return this;
},
/**
* Sets the vertical velocity of each Group member.
*
* @method Phaser.Physics.Arcade.Group#setVelocityY
* @since 3.0.0
*
* @param {number} value - The velocity value.
* @param {number} [step=0] - The velocity increment. When set, the first member receives velocity (y), the second (y + step), and so on.
*
* @return {Phaser.Physics.Arcade.Group} This Physics Group object.
*/
setVelocityY: function (value, step)
{
if (step === undefined) { step = 0; }
var items = this.getChildren();
for (var i = 0; i < items.length; i++)
{
items[i].body.velocity.y = value + (i * step);
}
return this;
}
});
module.exports = PhysicsGroup;