2020-06-15 10:42:06 +08:00
|
|
|
///<reference path="./Polygon.ts" />
|
|
|
|
|
class Box extends Polygon {
|
|
|
|
|
public width: number;
|
|
|
|
|
public height: number;
|
|
|
|
|
|
2020-06-16 16:35:17 +08:00
|
|
|
constructor(width: number, height: number){
|
|
|
|
|
super(Box.buildBox(width, height), true);
|
|
|
|
|
this.width = width;
|
|
|
|
|
this.height = height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static buildBox(width: number, height: number): Vector2[]{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
return verts;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 12:18:51 +08:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param other
|
|
|
|
|
*/
|
|
|
|
|
public collidesWithShape(other: Shape){
|
|
|
|
|
if (this.isUnrotated && other instanceof Box && other.isUnrotated){
|
|
|
|
|
return ShapeCollisions.boxToBox(this, other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: 让 minkowski 运行于 cricleToBox
|
|
|
|
|
|
|
|
|
|
return super.collidesWithShape(other);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 10:42:06 +08:00
|
|
|
public updateBox(width: number, height: number){
|
|
|
|
|
this.width = width;
|
|
|
|
|
this.height = height;
|
|
|
|
|
|
|
|
|
|
let halfWidth = width / 2;
|
|
|
|
|
let halfHeight = height / 2;
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public containsPoint(point: Vector2){
|
|
|
|
|
if (this.isUnrotated)
|
|
|
|
|
return this.bounds.contains(point);
|
|
|
|
|
|
|
|
|
|
return super.containsPoint(point);
|
|
|
|
|
}
|
|
|
|
|
}
|