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

73 lines
2.3 KiB
TypeScript
Raw Normal View History

class SpriteRenderer extends RenderableComponent {
private _origin: Vector2;
2020-07-01 14:19:40 +08:00
private _bitmap: egret.Bitmap;
private _sprite: Sprite;
public get origin(){
return this._origin;
}
public set origin(value: Vector2){
this.setOrigin(value);
}
public setOrigin(origin: Vector2){
if (this._origin != origin){
this._origin = origin;
}
return this;
}
2020-07-03 16:45:52 +08:00
/** 应该由这个精灵显示的精灵。当设置时,精灵的原点也被设置为匹配精灵.origin。 */
public get sprite(): Sprite{
return this._sprite;
}
/** 应该由这个精灵显示的精灵。当设置时,精灵的原点也被设置为匹配精灵.origin。 */
public set sprite(value: Sprite){
this.setSprite(value);
}
2020-07-01 14:19:40 +08:00
public setSprite(sprite: Sprite): SpriteRenderer{
2020-06-29 15:41:02 +08:00
this.removeChildren();
2020-07-01 14:19:40 +08:00
this._sprite = sprite;
if (this._sprite) this._origin = this._sprite.origin;
this._bitmap = new egret.Bitmap(sprite.texture2D);
this.addChild(this._bitmap);
return this;
2020-06-29 15:41:02 +08:00
}
2020-07-01 14:19:40 +08:00
public setColor(color: number): SpriteRenderer{
let colorMatrix = [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
colorMatrix[0] = Math.floor(color / 256 / 256) / 255;
colorMatrix[6] = Math.floor(color / 256 % 256) / 255;
colorMatrix[12] = color % 256 / 255;
let colorFilter = new egret.ColorMatrixFilter(colorMatrix);
2020-06-29 15:41:02 +08:00
this.filters = [colorFilter];
2020-07-01 14:19:40 +08:00
return this;
}
2020-06-19 18:16:42 +08:00
public isVisibleFromCamera(camera: Camera): boolean{
2020-07-01 14:19:40 +08:00
this.isVisible = new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds);
2020-06-29 15:41:02 +08:00
this.visible = this.isVisible;
2020-06-19 18:16:42 +08:00
return this.isVisible;
}
2020-07-01 14:19:40 +08:00
/** 渲染处理 在每个模块中处理各自的渲染逻辑 */
2020-06-19 18:16:42 +08:00
public render(camera: Camera){
2020-07-01 14:19:40 +08:00
this.x = this.entity.position.x - this.origin.x - camera.position.x + camera.origin.x;
this.y = this.entity.position.y - this.origin.y - camera.position.y + camera.origin.y;
}
2020-06-22 15:27:58 +08:00
public onRemovedFromEntity(){
2020-06-29 15:41:02 +08:00
if (this.parent)
this.parent.removeChild(this);
2020-06-22 15:27:58 +08:00
}
2020-07-01 14:19:40 +08:00
public reset(){
}
}