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

134 lines
4.4 KiB
TypeScript
Raw Normal View History

module es {
import Bitmap = egret.Bitmap;
export class SpriteRenderer extends RenderableComponent {
2020-07-28 16:25:20 +08:00
constructor(sprite: Sprite | egret.Texture = null) {
super();
if (sprite instanceof Sprite)
this.setSprite(sprite);
else if (sprite instanceof egret.Texture)
this.setSprite(new Sprite(sprite));
}
2020-07-23 09:10:27 +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.sourceRect.width,
this._sprite.sourceRect.height);
this._areBoundsDirty = false;
}
}
return this._bounds;
}
/**
*
* x/y 0-1
*/
2020-07-23 09:10:27 +08:00
public get originNormalized(): Vector2 {
return new Vector2(this._origin.x / this.width * this.entity.transform.scale.x,
this._origin.y / this.height * this.entity.transform.scale.y);
}
/**
*
* x/y 0-1
* @param value
*/
2020-07-23 09:10:27 +08:00
public set originNormalized(value: Vector2) {
this.setOrigin(new Vector2(value.x * this.width / this.entity.transform.scale.x,
value.y * this.height / this.entity.transform.scale.y));
}
2020-06-19 18:16:42 +08:00
2020-07-28 16:25:20 +08:00
protected _origin: Vector2;
/**
*
*/
public get origin(): Vector2 {
return this._origin;
}
/**
*
* @param value
*/
public set origin(value: Vector2) {
this.setOrigin(value);
}
protected _sprite: Sprite;
/**
*
* origin
*/
public get sprite(): Sprite {
return this._sprite;
2020-07-22 20:07:14 +08:00
}
2020-06-22 15:27:58 +08:00
/**
*
* origin
* @param value
*/
public set sprite(value: Sprite) {
this.setSprite(value);
}
/**
* sprite.origin
* @param sprite
*/
public setSprite(sprite: Sprite): SpriteRenderer {
this._sprite = sprite;
if (this._sprite) {
this._origin = this._sprite.origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
}
this.displayObject = new Bitmap(sprite.texture2D);
return this;
}
/**
*
* @param origin
*/
2020-07-23 09:10:27 +08:00
public setOrigin(origin: Vector2): SpriteRenderer {
if (this._origin != origin) {
this._origin = origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
this._areBoundsDirty = true;
}
return this;
}
/**
*
* x/y 0-1
* @param value
*/
2020-07-23 09:10:27 +08:00
public setOriginNormalized(value: Vector2): SpriteRenderer {
this.setOrigin(new Vector2(value.x * this.width / this.entity.transform.scale.x,
value.y * this.height / this.entity.transform.scale.y));
return this;
}
public render(camera: Camera) {
this.sync(camera);
2020-07-27 18:16:19 +08:00
2020-08-15 21:59:52 +08:00
let afterPos = new Vector2(this.entity.position.x + this.localOffset.x - camera.position.x + camera.origin.x,
this.entity.position.y + this.localOffset.y - camera.position.y + camera.origin.y);
if (this.displayObject.x != afterPos.x) this.displayObject.x = afterPos.x;
if (this.displayObject.y != afterPos.y) this.displayObject.y = afterPos.y;
}
2020-07-01 14:19:40 +08:00
}
}