class Renderer { sprite = null; gameObject = null; subImage = 0; alpha = 1; mirrorX = false; mirrorY = false; defaultAnimationSpeed = 1; cycle = null; visible = true; shader = null; shaderUniforms = null; // Optional maps for builtin sprite effects (framework `image` instances). bumpMap = null; secondaryMap = null; // Dynamic lights for bumpmap: [{ x, y, color: [r,g,b]|{r,g,b} }, ...] lights = null; // RenderLayer name; null uses RenderLayer.DEFAULT_NAME ("default"). layer = null; constructor (parentObject, sprite) { this.gameObject = parentObject; this.sprite = sprite; this.shader = null; this.shaderUniforms = null; this.bumpMap = null; this.secondaryMap = null; this.lights = null; this.layer = null; } get layerName () { return this.layer || RenderLayer.DEFAULT_NAME; } update (deltaTime) { if (this.cycle) { if (!this.cycle.cycleSpeed || this.cycle.cycleSpeed == -1) { this.subImage = this.cycle.cycleArray[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; let next; if (this.cycle.loop) { next = (this.cycle.current + 1) % this.cycle.cycleArray.length; } else { if (this.cycle.current + 1 >= this.cycle.cycleArray.length) { if (this.cycle.chain) { this.cycle.chainDefaultIndex = this.cycle.chainDefaultIndex || this.cycle.default; this.cycle.chainSpeed = this.cycle.chainSpeed || this.cycle.cycleSpeed; this.setAnimation(this.cycle.chain, this.cycle.chainDefaultIndex, this.cycle.chainSpeed); } return; } else { next = this.cycle.current + 1; } } 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 (dx, dy, dw, dh, rotation) { if (isNaN(this.subImage)) { this.subImage = this.g_subImgOld; } else { this.g_subImgOld = this.subImage; } const textures = {}; if (this.bumpMap) { textures.u_bump = this.bumpMap; } if (this.secondaryMap) { textures.u_secondary = this.secondaryMap; } this.sprite.draw( this.gameObject.scene, Math.round(dx), Math.round(dy), dw, dh, rotation, this.mirrorX, this.mirrorY, this.alpha, this.subImage, this.shader, this.shaderUniforms, textures, this.lights ); } get currentAnimationName () { return this.cycle ? this.cycle.label : null; } setAnimation (label, defaultIndex = 0, cycleSpeed = -1) { if (cycleSpeed < 0) { cycleSpeed = this.defaultAnimationSpeed; } if (!this.sprite || !this.sprite.animationCycles || !this.sprite.animationCycles[label]) { console.warn("Renderer.setAnimation: unknown animation '" + label + "'"); return; } if (this.cycle) { if (this.cycle.label == label) { this.cycle.default = defaultIndex; this.cycle.cycleSpeed = cycleSpeed; return; } } const cycleData = this.sprite.animationCycles[label]; this.cycle = { label: label, cycleArray: cycleData.frames, default: defaultIndex, current: defaultIndex, cycleSpeed: cycleSpeed, loop: cycleData.loop, chain: cycleData.chain, chainDefaultIndex: cycleData.chainDefaultIndex, chainSpeed: cycleData.chainSpeed, 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; } }