Files
esengine/source/src/ECS/Components/RenderableComponent.ts

231 lines
8.2 KiB
TypeScript
Raw Normal View History

2020-07-01 16:55:10 +08:00
///<reference path="./PooledComponent.ts" />
module es {
/**
*
*/
export abstract class RenderableComponent extends Component implements IRenderable {
/**
* egret显示对象
*/
public displayObject: egret.DisplayObject = new egret.DisplayObject();
public hollowShape: egret.Shape = new egret.Shape();
public pixelShape: egret.Shape = new egret.Shape();
2020-07-28 16:25:20 +08:00
/**
*
*/
public color: number = 0x000000;
protected _areBoundsDirty = true;
/**
* renderableComponent的宽度
* bounds属性则需要实现这个
*/
public get width() {
return this.bounds.width;
}
/**
* renderableComponent的高度
* bounds属性则需要实现这个
*/
public get height() {
return this.bounds.height;
}
public debugRenderEnabled: boolean = true;
2020-07-28 16:25:20 +08:00
protected _localOffset: Vector2 = Vector2.zero;
/**
2020-07-28 16:25:20 +08:00
*
*/
2020-07-28 16:25:20 +08:00
public get localOffset(): Vector2 {
return this._localOffset;
}
2020-07-28 16:25:20 +08:00
/**
*
* @param value
*/
public set localOffset(value: Vector2) {
this.setLocalOffset(value);
}
2020-07-28 16:25:20 +08:00
protected _renderLayer: number = 0;
/**
*
*/
2020-07-28 16:25:20 +08:00
public get renderLayer(): number {
return this._renderLayer;
}
2020-07-28 16:25:20 +08:00
public set renderLayer(value: number) {
2020-08-08 09:43:03 +08:00
this.setRenderLayer(value);
}
2020-07-28 16:25:20 +08:00
protected _bounds: Rectangle = new Rectangle();
/**
2020-07-28 16:25:20 +08:00
* AABB,
*/
2020-07-28 16:25:20 +08:00
public get bounds(): Rectangle {
if (this._areBoundsDirty) {
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, Vector2.zero,
this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
this._areBoundsDirty = false;
}
2020-07-28 16:25:20 +08:00
return this._bounds;
}
2020-07-28 16:25:20 +08:00
private _isVisible: boolean;
/**
* onBecameVisible/onBecameInvisible方法
*/
public get isVisible() {
return this._isVisible;
}
/**
* onBecameVisible/onBecameInvisible方法
* @param value
*/
public set isVisible(value: boolean) {
2020-07-28 16:25:20 +08:00
if (this._isVisible != value) {
this._isVisible = value;
if (this._isVisible)
this.onBecameVisible();
else
this.onBecameInvisible();
}
}
public onEntityTransformChanged(comp: transform.Component) {
this._areBoundsDirty = true;
}
/**
* 使
* @param camera
*/
public abstract render(camera: Camera);
public debugRender(camera: Camera) {
if (!this.debugRenderEnabled)
return;
if (!this.hollowShape.parent)
this.debugDisplayObject.addChild(this.hollowShape);
if (!this.pixelShape.parent)
this.debugDisplayObject.addChild(this.pixelShape);
if (!this.entity.getComponent(Collider)){
this.hollowShape.graphics.clear();
this.hollowShape.graphics.beginFill(Colors.renderableBounds, 0);
this.hollowShape.graphics.lineStyle(1, Colors.renderableBounds);
this.hollowShape.graphics.drawRect(this.bounds.x - camera.bounds.x, this.bounds.y - camera.bounds.y, this.bounds.width, this.bounds.height);
this.hollowShape.graphics.endFill();
}
let pixelPos = Vector2.add(this.entity.transform.position, this._localOffset).subtract(camera.bounds.location);
this.pixelShape.graphics.clear();
this.pixelShape.graphics.beginFill(Colors.renderableCenter, 0);
this.pixelShape.graphics.lineStyle(4, Colors.renderableCenter);
this.pixelShape.graphics.moveTo(pixelPos.x, pixelPos.y);
this.pixelShape.graphics.lineTo(pixelPos.x, pixelPos.y);
this.pixelShape.graphics.endFill();
}
/**
* renderableComponent的边界与camera.bounds相交 true
* isVisible标志的状态开关
* 使
* @param camera
*/
public isVisibleFromCamera(camera: Camera): boolean {
2020-08-21 19:21:40 +08:00
if (!camera)
return false;
this.isVisible = camera.bounds.intersects(this.bounds);
return this.isVisible;
}
/**
*
* @param renderLayer
*/
2020-07-28 16:25:20 +08:00
public setRenderLayer(renderLayer: number): RenderableComponent {
if (renderLayer != this._renderLayer) {
let oldRenderLayer = this._renderLayer;
this._renderLayer = renderLayer;
// 如果该组件拥有一个实体那么是由ComponentList管理需要通知它改变了渲染层
if (this.entity && this.entity.scene)
this.entity.scene.renderableComponents.updateRenderableRenderLayer(this, oldRenderLayer, this._renderLayer);
}
return this;
}
/**
*
* @param color
*/
2020-07-28 16:25:20 +08:00
public setColor(color: number): RenderableComponent {
this.color = color;
return this;
}
/**
*
* @param offset
*/
2020-07-28 16:25:20 +08:00
public setLocalOffset(offset: Vector2): RenderableComponent {
if (this._localOffset != offset) {
this._localOffset = offset;
}
return this;
}
/**
*
*/
2020-07-28 16:25:20 +08:00
public sync(camera: Camera) {
if (this.displayObject.x != this.bounds.x - camera.bounds.y) this.displayObject.x = this.bounds.x - camera.bounds.y;
if (this.displayObject.y != this.bounds.y - camera.bounds.y) this.displayObject.y = this.bounds.y - camera.bounds.y;
2020-08-15 21:59:52 +08:00
if (this.displayObject.scaleX != this.entity.scale.x) this.displayObject.scaleX = this.entity.scale.x;
if (this.displayObject.scaleY != this.entity.scale.y) this.displayObject.scaleY = this.entity.scale.y;
if (this.displayObject.rotation != this.entity.rotationDegrees) this.displayObject.rotation = this.entity.rotationDegrees;
}
public compareTo(other: RenderableComponent){
return other.renderLayer - this.renderLayer;
}
2020-07-28 16:25:20 +08:00
public toString() {
return `[RenderableComponent] renderLayer: ${this.renderLayer}`;
}
2020-07-28 16:25:20 +08:00
/**
* renderableComponent进入相机框架时调用
* isVisibleFromCamera进行剔除检查
*/
protected onBecameVisible() {
this.displayObject.visible = this.isVisible;
this.debugDisplayObject.visible = this.isVisible;
2020-07-28 16:25:20 +08:00
}
/**
* renderableComponent离开相机框架时调用
* isVisibleFromCamera进行剔除检查
*/
protected onBecameInvisible() {
this.displayObject.visible = this.isVisible;
this.debugDisplayObject.visible = this.isVisible;
2020-07-28 16:25:20 +08:00
}
}
}