2020-06-15 10:42:06 +08:00
|
|
|
|
///<reference path="./Polygon.ts" />
|
2020-07-22 23:30:31 +08:00
|
|
|
|
module es {
|
2020-07-08 18:12:17 +08:00
|
|
|
|
/**
|
2020-07-22 23:30:31 +08:00
|
|
|
|
* 多边形的特殊情况。在进行SAT碰撞检查时,我们只需要检查2个轴而不是8个轴
|
2020-07-08 18:12:17 +08:00
|
|
|
|
*/
|
2020-07-22 23:30:31 +08:00
|
|
|
|
export class Box extends Polygon {
|
|
|
|
|
|
public width: number;
|
|
|
|
|
|
public height: number;
|
2020-06-16 16:35:17 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
constructor(width: number, height: number){
|
|
|
|
|
|
super(Box.buildBox(width, height), true);
|
|
|
|
|
|
this.width = width;
|
|
|
|
|
|
this.height = height;
|
|
|
|
|
|
}
|
2020-07-07 18:54:19 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 在一个盒子的形状中建立多边形需要的点的帮助方法
|
|
|
|
|
|
* @param width
|
|
|
|
|
|
* @param height
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static buildBox(width: number, height: number): Vector2[]{
|
|
|
|
|
|
// 我们在(0,0)的中心周围创建点
|
|
|
|
|
|
let halfWidth = width / 2;
|
|
|
|
|
|
let halfHeight = height / 2;
|
|
|
|
|
|
let verts = new Array(4);
|
|
|
|
|
|
verts[0] = new Vector2(-halfWidth, -halfHeight);
|
|
|
|
|
|
verts[1] = new Vector2(halfWidth, -halfHeight);
|
|
|
|
|
|
verts[2] = new Vector2(halfWidth, halfHeight);
|
|
|
|
|
|
verts[3] = new Vector2(-halfWidth, halfHeight);
|
2020-07-07 18:54:19 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return verts;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新框点,重新计算中心,设置宽度/高度
|
|
|
|
|
|
* @param width
|
|
|
|
|
|
* @param height
|
|
|
|
|
|
*/
|
|
|
|
|
|
public updateBox(width: number, height: number){
|
|
|
|
|
|
this.width = width;
|
|
|
|
|
|
this.height = height;
|
|
|
|
|
|
|
|
|
|
|
|
// 我们在(0,0)的中心周围创建点
|
|
|
|
|
|
let halfWidth = width / 2;
|
|
|
|
|
|
let halfHeight = height / 2;
|
2020-07-07 18:54:19 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.points[0] = new Vector2(-halfWidth, -halfHeight);
|
|
|
|
|
|
this.points[1] = new Vector2(halfWidth, -halfHeight);
|
|
|
|
|
|
this.points[2] = new Vector2(halfWidth, halfHeight);
|
|
|
|
|
|
this.points[3] = new Vector2(-halfWidth, halfHeight);
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.points.length; i ++)
|
|
|
|
|
|
this._originalPoints[i] = this.points[i];
|
2020-07-07 12:18:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public overlaps(other: Shape){
|
|
|
|
|
|
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
|
|
|
|
|
|
if (other instanceof Box)
|
|
|
|
|
|
return this.bounds.intersects(other.bounds);
|
2020-07-07 12:18:51 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
if (other instanceof Circle)
|
|
|
|
|
|
return Collisions.isRectToCircle(this.bounds, other.position, other.radius);
|
2020-07-07 12:18:51 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return super.overlaps(other);
|
|
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public collidesWithShape(other: Shape){
|
|
|
|
|
|
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
|
|
|
|
|
|
if (other instanceof Box){
|
|
|
|
|
|
return ShapeCollisions.boxToBox(this, other);
|
|
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
// TODO: 让 minkowski 运行于 cricleToBox
|
2020-06-15 10:42:06 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return super.collidesWithShape(other);
|
|
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param point
|
|
|
|
|
|
*/
|
|
|
|
|
|
public containsPoint(point: Vector2){
|
|
|
|
|
|
return this.bounds.contains(point.x, point.y);
|
|
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|