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

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-07-22 20:07:14 +08:00
class SpriteRenderer extends RenderableComponent {
2020-07-01 14:19:40 +08:00
private _sprite: Sprite;
2020-07-03 17:51:18 +08:00
protected bitmap: egret.Bitmap;
2020-07-08 18:12:17 +08:00
/** 应该由这个精灵显示的精灵 */
2020-07-22 20:07:14 +08:00
public get sprite(): Sprite {
2020-07-03 16:45:52 +08:00
return this._sprite;
}
2020-07-08 18:12:17 +08:00
/** 应该由这个精灵显示的精灵 */
2020-07-22 20:07:14 +08:00
public set sprite(value: Sprite) {
2020-07-03 16:45:52 +08:00
this.setSprite(value);
}
2020-07-22 20:07:14 +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;
2020-07-08 18:12:17 +08:00
if (this._sprite) {
this.anchorOffsetX = this._sprite.origin.x / this._sprite.sourceRect.width;
this.anchorOffsetY = this._sprite.origin.y / this._sprite.sourceRect.height;
}
2020-07-03 17:51:18 +08:00
this.bitmap = new egret.Bitmap(sprite.texture2D);
this.addChild(this.bitmap);
2020-07-01 14:19:40 +08:00
return this;
2020-06-29 15:41:02 +08:00
}
2020-07-22 20:07:14 +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-07-22 20:07:14 +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-07-22 20:07:14 +08:00
public render(camera: Camera) {
if (this.x != -camera.position.x + camera.origin.x ||
this.y != -camera.position.y + camera.origin.y) {
this.x = -camera.position.x + camera.origin.x;
this.y = -camera.position.y + camera.origin.y;
this.entity.onEntityTransformChanged(TransformComponent.position);
}
}
2020-06-22 15:27:58 +08:00
2020-07-22 20:07:14 +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
2020-07-22 20:07:14 +08:00
public reset() {
2020-07-01 14:19:40 +08:00
}
}