mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
|
|
/**
|
|
* Entity layer system
|
|
*/
|
|
class Layers {
|
|
/**
|
|
* Add a new layer
|
|
* @param {string} name name of the new layer
|
|
* @return {number} new layer's index
|
|
*/
|
|
static addLayer(name) {
|
|
if (Layers._nextAvailable > 31)
|
|
return new Error('maximum layers reached.');
|
|
Layers[name] = (1 << Layers._nextAvailable++);
|
|
return Layers[name];
|
|
}
|
|
|
|
/**
|
|
* Make a layer mask accepting nothing but the listed layers
|
|
* @param {number[]} includes layers accepted by the mask
|
|
* @return {number} the specified layer mask
|
|
*/
|
|
static makeInclusiveMask(includes) {
|
|
let mask = 0;
|
|
for (let i = 0; i < includes.length; i++)
|
|
mask |= includes[i];
|
|
return mask;
|
|
}
|
|
|
|
/**
|
|
* Make a layer mask accepting everything but the listed layers
|
|
* @param {number[]} excludes layers rejected by the mask
|
|
* @return {number} the specified layer mask
|
|
*/
|
|
static makeExclusiveMask(excludes) {
|
|
return ~Layers.makeInclusiveMask(excludes);
|
|
}
|
|
|
|
/**
|
|
* Check a layer is accepted by the mask or not
|
|
* @param {number} layer the layer number to be tested
|
|
* @param {number} mask the testing layer mask
|
|
* @return {boolean} true if accepted
|
|
*/
|
|
static check(layer, mask) {
|
|
return (layer & mask) == layer;
|
|
}
|
|
}
|
|
|
|
Layers._nextAvailable = 8;
|
|
|
|
// built-in layers, reserved up to (1 << 7)
|
|
Layers.Default = (1 << 0);
|
|
Layers.IgnoreRaycast = (1 << 1);
|
|
Layers.Gizmos = (1 << 2);
|
|
Layers.Editor = (1 << 3);
|
|
|
|
// masks
|
|
Layers.All = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor]);
|
|
Layers.RaycastMask = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor, Layers.IgnoreRaycast]);
|
|
|
|
export default Layers;
|
|
cc.Layers = Layers;
|