-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Collider.js
180 lines (158 loc) · 5.43 KB
/
Collider.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
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
/**
* @classdesc
* An Arcade Physics Collider will automatically check for collision, or overlaps, between two objects
* every step. If a collision, or overlap, occurs it will invoke the given callbacks.
*
* Note, if setting `overlapOnly` to `true`, and one of the objects is a `TilemapLayer`, every tile in the layer, regardless of tile ID, will be checked for collision.
* Even if the layer has had only a subset of tile IDs enabled for collision, all tiles will still be checked for overlap.
*
* @class Collider
* @memberof Phaser.Physics.Arcade
* @constructor
* @since 3.0.0
*
* @param {Phaser.Physics.Arcade.World} world - The Arcade physics World that will manage the collisions.
* @param {boolean} overlapOnly - Whether to check for collisions or overlaps.
* @param {Phaser.Types.Physics.Arcade.ArcadeColliderType} object1 - The first object to check for collision.
* @param {Phaser.Types.Physics.Arcade.ArcadeColliderType} object2 - The second object to check for collision.
* @param {Phaser.Types.Physics.Arcade.ArcadePhysicsCallback} collideCallback - The callback to invoke when the two objects collide.
* @param {Phaser.Types.Physics.Arcade.ArcadePhysicsCallback} processCallback - The callback to invoke when the two objects collide. Must return a boolean.
* @param {any} callbackContext - The scope in which to call the callbacks.
*/
var Collider = new Class({
initialize:
function Collider (world, overlapOnly, object1, object2, collideCallback, processCallback, callbackContext)
{
/**
* The world in which the bodies will collide.
*
* @name Phaser.Physics.Arcade.Collider#world
* @type {Phaser.Physics.Arcade.World}
* @since 3.0.0
*/
this.world = world;
/**
* The name of the collider (unused by Phaser).
*
* @name Phaser.Physics.Arcade.Collider#name
* @type {string}
* @since 3.1.0
*/
this.name = '';
/**
* Whether the collider is active.
*
* @name Phaser.Physics.Arcade.Collider#active
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.active = true;
/**
* Whether to check for collisions or overlaps.
*
* @name Phaser.Physics.Arcade.Collider#overlapOnly
* @type {boolean}
* @since 3.0.0
*/
this.overlapOnly = overlapOnly;
/**
* The first object to check for collision.
*
* @name Phaser.Physics.Arcade.Collider#object1
* @type {Phaser.Types.Physics.Arcade.ArcadeColliderType}
* @since 3.0.0
*/
this.object1 = object1;
/**
* The second object to check for collision.
*
* @name Phaser.Physics.Arcade.Collider#object2
* @type {Phaser.Types.Physics.Arcade.ArcadeColliderType}
* @since 3.0.0
*/
this.object2 = object2;
/**
* The callback to invoke when the two objects collide.
*
* @name Phaser.Physics.Arcade.Collider#collideCallback
* @type {Phaser.Types.Physics.Arcade.ArcadePhysicsCallback}
* @since 3.0.0
*/
this.collideCallback = collideCallback;
/**
* If a processCallback exists it must return true or collision checking will be skipped.
*
* @name Phaser.Physics.Arcade.Collider#processCallback
* @type {Phaser.Types.Physics.Arcade.ArcadePhysicsCallback}
* @since 3.0.0
*/
this.processCallback = processCallback;
/**
* The context the collideCallback and processCallback will run in.
*
* @name Phaser.Physics.Arcade.Collider#callbackContext
* @type {object}
* @since 3.0.0
*/
this.callbackContext = callbackContext;
},
/**
* A name for the Collider.
*
* Phaser does not use this value, it's for your own reference.
*
* @method Phaser.Physics.Arcade.Collider#setName
* @since 3.1.0
*
* @param {string} name - The name to assign to the Collider.
*
* @return {Phaser.Physics.Arcade.Collider} This Collider instance.
*/
setName: function (name)
{
this.name = name;
return this;
},
/**
* Called by World as part of its step processing, initial operation of collision checking.
*
* @method Phaser.Physics.Arcade.Collider#update
* @since 3.0.0
*/
update: function ()
{
this.world.collideObjects(
this.object1,
this.object2,
this.collideCallback,
this.processCallback,
this.callbackContext,
this.overlapOnly
);
},
/**
* Removes Collider from World and disposes of its resources.
*
* @method Phaser.Physics.Arcade.Collider#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.world.removeCollider(this);
this.active = false;
this.world = null;
this.object1 = null;
this.object2 = null;
this.collideCallback = null;
this.processCallback = null;
this.callbackContext = null;
}
});
module.exports = Collider;