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

57 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-07-07 21:40:57 +08:00
///<reference path="./SpriteRenderer.ts" />
/**
*
*/
2020-07-03 17:51:18 +08:00
class TiledSpriteRenderer extends SpriteRenderer {
protected sourceRect: Rectangle;
2020-07-07 21:40:57 +08:00
protected leftTexture: egret.Bitmap;
protected rightTexture: egret.Bitmap;
2020-07-03 17:51:18 +08:00
2020-07-07 21:40:57 +08:00
public get scrollX() {
2020-07-03 17:51:18 +08:00
return this.sourceRect.x;
}
2020-07-07 21:40:57 +08:00
public set scrollX(value: number) {
2020-07-03 17:51:18 +08:00
this.sourceRect.x = value;
}
2020-07-07 21:40:57 +08:00
public get scrollY() {
2020-07-03 17:51:18 +08:00
return this.sourceRect.y;
}
2020-07-07 21:40:57 +08:00
public set scrollY(value: number) {
2020-07-03 17:51:18 +08:00
this.sourceRect.y = value;
}
2020-07-07 21:40:57 +08:00
constructor(sprite: Sprite) {
2020-07-03 17:51:18 +08:00
super();
2020-07-07 21:40:57 +08:00
this.leftTexture = new egret.Bitmap();
this.rightTexture = new egret.Bitmap();
this.leftTexture.texture = sprite.texture2D;
this.rightTexture.texture = sprite.texture2D;
2020-07-03 17:51:18 +08:00
this.setSprite(sprite);
this.sourceRect = sprite.sourceRect;
}
2020-07-07 21:40:57 +08:00
public render(camera: Camera) {
2020-07-03 17:51:18 +08:00
if (!this.sprite)
return;
super.render(camera);
let renderTexture = new egret.RenderTexture();
2020-07-07 21:40:57 +08:00
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
this.bitmap.texture = renderTexture;
}
}