2020-07-22 23:30:31 +08:00
|
|
|
|
module es {
|
2020-07-27 16:10:36 +08:00
|
|
|
|
import Bitmap = egret.Bitmap;
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
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) {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-07-27 16:10:36 +08:00
|
|
|
|
|
|
|
|
|
|
return this._bounds;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
2020-06-18 16:35:51 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 用归一化方法设置原点
|
|
|
|
|
|
* x/y 均为 0-1
|
|
|
|
|
|
*/
|
2020-07-23 09:10:27 +08:00
|
|
|
|
public get originNormalized(): Vector2 {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return new Vector2(this._origin.x / this.width * this.entity.transform.scale.x,
|
|
|
|
|
|
this._origin.y / this.height * this.entity.transform.scale.y);
|
|
|
|
|
|
}
|
2020-06-19 19:28:14 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 用归一化方法设置原点
|
|
|
|
|
|
* x/y 均为 0-1
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-23 09:10:27 +08:00
|
|
|
|
public set originNormalized(value: Vector2) {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
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;
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 应该由这个精灵显示的精灵
|
|
|
|
|
|
* 当设置时,精灵的原点也被设置为精灵的origin
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get sprite(): Sprite {
|
|
|
|
|
|
return this._sprite;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
2020-06-22 15:27:58 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +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;
|
2020-07-27 16:10:36 +08:00
|
|
|
|
this.displayObject.anchorOffsetX = this._origin.x;
|
|
|
|
|
|
this.displayObject.anchorOffsetY = this._origin.y;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
2020-07-27 16:10:36 +08:00
|
|
|
|
this.displayObject = new Bitmap(sprite.texture2D);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置可渲染的原点
|
|
|
|
|
|
* @param origin
|
|
|
|
|
|
*/
|
2020-07-23 09:10:27 +08:00
|
|
|
|
public setOrigin(origin: Vector2): SpriteRenderer {
|
|
|
|
|
|
if (this._origin != origin) {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this._origin = origin;
|
2020-07-27 16:10:36 +08:00
|
|
|
|
this.displayObject.anchorOffsetX = this._origin.x;
|
|
|
|
|
|
this.displayObject.anchorOffsetY = this._origin.y;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this._areBoundsDirty = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 用归一化方法设置原点
|
|
|
|
|
|
* x/y 均为 0-1
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
2020-07-23 09:10:27 +08:00
|
|
|
|
public setOriginNormalized(value: Vector2): SpriteRenderer {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
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) {
|
2020-07-27 16:10:36 +08:00
|
|
|
|
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-22 23:30:31 +08:00
|
|
|
|
}
|
2020-07-01 14:19:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|