| 1 | class Renderer |
| 2 | { |
| 3 | sprite = null; |
| 4 | gameObject = null; |
| 5 | subImage = 0; |
| 6 | alpha = 1; |
| 7 | mirrorX = false; |
| 8 | mirrorY = false; |
| 9 | |
| 10 | cycle = null; |
| 11 | |
| 12 | visible = true; |
| 13 | |
| 14 | constructor(parentObject, sprite) |
| 15 | { |
| 16 | this.gameObject = parentObject; |
| 17 | this.sprite = sprite; |
| 18 | } |
| 19 | |
| 20 | update(deltaTime) |
| 21 | { |
| 22 | if(this.cycle) |
| 23 | { |
| 24 | if(!this.cycle.cycleSpeed || this.cycle.cycleSpeed == -1) |
| 25 | { |
| 26 | this.subImage = this.cycle.default; |
| 27 | this.cycle.current = this.cycle.default; |
| 28 | return; |
| 29 | } |
| 30 | this.cycle.timer += deltaTime; |
| 31 | |
| 32 | if(this.cycle.timer > this.cycle.cycleSpeed) |
| 33 | { |
| 34 | this.cycle.timer -= this.cycle.cycleSpeed; |
| 35 | |
| 36 | let next = (this.cycle.current + 1) % this.cycle.cycleArray.length; |
| 37 | this.updateCycleSprite(next) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | updateCycleSprite(index) { |
| 43 | this.subImage = this.cycle.cycleArray[index]; |
| 44 | this.cycle.current = index; |
| 45 | } |
| 46 | |
| 47 | draw() |
| 48 | { |
| 49 | if(this.sprite) |
| 50 | { |
| 51 | var offX = this.gameObject.scene.cameraPosition.x; |
| 52 | var offY = this.gameObject.scene.cameraPosition.y; |
| 53 | |
| 54 | this.drawSprite(this.gameObject.transform.position.x + offX, |
| 55 | this.gameObject.transform.position.y + offY, |
| 56 | this.gameObject.transform.size.x, |
| 57 | this.gameObject.transform.size.y, |
| 58 | this.gameObject.transform.rotation); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | drawSprite() |
| 63 | { |
| 64 | if(isNaN(this.subImage)) |
| 65 | { |
| 66 | this.subImage = this.g_subImgOld; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | this.g_subImgOld = this.subImage; |
| 71 | } |
| 72 | |
| 73 | var subImage = Math.round(this.subImage); |
| 74 | subImage %= this.sprite.subImages; |
| 75 | |
| 76 | var c = subImage % this.sprite.subImagesPerRow; |
| 77 | var r = Math.floor(subImage / this.sprite.subImagesPerRow); |
| 78 | var sw = (this.sprite.g_image.width / this.sprite.subImagesPerRow); |
| 79 | var sh = (this.sprite.g_image.height / Math.ceil(this.sprite.subImages / this.sprite.subImagesPerRow)); |
| 80 | var sx = sw * c; |
| 81 | var sy = sh * r; |
| 82 | |
| 83 | this.sprite.draw( |
| 84 | this.gameObject.scene, |
| 85 | sx, sy, sw, sh, |
| 86 | this.gameObject.transform.position.x, |
| 87 | this.gameObject.transform.position.y, |
| 88 | this.gameObject.transform.size.x, |
| 89 | this.gameObject.transform.size.y, |
| 90 | this.gameObject.transform.rotation, |
| 91 | this.mirrorX, |
| 92 | this.mirrorY, |
| 93 | this.alpha |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | setAnimation(label, defaultIndex, cycleSpeed = -1) |
| 98 | { |
| 99 | if(this.cycle) |
| 100 | { |
| 101 | if(this.cycle.label == label) |
| 102 | { |
| 103 | this.cycle.defaultIndex = defaultIndex; |
| 104 | this.cycle.cycleSpeed = cycleSpeed; |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | this.cycle = { |
| 110 | label: label, |
| 111 | cycleArray: this.sprite.animationCycles[label], |
| 112 | default: defaultIndex, |
| 113 | current: defaultIndex, |
| 114 | cycleSpeed: cycleSpeed, |
| 115 | timer: 0}; |
| 116 | |
| 117 | this.subImage = this.cycle.cycleArray[this.cycle.current]; |
| 118 | } |
| 119 | |
| 120 | setAnimationCycleSpeed(cycleSpeed) |
| 121 | { |
| 122 | if(this.cycle) |
| 123 | { |
| 124 | this.cycle.cycleSpeed = cycleSpeed; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | setSubImage(subImage) |
| 129 | { |
| 130 | this.subImage = subImage; |
| 131 | this.cycle = null; |
| 132 | } |
| 133 | } |