Files
esengine/source/src/ECS/Components/Physics/Colliders/CircleCollider.ts
2021-05-27 18:32:38 +08:00

62 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
module es {
export class CircleCollider extends Collider {
/**
* 创建一个具有半径的CircleCollider。
* 请注意当指定半径时如果在实体上使用RenderableComponent您将需要设置原点来对齐CircleCollider。
* 例如如果RenderableComponent有一个0,0的原点并且创建了一个半径为1.5f * renderable.width的CircleCollider你可以通过设置originNormalied为中心除以缩放尺寸来偏移原点
*
* @param radius
*/
constructor(radius: number = 1) {
super();
this.shape = new Circle(radius);
if (radius == 1) {
this._colliderRequiresAutoSizing = true;
}
}
public get radius(): number {
return (this.shape as Circle).radius;
}
public set radius(value: number) {
this.setRadius(value);
}
/**
* 设置圆的半径
* @param radius
*/
public setRadius(radius: number): CircleCollider {
this._colliderRequiresAutoSizing = false;
let circle = this.shape as Circle;
if (radius != circle.radius) {
circle.radius = radius;
circle._originalRadius = radius;
this._isPositionDirty = true;
if (this.entity != null && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
return this;
}
public debugRender(batcher: IBatcher) {
batcher.drawHollowRect(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height, new Color(76, 76, 76, 76), 2);
batcher.end();
batcher.drawCircle(this.shape.position, this.radius, new Color(139, 0, 0), 2);
batcher.end();
batcher.drawPixel(this.entity.transform.position, new Color(255, 255, 0), 4);
batcher.end();
batcher.drawPixel(this.shape.position, new Color(255, 0, 0), 2);
batcher.end();
}
public toString() {
return `[CircleCollider: bounds: ${this.bounds}, radius: ${(this.shape as Circle).radius}]`
}
}
}