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

149 lines
4.3 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;
import RenderTexture = egret.RenderTexture;
2020-08-07 08:50:26 +08:00
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;
}
}
2020-07-28 16:25:20 +08:00
2020-08-07 08:50:26 +08:00
return this._bounds;
2020-07-28 16:25:20 +08:00
}
2020-08-07 08:50:26 +08:00
/**
* x值
*/
2020-07-23 09:10:27 +08:00
public get scrollX() {
2020-08-07 08:50:26 +08:00
return this._sourceRect.x;
2020-07-23 09:10:27 +08:00
}
2020-07-28 16:25:20 +08:00
2020-08-07 08:50:26 +08:00
/**
* x值
* @param value
*/
2020-07-23 09:10:27 +08:00
public set scrollX(value: number) {
2020-08-07 08:50:26 +08:00
this._sourceRect.x = value;
2020-07-23 09:10:27 +08:00
}
2020-07-28 16:25:20 +08:00
2020-08-07 08:50:26 +08:00
/**
* y值
*/
2020-07-23 09:10:27 +08:00
public get scrollY() {
2020-08-07 08:50:26 +08:00
return this._sourceRect.y;
2020-07-23 09:10:27 +08:00
}
2020-07-28 16:25:20 +08:00
2020-08-07 08:50:26 +08:00
/**
* y值
* @param value
*/
2020-07-23 09:10:27 +08:00
public set scrollY(value: number) {
2020-08-07 08:50:26 +08:00
this._sourceRect.y = value;
2020-07-23 09:10:27 +08:00
}
2020-08-07 08:50:26 +08:00
/**
*
*/
public get textureScale(): Vector2 {
return this._textureScale;
}
2020-07-23 09:10:27 +08:00
2020-08-07 08:50:26 +08:00
/**
*
* @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;
}
2020-07-23 09:10:27 +08:00
2020-08-07 08:50:26 +08:00
public set height(value: number) {
this._areBoundsDirty = true;
this._sourceRect.height = value;
}
2020-07-23 09:10:27 +08:00
public get gapXY(): Vector2{
return new Vector2(this._gapX, this._gapY);
}
public set gapXY(value: Vector2){
this._gapX = value.x;
this._gapY = value.y;
let renderTexture = new RenderTexture();
let newRectangle = this.sprite.sourceRect;
newRectangle.x = 0;
newRectangle.y = 0;
newRectangle.width += this._gapX;
newRectangle.height += this._gapY;
renderTexture.drawToTexture(this.displayObject, newRectangle);
2020-08-08 09:06:32 +08:00
if (!this.displayObject){
this.displayObject = new Bitmap(renderTexture);
}else{
(this.displayObject as Bitmap).texture = renderTexture;
}
}
2020-08-07 11:02:04 +08:00
protected _sourceRect: Rectangle;
2020-08-07 08:50:26 +08:00
protected _textureScale = Vector2.one;
protected _inverseTexScale = Vector2.one;
private _gapX = 0;
private _gapY = 0;
2020-08-07 08:50:26 +08:00
constructor(sprite: Sprite) {
super(sprite);
2020-07-23 09:10:27 +08:00
2020-08-07 08:50:26 +08:00
this._sourceRect = sprite.sourceRect;
let bitmap = this.displayObject as Bitmap;
bitmap.$fillMode = egret.BitmapFillMode.REPEAT;
}
2020-08-08 09:06:32 +08:00
/**
*
* @param value
*/
public setGapXY(value: Vector2): TiledSpriteRenderer {
this.gapXY = value;
return this;
}
2020-08-07 08:50:26 +08:00
public render(camera: es.Camera) {
2020-08-07 11:02:04 +08:00
super.render(camera);
2020-08-07 08:50:26 +08:00
let bitmap = this.displayObject as Bitmap;
bitmap.width = this.width;
bitmap.height = this.height;
2020-08-07 11:02:04 +08:00
bitmap.scrollRect = this._sourceRect;
2020-07-23 09:10:27 +08:00
}
2020-07-03 17:51:18 +08:00
}
2020-07-23 09:10:27 +08:00
}