-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Collision.js
151 lines (130 loc) · 4.29 KB
/
Collision.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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var GetCollidesWith = require('../GetCollidesWith');
/**
* Provides methods used for setting the collision category and mask of an Arcade Physics Body.
*
* @namespace Phaser.Physics.Arcade.Components.Collision
* @since 3.70.0
*/
var Collision = {
/**
* Sets the Collision Category that this Arcade Physics Body
* will use in order to determine what it can collide with.
*
* It can only have one single category assigned to it.
*
* If you wish to reset the collision category and mask, call
* the `resetCollisionCategory` method.
*
* @method Phaser.Physics.Arcade.Components.Collision#setCollisionCategory
* @since 3.70.0
*
* @param {number} category - The collision category.
*
* @return {this} This Game Object.
*/
setCollisionCategory: function (category)
{
var target = (this.body) ? this.body : this;
target.collisionCategory = category;
return this;
},
/**
* Checks to see if the given Collision Category will collide with
* this Arcade Physics object or not.
*
* @method Phaser.Physics.Arcade.Components.Collision#willCollideWith
* @since 3.70.0
*
* @param {number} category - Collision category value to test.
*
* @return {boolean} `true` if the given category will collide with this object, otherwise `false`.
*/
willCollideWith: function (category)
{
var target = (this.body) ? this.body : this;
return (target.collisionMask & category) !== 0;
},
/**
* Adds the given Collision Category to the list of those that this
* Arcade Physics Body will collide with.
*
* @method Phaser.Physics.Arcade.Components.Collision#addCollidesWith
* @since 3.70.0
*
* @param {number} category - The collision category to add.
*
* @return {this} This Game Object.
*/
addCollidesWith: function (category)
{
var target = (this.body) ? this.body : this;
target.collisionMask = target.collisionMask | category;
return this;
},
/**
* Removes the given Collision Category from the list of those that this
* Arcade Physics Body will collide with.
*
* @method Phaser.Physics.Arcade.Components.Collision#removeCollidesWith
* @since 3.70.0
*
* @param {number} category - The collision category to add.
*
* @return {this} This Game Object.
*/
removeCollidesWith: function (category)
{
var target = (this.body) ? this.body : this;
target.collisionMask = target.collisionMask & ~category;
return this;
},
/**
* Sets all of the Collision Categories that this Arcade Physics Body
* will collide with. You can either pass a single category value, or
* an array of them.
*
* Calling this method will reset all of the collision categories,
* so only those passed to this method are enabled.
*
* If you wish to add a new category to the existing mask, call
* the `addCollisionCategory` method.
*
* If you wish to reset the collision category and mask, call
* the `resetCollisionCategory` method.
*
* @method Phaser.Physics.Arcade.Components.Collision#setCollidesWith
* @since 3.70.0
*
* @param {(number|number[])} categories - The collision category to collide with, or an array of them.
*
* @return {this} This Game Object.
*/
setCollidesWith: function (categories)
{
var target = (this.body) ? this.body : this;
target.collisionMask = GetCollidesWith(categories);
return this;
},
/**
* Resets the Collision Category and Mask back to the defaults,
* which is to collide with everything.
*
* @method Phaser.Physics.Arcade.Components.Collision#resetCollisionCategory
* @since 3.70.0
*
* @return {this} This Game Object.
*/
resetCollisionCategory: function ()
{
var target = (this.body) ? this.body : this;
target.collisionCategory = 0x0001;
target.collisionMask = 1;
return this;
}
};
module.exports = Collision;