2020-06-12 08:47:13 +08:00
|
|
|
///<reference path="./Shape.ts" />
|
|
|
|
|
class Circle extends Shape {
|
|
|
|
|
public radius: number;
|
2020-07-09 15:11:30 +08:00
|
|
|
public _originalRadius: number;
|
2020-07-09 14:16:10 +08:00
|
|
|
public center = new Vector2();
|
2020-07-22 20:07:14 +08:00
|
|
|
public get position(){
|
|
|
|
|
return new Vector2(this.parent.x, this.parent.y);
|
|
|
|
|
}
|
2020-06-12 08:47:13 +08:00
|
|
|
|
2020-07-20 16:38:49 +08:00
|
|
|
public get bounds(){
|
|
|
|
|
return new Rectangle().setEgretRect(this.getBounds());
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
constructor(radius: number) {
|
2020-06-12 08:47:13 +08:00
|
|
|
super();
|
|
|
|
|
this.radius = radius;
|
|
|
|
|
this._originalRadius = radius;
|
|
|
|
|
}
|
2020-06-12 20:24:51 +08:00
|
|
|
|
|
|
|
|
public pointCollidesWithShape(point: Vector2): CollisionResult {
|
2020-06-16 00:04:28 +08:00
|
|
|
return ShapeCollisions.pointToCircle(point, this);
|
2020-06-12 20:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
public collidesWithShape(other: Shape): CollisionResult {
|
2020-07-20 16:38:49 +08:00
|
|
|
if (other instanceof Box) {
|
2020-06-16 00:04:28 +08:00
|
|
|
return ShapeCollisions.circleToBox(this, other);
|
2020-06-12 20:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
if (other instanceof Circle) {
|
|
|
|
|
return ShapeCollisions.circleToCircle(this, other);
|
2020-06-15 20:08:21 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
if (other instanceof Polygon) {
|
2020-06-15 20:08:21 +08:00
|
|
|
return ShapeCollisions.circleToPolygon(this, other);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-12 20:24:51 +08:00
|
|
|
throw new Error(`Collisions of Circle to ${other} are not supported`);
|
|
|
|
|
}
|
2020-06-15 20:08:21 +08:00
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
public overlaps(other: Shape){
|
2020-07-20 16:38:49 +08:00
|
|
|
if (other instanceof Box)
|
2020-06-16 00:04:28 +08:00
|
|
|
return Collisions.isRectToCircle(other.bounds, this.position, this.radius);
|
|
|
|
|
|
|
|
|
|
if (other instanceof Circle)
|
|
|
|
|
return Collisions.isCircleToCircle(this.position, this.radius, other.position, (other as Circle).radius);
|
|
|
|
|
|
|
|
|
|
if (other instanceof Polygon)
|
|
|
|
|
return ShapeCollisions.circleToPolygon(this, other);
|
|
|
|
|
|
|
|
|
|
throw new Error(`overlaps of circle to ${other} are not supported`);
|
2020-06-15 20:08:21 +08:00
|
|
|
}
|
2020-06-12 08:47:13 +08:00
|
|
|
}
|