2020-06-10 16:25:39 +08:00
|
|
|
class SpriteRenderer extends RenderableComponent {
|
|
|
|
|
private _sprite: egret.DisplayObject;
|
|
|
|
|
private _origin: Vector2;
|
|
|
|
|
|
2020-06-18 10:49:32 +08:00
|
|
|
public get bounds(){
|
|
|
|
|
if (this._areBoundsDirty){
|
|
|
|
|
if (this._sprite){
|
|
|
|
|
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin,
|
|
|
|
|
this.entity.transform.scale, this.entity.transform.rotation, this._sprite.width,
|
|
|
|
|
this._sprite.height);
|
|
|
|
|
this._areBoundsDirty = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.bounds;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 16:25:39 +08:00
|
|
|
public get sprite(){
|
|
|
|
|
return this._sprite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public set sprite(value: egret.DisplayObject){
|
|
|
|
|
this.setSprite(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setSprite(sprite: egret.DisplayObject): SpriteRenderer{
|
|
|
|
|
this._sprite = sprite;
|
|
|
|
|
if (this._sprite)
|
|
|
|
|
this._origin = new Vector2(this._sprite.anchorOffsetX, this._sprite.anchorOffsetY);
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 10:49:32 +08:00
|
|
|
public render(camera: Camera) {
|
|
|
|
|
if (!this.sprite)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.sprite.x = this.entity.transform.position.x;
|
|
|
|
|
this.sprite.y = this.entity.transform.position.y;
|
|
|
|
|
this.sprite.rotation = this.entity.transform.rotation;
|
|
|
|
|
this.sprite.anchorOffsetX = this._origin.x;
|
|
|
|
|
this.sprite.anchorOffsetY = this._origin.y;
|
|
|
|
|
this.sprite.scaleX = this.entity.transform.scale.x;
|
|
|
|
|
this.sprite.scaleY = this.entity.transform.scale.y;
|
2020-06-10 16:25:39 +08:00
|
|
|
}
|
|
|
|
|
}
|