class Renderer { sprite = null; gameObject = null; subImage = 0; alpha = 1; mirrorX = false; mirrorY = false; cycle = null; visible = true; constructor(parentObject, sprite) { this.gameObject = parentObject; this.sprite = sprite; } update(deltaTime) { if(this.cycle) { if(!this.cycle.cycleSpeed || this.cycle.cycleSpeed == -1) { this.subImage = this.cycle.default; this.cycle.current = this.cycle.default; return; } this.cycle.timer += deltaTime; if(this.cycle.timer > this.cycle.cycleSpeed) { this.cycle.timer -= this.cycle.cycleSpeed; if(this.cycle.loop) { let next = (this.cycle.current + 1) % this.cycle.cycleArray.length; } else { if(this.cycle.current + 1 > this.cycle.cycleArray.length) { if(this.cycle.chain) { this.setAnimation(this.cycle.chain, this.cycle.chainDefaultIndex, this.cycle.chainSpeed); return; } } else { this.cycle.current ++; } } this.updateCycleSprite(next) } } } updateCycleSprite(index) { this.subImage = this.cycle.cycleArray[index]; this.cycle.current = index; } draw() { if(this.sprite) { var offX = this.gameObject.scene.cameraPosition.x; var offY = this.gameObject.scene.cameraPosition.y; this.drawSprite(this.gameObject.transform.position.x + offX, this.gameObject.transform.position.y + offY, this.gameObject.transform.size.x, this.gameObject.transform.size.y, this.gameObject.transform.rotation); } } drawSprite() { if(isNaN(this.subImage)) { this.subImage = this.g_subImgOld; } else { this.g_subImgOld = this.subImage; } var subImage = Math.round(this.subImage); subImage %= this.sprite.subImages; var c = subImage % this.sprite.subImagesPerRow; var r = Math.floor(subImage / this.sprite.subImagesPerRow); var sw = (this.sprite.g_image.width / this.sprite.subImagesPerRow); var sh = (this.sprite.g_image.height / Math.ceil(this.sprite.subImages / this.sprite.subImagesPerRow)); var sx = sw * c; var sy = sh * r; this.sprite.draw( this.gameObject.scene, sx, sy, sw, sh, this.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.size.x, this.gameObject.transform.size.y, this.gameObject.transform.rotation, this.mirrorX, this.mirrorY, this.alpha ); } setAnimation(label, defaultIndex, cycleSpeed = -1) { if(this.cycle) { if(this.cycle.label == label) { this.cycle.defaultIndex = defaultIndex; this.cycle.cycleSpeed = cycleSpeed; return; } } this.cycle = { label: label, cycleArray: this.sprite.animationCycles[label], default: defaultIndex, current: defaultIndex, cycleSpeed: cycleSpeed, timer: 0}; this.subImage = this.cycle.cycleArray[this.cycle.current]; } setAnimationCycleSpeed(cycleSpeed) { if(this.cycle) { this.cycle.cycleSpeed = cycleSpeed; } } setSubImage(subImage) { this.subImage = subImage; this.cycle = null; } }