2020-06-10 16:25:39 +08:00
|
|
|
class SpriteRenderer extends RenderableComponent {
|
|
|
|
|
private _origin: Vector2;
|
2020-07-01 14:19:40 +08:00
|
|
|
private _bitmap: egret.Bitmap;
|
|
|
|
|
private _sprite: Sprite;
|
2020-06-10 16:25:39 +08:00
|
|
|
|
2020-06-18 16:35:51 +08:00
|
|
|
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-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{
|
2020-06-19 19:28:14 +08:00
|
|
|
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 19:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
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-10 16:25:39 +08:00
|
|
|
}
|
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(){
|
|
|
|
|
}
|
|
|
|
|
}
|