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

61 lines
1.9 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 {
/**
*
*/
export class TiledSpriteRenderer extends SpriteRenderer {
protected sourceRect: Rectangle;
protected leftTexture: egret.Bitmap;
protected rightTexture: egret.Bitmap;
2020-07-28 16:25:20 +08:00
constructor(sprite: Sprite) {
super(sprite);
this.leftTexture = new egret.Bitmap();
this.rightTexture = new egret.Bitmap();
this.leftTexture.texture = sprite.texture2D;
this.rightTexture.texture = sprite.texture2D;
this.setSprite(sprite);
this.sourceRect = sprite.sourceRect;
}
2020-07-23 09:10:27 +08:00
public get scrollX() {
return this.sourceRect.x;
}
2020-07-28 16:25:20 +08:00
2020-07-23 09:10:27 +08:00
public set scrollX(value: number) {
this.sourceRect.x = value;
}
2020-07-28 16:25:20 +08:00
2020-07-23 09:10:27 +08:00
public get scrollY() {
return this.sourceRect.y;
}
2020-07-28 16:25:20 +08:00
2020-07-23 09:10:27 +08:00
public set scrollY(value: number) {
this.sourceRect.y = value;
}
public render(camera: es.Camera) {
if (!this.sprite)
return;
super.render(camera);
let renderTexture = new egret.RenderTexture();
let cacheBitmap = new egret.DisplayObjectContainer();
cacheBitmap.removeChildren();
cacheBitmap.addChild(this.leftTexture);
cacheBitmap.addChild(this.rightTexture);
this.leftTexture.x = this.sourceRect.x;
this.rightTexture.x = this.sourceRect.x - this.sourceRect.width;
this.leftTexture.y = this.sourceRect.y;
this.rightTexture.y = this.sourceRect.y;
cacheBitmap.cacheAsBitmap = true;
renderTexture.drawToTexture(cacheBitmap, new egret.Rectangle(0, 0, this.sourceRect.width, this.sourceRect.height));
}
2020-07-03 17:51:18 +08:00
}
2020-07-23 09:10:27 +08:00
}