2020-06-11 00:03:26 +08:00
|
|
|
class Physics {
|
|
|
|
|
private static _spatialHash: SpatialHash;
|
2020-06-16 11:59:40 +08:00
|
|
|
/** 调用reset并创建一个新的SpatialHash时使用的单元格大小 */
|
|
|
|
|
public static spatialHashCellSize = 100;
|
2020-06-11 00:03:26 +08:00
|
|
|
|
2020-06-12 20:24:51 +08:00
|
|
|
public static readonly allLayers: number = -1;
|
|
|
|
|
|
2020-06-16 11:59:40 +08:00
|
|
|
public static reset(){
|
|
|
|
|
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 10:27:15 +08:00
|
|
|
public static clear(){
|
|
|
|
|
this._spatialHash.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 00:03:26 +08:00
|
|
|
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
|
|
|
|
|
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
|
|
|
|
}
|
2020-06-15 08:46:38 +08:00
|
|
|
|
|
|
|
|
public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers){
|
|
|
|
|
return this._spatialHash.aabbBroadphase(rect, null, layerMask);
|
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
2020-06-16 11:22:37 +08:00
|
|
|
public static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask = this.allLayers){
|
|
|
|
|
return this._spatialHash.aabbBroadphase(rect, collider, layerMask);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 20:08:21 +08:00
|
|
|
public static addCollider(collider: Collider){
|
|
|
|
|
Physics._spatialHash.register(collider);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static removeCollider(collider: Collider){
|
|
|
|
|
Physics._spatialHash.remove(collider);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 10:42:06 +08:00
|
|
|
public static updateCollider(collider: Collider){
|
|
|
|
|
this._spatialHash.remove(collider);
|
|
|
|
|
this._spatialHash.register(collider);
|
|
|
|
|
}
|
2020-06-11 00:03:26 +08:00
|
|
|
}
|