Files
esengine/source/src/ECS/Components/Physics/Colliders/BoxCollider.ts

79 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-06-15 10:42:06 +08:00
///<reference path="./Collider.ts" />
2020-07-23 09:10:27 +08:00
module es {
export class BoxCollider extends Collider {
constructor(x: number, y: number, width: number, height: number) {
2020-07-28 16:25:20 +08:00
super();
this._localOffset = new Vector2(x + width / 2, y + height / 2);
this.shape = new Box(width, height);
2020-07-28 16:25:20 +08:00
}
public get width() {
2020-07-23 09:10:27 +08:00
return (this.shape as Box).width;
}
2020-06-15 10:42:06 +08:00
2020-07-28 16:25:20 +08:00
public set width(value: number) {
2020-07-23 09:10:27 +08:00
this.setWidth(value);
}
2020-06-15 10:42:06 +08:00
2020-07-28 16:25:20 +08:00
public get height() {
2020-07-23 09:10:27 +08:00
return (this.shape as Box).height;
2020-06-15 10:42:06 +08:00
}
2020-07-28 16:25:20 +08:00
public set height(value: number) {
2020-07-23 09:10:27 +08:00
this.setHeight(value);
}
2020-07-23 09:10:27 +08:00
/**
* BoxCollider的大小
* @param width
* @param height
*/
2020-07-28 16:25:20 +08:00
public setSize(width: number, height: number) {
2020-07-23 09:10:27 +08:00
let box = this.shape as Box;
2020-07-28 16:25:20 +08:00
if (width != box.width || height != box.height) {
2020-07-23 09:10:27 +08:00
// 更新框,改变边界,如果我们需要更新物理系统中的边界
box.updateBox(width, height);
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
return this;
}
2020-07-23 09:10:27 +08:00
/**
* BoxCollider的宽度
* @param width
*/
2020-07-28 16:25:20 +08:00
public setWidth(width: number): BoxCollider {
2020-07-23 09:10:27 +08:00
let box = this.shape as Box;
2020-07-28 16:25:20 +08:00
if (width != box.width) {
2020-07-23 09:10:27 +08:00
// 更新框,改变边界,如果我们需要更新物理系统中的边界
box.updateBox(width, box.height);
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
2020-07-23 09:10:27 +08:00
return this;
}
2020-07-23 09:10:27 +08:00
/**
* BoxCollider的高度
* @param height
*/
2020-07-28 16:25:20 +08:00
public setHeight(height: number) {
2020-07-23 09:10:27 +08:00
let box = this.shape as Box;
2020-07-28 16:25:20 +08:00
if (height != box.height) {
2020-07-23 09:10:27 +08:00
// 更新框,改变边界,如果我们需要更新物理系统中的边界
box.updateBox(box.width, height);
if (this.entity && this._isParentEntityAddedToScene)
Physics.updateCollider(this);
}
}
2020-07-28 16:25:20 +08:00
public toString() {
2020-07-23 09:10:27 +08:00
return `[BoxCollider: bounds: ${this.bounds}]`;
}
}
2020-07-23 09:10:27 +08:00
}