Files
esengine/source/src/ECS/Components/TiledSpriteRenderer.ts
T

114 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-07-07 21:40:57 +08:00
///<reference path="./SpriteRenderer.ts" />
2020-07-23 09:10:27 +08:00
module es {
2020-08-07 08:50:26 +08:00
import Bitmap = egret.Bitmap;
2020-07-23 09:10:27 +08:00
/**
* 滚动由两张图片组合而成
*/
export class TiledSpriteRenderer extends SpriteRenderer {
2020-08-07 08:50:26 +08:00
public get bounds(): Rectangle {
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.width, this.height);
this._areBoundsDirty = false;
}
}
return this._bounds;
}
/**
* 纹理滚动的x值
*/
public get scrollX() {
return this._sourceRect.x;
}
/**
* 纹理滚动的x值
* @param value
*/
public set scrollX(value: number) {
this._sourceRect.x = value;
}
/**
* 纹理滚动的y值
*/
public get scrollY() {
return this._sourceRect.y;
}
/**
* 纹理滚动的y值
* @param value
*/
public set scrollY(value: number) {
this._sourceRect.y = value;
}
/**
* 纹理比例尺
*/
public get textureScale(): Vector2 {
return this._textureScale;
}
/**
* 纹理比例尺
* @param value
*/
public set textureScale(value: Vector2) {
this._textureScale = value;
// 重新计算我们的inverseTextureScale和源矩形大小
this._inverseTexScale = new Vector2(1 / this._textureScale.x, 1 / this._textureScale.y);
this._sourceRect.width = this._sprite.sourceRect.width * this._inverseTexScale.x;
this._sourceRect.height = this._sprite.sourceRect.height * this._inverseTexScale.y;
}
/**
* 覆盖宽度值,这样TiledSprite可以有一个独立于其纹理的宽度
*/
public get width(): number{
return this._sourceRect.width;
}
public set width(value: number) {
this._areBoundsDirty = true;
this._sourceRect.width = value;
}
public get height(): number {
return this._sourceRect.height;
}
public set height(value: number) {
this._areBoundsDirty = true;
this._sourceRect.height = value;
}
protected _sourceRect: Rectangle = new Rectangle();
protected _textureScale = Vector2.one;
protected _inverseTexScale = Vector2.one;
2020-07-03 17:51:18 +08:00
2020-07-23 09:10:27 +08:00
constructor(sprite: Sprite) {
super(sprite);
2020-08-07 08:50:26 +08:00
this._sourceRect = sprite.sourceRect;
2020-07-23 09:10:27 +08:00
2020-08-07 08:50:26 +08:00
let bitmap = this.displayObject as Bitmap;
bitmap.$fillMode = egret.BitmapFillMode.REPEAT;
2020-07-28 16:25:20 +08:00
}
2020-07-23 09:10:27 +08:00
public render(camera: es.Camera) {
2020-08-07 08:50:26 +08:00
let bitmap = this.displayObject as Bitmap;
bitmap.width = this.width;
bitmap.height = this.height;
2020-07-23 09:10:27 +08:00
super.render(camera);
}
2020-07-03 17:51:18 +08:00
}
2020-07-23 09:10:27 +08:00
}