修复physics register失效问题

This commit is contained in:
yhh
2020-06-16 11:59:40 +08:00
parent 8b21edc65f
commit ced176706b
14 changed files with 169 additions and 18 deletions

View File

@@ -11,7 +11,7 @@ abstract class Collider extends Component{
public _isRotationDirty = true;
protected _isParentEntityAddedToScene;
protected _colliderRequiresAutoSizing;
protected _localOffset: Vector2;
protected _localOffset: Vector2 = new Vector2(0, 0);
protected _isColliderRegisterd;
public get bounds(): Rectangle {

View File

@@ -74,7 +74,9 @@ class Scene extends egret.DisplayObjectContainer {
/** 初始化场景 */
public initialize(){
/** 初始化默认相机 */
this.camera = this.createEntity("camera").addComponent(new Camera());
this.camera = this.createEntity("camera").getOrCreateComponent(new Camera());
Physics.reset();
if (this.entityProcessors)
this.entityProcessors.begin();

View File

@@ -2,8 +2,8 @@ class Point {
public x: number;
public y: number;
constructor(x: number, y: number){
this.x = x;
this.y = y;
constructor(x?: number, y?: number){
this.x = x ? x : 0;
this.y = y ? y : this.x;
}
}

View File

@@ -1,8 +1,14 @@
class Physics {
private static _spatialHash: SpatialHash;
/** 调用reset并创建一个新的SpatialHash时使用的单元格大小 */
public static spatialHashCellSize = 100;
public static readonly allLayers: number = -1;
public static reset(){
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
}
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
}

View File

@@ -35,6 +35,21 @@ class SpatialHash {
collider.registeredPhysicsBounds = bounds;
let p1 = this.cellCoords(bounds.x, bounds.y);
let p2 = this.cellCoords(bounds.right, bounds.bottom);
if (!this.gridBounds.contains(new Vector2(p1.x, p1.y))){
this.gridBounds = RectangleExt.union(this.gridBounds, p1);
}
if (!this.gridBounds.contains(new Vector2(p2.x, p2.y))){
this.gridBounds = RectangleExt.union(this.gridBounds, p2);
}
for (let x = p1.x; x <= p2.x; x++){
for (let y = p1.y; y <= p2.y; y++){
let c = this.cellAtPosition(x, y, true);
c.push(collider);
}
}
}
public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask){

View File

@@ -0,0 +1,16 @@
class RectangleExt {
public static union(first: Rectangle, point: Point){
let rect = new Rectangle(point.x, point.y, 0, 0);
return this.unionR(first, rect);
}
public static unionR(value1: Rectangle, value2: Rectangle){
let result = new Rectangle();
result.x = Math.min(value1.x, value2.x);
result.y = Math.min(value1.y, value2.y);
result.width = Math.max(value1.right, value2.right) - result.x;
result.height = Math.max(value1.bottom, value2.bottom) - result.y;
return result;
}
}