2020-06-10 16:25:39 +08:00
|
|
|
class SpriteRenderer extends RenderableComponent {
|
2020-06-19 18:16:42 +08:00
|
|
|
private _sprite: Sprite;
|
2020-06-10 16:25:39 +08:00
|
|
|
private _origin: Vector2;
|
2020-06-19 18:16:42 +08:00
|
|
|
private _bitmap: egret.Bitmap;
|
2020-06-10 16:25:39 +08:00
|
|
|
|
2020-06-19 18:16:42 +08:00
|
|
|
public get bounds(){
|
2020-06-18 10:49:32 +08:00
|
|
|
if (this._areBoundsDirty){
|
|
|
|
|
if (this._sprite){
|
|
|
|
|
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin,
|
2020-06-19 18:16:42 +08:00
|
|
|
this.entity.transform.scale, this.entity.transform.rotation, this._sprite.texture2D.textureWidth,
|
|
|
|
|
this._sprite.texture2D.textureHeight);
|
2020-06-18 10:49:32 +08:00
|
|
|
this._areBoundsDirty = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-19 18:16:42 +08:00
|
|
|
|
|
|
|
|
return this._bounds;
|
2020-06-18 10:49:32 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-10 16:25:39 +08:00
|
|
|
public get sprite(){
|
|
|
|
|
return this._sprite;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 18:16:42 +08:00
|
|
|
public set sprite(value: Sprite){
|
2020-06-10 16:25:39 +08:00
|
|
|
this.setSprite(value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 18:16:42 +08:00
|
|
|
public setSprite(sprite: Sprite): SpriteRenderer{
|
2020-06-10 16:25:39 +08:00
|
|
|
this._sprite = sprite;
|
|
|
|
|
if (this._sprite)
|
2020-06-19 18:16:42 +08:00
|
|
|
this._origin = sprite.origin;
|
|
|
|
|
|
|
|
|
|
this._bitmap = new egret.Bitmap(sprite.texture2D);
|
2020-06-23 16:18:14 +08:00
|
|
|
this.scene.addChild(this._bitmap);
|
2020-06-10 16:25:39 +08:00
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
this._areBoundsDirty = true;
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 19:28:14 +08:00
|
|
|
public setColor(color: number){
|
|
|
|
|
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);
|
|
|
|
|
this._bitmap.filters = [colorFilter];
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 18:16:42 +08:00
|
|
|
public isVisibleFromCamera(camera: Camera): boolean{
|
2020-06-19 19:28:14 +08:00
|
|
|
let topLeft = camera.screenToWorldPoint(new Vector2(0, 0));
|
|
|
|
|
this.isVisible = new Rectangle(topLeft.x, topLeft.y, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds);
|
2020-06-19 18:16:42 +08:00
|
|
|
this._bitmap.visible = this.isVisible;
|
|
|
|
|
return this.isVisible;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public render(camera: Camera){
|
2020-06-18 10:49:32 +08:00
|
|
|
if (!this.sprite)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-19 18:16:42 +08:00
|
|
|
this._bitmap.x = this.entity.transform.position.x - camera.transform.position.x + camera.origin.x;
|
|
|
|
|
this._bitmap.y = this.entity.transform.position.y - camera.transform.position.y + camera.origin.y;
|
2020-06-21 10:27:15 +08:00
|
|
|
this._bitmap.rotation = this.entity.transform.rotation + camera.transform.rotation;
|
2020-06-19 18:16:42 +08:00
|
|
|
this._bitmap.anchorOffsetX = this._origin.x;
|
|
|
|
|
this._bitmap.anchorOffsetY = this._origin.y;
|
2020-06-21 10:27:15 +08:00
|
|
|
this._bitmap.scaleX = this.entity.transform.scale.x * camera.transform.scale.x;
|
|
|
|
|
this._bitmap.scaleY = this.entity.transform.scale.y * camera.transform.scale.y;
|
2020-06-10 16:25:39 +08:00
|
|
|
}
|
2020-06-22 15:27:58 +08:00
|
|
|
|
|
|
|
|
public onRemovedFromEntity(){
|
|
|
|
|
if (this._bitmap)
|
2020-06-23 16:18:14 +08:00
|
|
|
this.scene.removeChild(this._bitmap);
|
2020-06-22 15:27:58 +08:00
|
|
|
}
|
2020-06-10 16:25:39 +08:00
|
|
|
}
|