2020-06-12 08:47:13 +08:00
|
|
|
abstract class Collider extends Component{
|
|
|
|
|
public shape: Shape;
|
|
|
|
|
public physicsLayer = 1 << 0;
|
2020-06-12 20:24:51 +08:00
|
|
|
public isTrigger: boolean;
|
2020-06-15 10:42:06 +08:00
|
|
|
public registeredPhysicsBounds: Rectangle;
|
2020-06-16 00:04:28 +08:00
|
|
|
public shouldColliderScaleAndRotationWithTransform = true;
|
|
|
|
|
public collidesWithLayers = Physics.allLayers;
|
2020-06-15 10:42:06 +08:00
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
public _localOffsetLength: number;
|
2020-06-15 10:42:06 +08:00
|
|
|
protected _isParentEntityAddedToScene;
|
|
|
|
|
protected _isPositionDirty = true;
|
2020-06-15 20:08:21 +08:00
|
|
|
protected _isRotationDirty = true;
|
2020-06-15 10:42:06 +08:00
|
|
|
protected _colliderRequiresAutoSizing;
|
2020-06-15 20:08:21 +08:00
|
|
|
protected _localOffset: Vector2;
|
|
|
|
|
protected _isColliderRegisterd;
|
2020-06-12 08:47:13 +08:00
|
|
|
|
|
|
|
|
public get bounds(): Rectangle {
|
2020-06-15 20:08:21 +08:00
|
|
|
if (this._isPositionDirty || this._isRotationDirty){
|
|
|
|
|
this.shape.recalculateBounds(this);
|
|
|
|
|
this._isPositionDirty = this._isRotationDirty = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-12 08:47:13 +08:00
|
|
|
return this.shape.bounds;
|
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
2020-06-15 20:08:21 +08:00
|
|
|
public get localOffset(){
|
|
|
|
|
return this._localOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public set localOffset(value: Vector2){
|
|
|
|
|
this.setLocalOffset(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setLocalOffset(offset: Vector2){
|
|
|
|
|
if (this._localOffset != offset){
|
|
|
|
|
this.unregisterColliderWithPhysicsSystem();
|
|
|
|
|
this._localOffset = offset;
|
2020-06-16 00:04:28 +08:00
|
|
|
this._localOffsetLength = this._localOffset.length();
|
2020-06-15 20:08:21 +08:00
|
|
|
this._isPositionDirty = true;
|
|
|
|
|
this.registerColliderWithPhysicsSystem();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public registerColliderWithPhysicsSystem(){
|
|
|
|
|
if (this._isParentEntityAddedToScene && !this._isColliderRegisterd){
|
|
|
|
|
Physics.addCollider(this);
|
|
|
|
|
this._isColliderRegisterd = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unregisterColliderWithPhysicsSystem(){
|
|
|
|
|
if (this._isParentEntityAddedToScene && this._isColliderRegisterd){
|
|
|
|
|
Physics.removeCollider(this);
|
|
|
|
|
}
|
|
|
|
|
this._isColliderRegisterd = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
public overlaps(other: Collider){
|
|
|
|
|
return this.shape.overlaps(other.shape);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onEntityTransformChanged(comp: ComponentTransform){
|
|
|
|
|
switch (comp){
|
|
|
|
|
case ComponentTransform.position:
|
|
|
|
|
this._isPositionDirty = true;
|
|
|
|
|
break;
|
|
|
|
|
case ComponentTransform.scale:
|
|
|
|
|
this._isPositionDirty = true;
|
|
|
|
|
break;
|
|
|
|
|
case ComponentTransform.rotation:
|
|
|
|
|
this._isRotationDirty = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this._isColliderRegisterd)
|
|
|
|
|
Physics.updateCollider(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onEnabled(){
|
|
|
|
|
this.registerColliderWithPhysicsSystem();
|
|
|
|
|
this._isPositionDirty = this._isRotationDirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onDisabled(){
|
|
|
|
|
this.unregisterColliderWithPhysicsSystem();
|
2020-06-15 10:42:06 +08:00
|
|
|
}
|
2020-06-12 08:47:13 +08:00
|
|
|
}
|