2020-06-10 16:25:39 +08:00
|
|
|
/**
|
|
|
|
|
* 所有可渲染组件的基类
|
|
|
|
|
*/
|
2020-06-18 16:35:51 +08:00
|
|
|
abstract class RenderableComponent extends Component implements IRenderable {
|
2020-06-10 17:41:53 +08:00
|
|
|
private _isVisible: boolean;
|
2020-06-18 10:49:32 +08:00
|
|
|
protected _areBoundsDirty = true;
|
2020-06-18 23:22:54 +08:00
|
|
|
protected _bounds: Rectangle = new Rectangle();
|
|
|
|
|
protected _localOffset: Vector2 = Vector2.zero;
|
2020-06-10 17:41:53 +08:00
|
|
|
|
|
|
|
|
public get width(){
|
2020-06-11 00:03:26 +08:00
|
|
|
return this.getWidth();
|
2020-06-10 17:41:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get height(){
|
2020-06-11 00:03:26 +08:00
|
|
|
return this.getHeight();
|
2020-06-10 17:41:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get isVisible(){
|
|
|
|
|
return this._isVisible;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public set isVisible(value: boolean){
|
|
|
|
|
this._isVisible = value;
|
|
|
|
|
|
|
|
|
|
if (this._isVisible)
|
|
|
|
|
this.onBecameVisible();
|
|
|
|
|
else
|
|
|
|
|
this.onBecameInvisible();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get bounds(): Rectangle{
|
2020-06-19 18:16:42 +08:00
|
|
|
if (this._areBoundsDirty){
|
|
|
|
|
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(0, 0),
|
|
|
|
|
this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
|
|
|
|
|
this._areBoundsDirty = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this._bounds;
|
2020-06-11 00:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected getWidth(){
|
|
|
|
|
return this.bounds.width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected getHeight(){
|
|
|
|
|
return this.bounds.height;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 17:41:53 +08:00
|
|
|
protected onBecameVisible(){}
|
|
|
|
|
|
|
|
|
|
protected onBecameInvisible(){}
|
|
|
|
|
|
2020-06-18 10:49:32 +08:00
|
|
|
public abstract render(camera: Camera);
|
|
|
|
|
|
2020-06-10 17:41:53 +08:00
|
|
|
public isVisibleFromCamera(camera: Camera): boolean{
|
2020-06-19 18:16:42 +08:00
|
|
|
this.isVisible = camera.bounds.intersects(this.bounds);
|
|
|
|
|
return this.isVisible;
|
2020-06-10 17:41:53 +08:00
|
|
|
}
|
2020-06-18 16:35:51 +08:00
|
|
|
|
|
|
|
|
public onEntityTransformChanged(comp: ComponentTransform){
|
|
|
|
|
this._areBoundsDirty = true;
|
|
|
|
|
}
|
2020-06-10 16:25:39 +08:00
|
|
|
}
|