移动类至es模块

This commit is contained in:
YHH
2020-07-23 09:10:27 +08:00
parent 15c0844e29
commit 814234ca61
20 changed files with 1101 additions and 998 deletions

View File

@@ -55,18 +55,20 @@ module es {
public overlaps(other: Shape){
// 特殊情况这一个高性能方式实现其他情况则使用polygon方法检测
if (other instanceof Box)
return this.bounds.intersects(other.bounds);
if (this.isUnrotated){
if (other instanceof Box)
return this.bounds.intersects(other.bounds);
if (other instanceof Circle)
return Collisions.isRectToCircle(this.bounds, other.position, other.radius);
if (other instanceof Circle)
return Collisions.isRectToCircle(this.bounds, other.position, other.radius);
}
return super.overlaps(other);
}
public collidesWithShape(other: Shape){
// 特殊情况这一个高性能方式实现其他情况则使用polygon方法检测
if (other instanceof Box){
if (other instanceof Box && (other as Box).isUnrotated){
return ShapeCollisions.boxToBox(this, other);
}
@@ -75,14 +77,11 @@ module es {
return super.collidesWithShape(other);
}
/**
*
* @param point
*/
public containsPoint(point: Vector2){
return this.bounds.contains(point.x, point.y);
if (this.isUnrotated)
return this.bounds.contains(point.x, point.y);
return super.containsPoint(point);
}
}
}

View File

@@ -1,52 +1,69 @@
///<reference path="./Shape.ts" />
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);
}
module es {
export class Circle extends Shape {
public radius: number;
public _originalRadius: number;
public get bounds(){
return new Rectangle().setEgretRect(this.getBounds());
}
constructor(radius: number) {
super();
this.radius = radius;
this._originalRadius = radius;
}
public pointCollidesWithShape(point: Vector2): CollisionResult {
return ShapeCollisions.pointToCircle(point, this);
}
public collidesWithShape(other: Shape): CollisionResult {
if (other instanceof Box) {
return ShapeCollisions.circleToBox(this, other);
constructor(radius: number) {
super();
this.radius = radius;
this._originalRadius = radius;
}
if (other instanceof Circle) {
return ShapeCollisions.circleToCircle(this, other);
public recalculateBounds(collider: es.Collider) {
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心
this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotateWithTransform) {
// 我们只将直线缩放为一个圆,所以我们将使用最大值
let scale = collider.entity.transform.scale;
let hasUnitScale = scale.x == 1 && scale.y == 1;
let maxScale = Math.max(scale.x, scale.y);
this.radius = this._originalRadius * maxScale;
if (collider.entity.transform.rotation != 0) {
// 为了处理偏移原点的旋转,我们只需要将圆心围绕(0,0)在一个圆上移动我们的偏移量就是0角
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
let offsetLength = hasUnitScale ? collider._localOffsetLength : Vector2.multiply(collider.localOffset, collider.entity.transform.scale).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, collider.entity.transform.rotation + offsetAngle);
}
}
this.position = Vector2.add(collider.transform.position, this.center);
this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
}
if (other instanceof Polygon) {
return ShapeCollisions.circleToPolygon(this, other);
public overlaps(other: Shape) {
if (other instanceof Box && (other as Box).isUnrotated)
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`);
}
throw new Error(`Collisions of Circle to ${other} are not supported`);
public collidesWithShape(other: Shape): CollisionResult {
if (other instanceof Box && (other as Box).isUnrotated) {
return ShapeCollisions.circleToBox(this, other);
}
if (other instanceof Circle) {
return ShapeCollisions.circleToCircle(this, other);
}
if (other instanceof Polygon) {
return ShapeCollisions.circleToPolygon(this, other);
}
throw new Error(`Collisions of Circle to ${other} are not supported`);
}
public pointCollidesWithShape(point: Vector2): CollisionResult {
return ShapeCollisions.pointToCircle(point, this);
}
}
public overlaps(other: Shape){
if (other instanceof Box)
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`);
}
}
}