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);
|
|
|
|
|
this.stage.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 18:16:42 +08:00
|
|
|
public isVisibleFromCamera(camera: Camera): boolean{
|
|
|
|
|
this.isVisible = new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds);
|
|
|
|
|
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;
|
|
|
|
|
this._bitmap.rotation = this.entity.transform.rotation;
|
|
|
|
|
this._bitmap.anchorOffsetX = this._origin.x;
|
|
|
|
|
this._bitmap.anchorOffsetY = this._origin.y;
|
|
|
|
|
this._bitmap.scaleX = this.entity.transform.scale.x;
|
|
|
|
|
this._bitmap.scaleY = this.entity.transform.scale.y;
|
2020-06-10 16:25:39 +08:00
|
|
|
}
|
|
|
|
|
}
|