| 1 | class Renderer |
| 2 | { |
| 3 | sprite = null; |
| 4 | gameObject = null; |
| 5 | subImage = 0; |
| 6 | alpha = 1; |
| 7 | mirrorX = false; |
| 8 | mirrorY = false; |
| 9 | |
| 10 | defaultAnimationSpeed = 1; |
| 11 | |
| 12 | cycle = null; |
| 13 | |
| 14 | visible = true; |
| 15 | |
| 16 | shader = null; |
| 17 | shaderUniforms = null; |
| 18 | |
| 19 | // Optional maps for builtin sprite effects (framework `image` instances). |
| 20 | bumpMap = null; |
| 21 | secondaryMap = null; |
| 22 | |
| 23 | // Dynamic lights for bumpmap: [{ x, y, color: [r,g,b]|{r,g,b} }, ...] |
| 24 | lights = null; |
| 25 | |
| 26 | // RenderLayer name; null uses RenderLayer.DEFAULT_NAME ("default"). |
| 27 | layer = null; |
| 28 | |
| 29 | constructor (parentObject, sprite) |
| 30 | { |
| 31 | this.gameObject = parentObject; |
| 32 | this.sprite = sprite; |
| 33 | this.shader = null; |
| 34 | this.shaderUniforms = null; |
| 35 | this.bumpMap = null; |
| 36 | this.secondaryMap = null; |
| 37 | this.lights = null; |
| 38 | this.layer = null; |
| 39 | } |
| 40 | |
| 41 | get layerName () |
| 42 | { |
| 43 | return this.layer || RenderLayer.DEFAULT_NAME; |
| 44 | } |
| 45 | |
| 46 | update (deltaTime) |
| 47 | { |
| 48 | if (this.cycle) |
| 49 | { |
| 50 | if (!this.cycle.cycleSpeed || this.cycle.cycleSpeed == -1) |
| 51 | { |
| 52 | this.subImage = this.cycle.cycleArray[this.cycle.default]; |
| 53 | this.cycle.current = this.cycle.default; |
| 54 | return; |
| 55 | } |
| 56 | this.cycle.timer += deltaTime; |
| 57 | |
| 58 | if (this.cycle.timer > this.cycle.cycleSpeed) |
| 59 | { |
| 60 | this.cycle.timer -= this.cycle.cycleSpeed; |
| 61 | let next; |
| 62 | |
| 63 | if (this.cycle.loop) |
| 64 | { |
| 65 | next = (this.cycle.current + 1) % this.cycle.cycleArray.length; |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | if (this.cycle.current + 1 >= this.cycle.cycleArray.length) |
| 70 | { |
| 71 | if (this.cycle.chain) |
| 72 | { |
| 73 | this.cycle.chainDefaultIndex = this.cycle.chainDefaultIndex || this.cycle.default; |
| 74 | this.cycle.chainSpeed = this.cycle.chainSpeed || this.cycle.cycleSpeed; |
| 75 | this.setAnimation(this.cycle.chain, this.cycle.chainDefaultIndex, this.cycle.chainSpeed); |
| 76 | } |
| 77 | |
| 78 | return; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | next = this.cycle.current + 1; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | this.updateCycleSprite(next); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | updateCycleSprite (index) |
| 92 | { |
| 93 | this.subImage = this.cycle.cycleArray[index]; |
| 94 | this.cycle.current = index; |
| 95 | } |
| 96 | |
| 97 | draw () |
| 98 | { |
| 99 | if (this.sprite) |
| 100 | { |
| 101 | var offX = this.gameObject.scene.cameraPosition.x; |
| 102 | var offY = this.gameObject.scene.cameraPosition.y; |
| 103 | |
| 104 | this.drawSprite(this.gameObject.transform.position.x + offX, |
| 105 | this.gameObject.transform.position.y + offY, |
| 106 | this.gameObject.transform.size.x, |
| 107 | this.gameObject.transform.size.y, |
| 108 | this.gameObject.transform.rotation); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | drawSprite (dx, dy, dw, dh, rotation) |
| 113 | { |
| 114 | if (isNaN(this.subImage)) |
| 115 | { |
| 116 | this.subImage = this.g_subImgOld; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | this.g_subImgOld = this.subImage; |
| 121 | } |
| 122 | |
| 123 | const textures = {}; |
| 124 | if (this.bumpMap) |
| 125 | { textures.u_bump = this.bumpMap; } |
| 126 | if (this.secondaryMap) |
| 127 | { textures.u_secondary = this.secondaryMap; } |
| 128 | |
| 129 | this.sprite.draw( |
| 130 | this.gameObject.scene, |
| 131 | Math.round(dx), |
| 132 | Math.round(dy), |
| 133 | dw, |
| 134 | dh, |
| 135 | rotation, |
| 136 | this.mirrorX, |
| 137 | this.mirrorY, |
| 138 | this.alpha, |
| 139 | this.subImage, |
| 140 | this.shader, |
| 141 | this.shaderUniforms, |
| 142 | textures, |
| 143 | this.lights |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | get currentAnimationName () |
| 148 | { |
| 149 | return this.cycle ? this.cycle.label : null; |
| 150 | } |
| 151 | |
| 152 | setAnimation (label, defaultIndex = 0, cycleSpeed = -1) |
| 153 | { |
| 154 | if (cycleSpeed < 0) |
| 155 | { |
| 156 | cycleSpeed = this.defaultAnimationSpeed; |
| 157 | } |
| 158 | |
| 159 | if (!this.sprite || !this.sprite.animationCycles || !this.sprite.animationCycles[label]) |
| 160 | { |
| 161 | console.warn("Renderer.setAnimation: unknown animation '" + label + "'"); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | if (this.cycle) |
| 166 | { |
| 167 | if (this.cycle.label == label) |
| 168 | { |
| 169 | this.cycle.default = defaultIndex; |
| 170 | this.cycle.cycleSpeed = cycleSpeed; |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | const cycleData = this.sprite.animationCycles[label]; |
| 176 | |
| 177 | this.cycle = { |
| 178 | label: label, |
| 179 | cycleArray: cycleData.frames, |
| 180 | default: defaultIndex, |
| 181 | current: defaultIndex, |
| 182 | cycleSpeed: cycleSpeed, |
| 183 | loop: cycleData.loop, |
| 184 | chain: cycleData.chain, |
| 185 | chainDefaultIndex: cycleData.chainDefaultIndex, |
| 186 | chainSpeed: cycleData.chainSpeed, |
| 187 | timer: 0 |
| 188 | }; |
| 189 | |
| 190 | this.subImage = this.cycle.cycleArray[this.cycle.current]; |
| 191 | } |
| 192 | |
| 193 | setAnimationCycleSpeed (cycleSpeed) |
| 194 | { |
| 195 | if (this.cycle) |
| 196 | { |
| 197 | this.cycle.cycleSpeed = cycleSpeed; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | setSubImage (subImage) |
| 202 | { |
| 203 | this.subImage = subImage; |
| 204 | this.cycle = null; |
| 205 | } |
| 206 | } |