整理ecs框架

This commit is contained in:
yhh
2020-07-22 20:07:14 +08:00
parent 6b8569b0b5
commit 5b8f414a45
31 changed files with 1908 additions and 1239 deletions

View File

@@ -3,6 +3,9 @@ class Circle extends Shape {
public radius: number;
public _originalRadius: number;
public center = new Vector2();
public get position(){
return new Vector2(this.parent.x, this.parent.y);
}
public get bounds(){
return new Rectangle().setEgretRect(this.getBounds());

View File

@@ -1,13 +1,22 @@
///<reference path="./Shape.ts" />
/**
* 多边形
*/
class Polygon extends Shape {
/** 组成多边形的点。它们应该是CW和凸的。 */
public points: Vector2[];
private _polygonCenter: Vector2;
private _areEdgeNormalsDirty = true;
protected _originalPoints: Vector2[];
public center = new Vector2();
/**
* 多边形坐标
* 此为内部字段 可访问
*/
public position: Vector2 = Vector2.zero;
public get bounds(){
return new Rectangle(0, 0, this.width, this.height);
return new Rectangle(this.position.x, this.position.y, 0, 0);
}
public _edgeNormals: Vector2[];
@@ -158,6 +167,11 @@ class Polygon extends Shape {
return { closestPoint: closestPoint, distanceSquared: distanceSquared, edgeNormal: edgeNormal };
}
public recalculateBounds(collider: Collider){
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心我们会从那开始
}
public pointCollidesWithShape(point: Vector2): CollisionResult {
return ShapeCollisions.pointToPoly(point, this);
}

View File

@@ -1,8 +1,20 @@
abstract class Shape extends egret.DisplayObject {
public abstract bounds: Rectangle;
public position: Vector2 = Vector2.zero;
public abstract center: Vector2;
abstract class Shape {
/** 缓存的形状边界 内部字段 */
public bounds: Rectangle;
/**
* 这不是中心。这个值不一定是物体的中心。对撞机更准确。
* 应用任何转换旋转的localOffset
* 内部字段
*/
public center: Vector2;
/**
* 有一个单独的位置字段可以让我们改变形状的位置来进行碰撞检查而不是改变entity.position。
* 触发碰撞器/边界/散列更新的位置。
* 内部字段
*/
public position: Vector2;
public abstract recalculateBounds(collider: Collider);
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
public abstract overlaps(other: Shape);
public abstract collidesWithShape(other: Shape): CollisionResult;

View File

@@ -63,7 +63,8 @@ class SpatialHash {
for (let y = p1.y; y <= p2.y; y++) {
// 如果没有单元格,我们需要创建它
let c = this.cellAtPosition(x, y, true);
c.push(collider);
if (c.indexOf(collider) == -1)
c.push(collider);
}
}
}
@@ -83,7 +84,7 @@ class SpatialHash {
let bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter;
// this._overlapTestCircle.position = circleCenter;
let resultCounter = 0;
let aabbBroadphaseResult = this.aabbBroadphase(bounds, null, layerMask);