| 1 | class MenuManager extends GameObject |
| 2 | { |
| 3 | static Instance; |
| 4 | |
| 5 | buttons = []; |
| 6 | buttonPadding = 10; |
| 7 | buttonXOffset = -32; |
| 8 | state = 0; |
| 9 | prevState = 0; |
| 10 | |
| 11 | openTween = null; |
| 12 | openTweenTime = 800; |
| 13 | |
| 14 | closeTween = null; |
| 15 | closeTweenTime = 1400; |
| 16 | |
| 17 | tweenTimer = 0; |
| 18 | timeDiffPercent = 1; |
| 19 | isOpened = null; |
| 20 | isClosed = null; |
| 21 | isLoaded = null; |
| 22 | |
| 23 | static States = { closed: 0, open: 1, moving: 2 }; |
| 24 | |
| 25 | constructor (x, y, sprite, layer) |
| 26 | { |
| 27 | super(x, y, sprite, layer); |
| 28 | MenuManager.Instance = this; |
| 29 | |
| 30 | this.openTween = this.createOpenTween(this.transform.position, { x: 60 }, this.openTweenTime); |
| 31 | this.closeTween = this.createCloseTween(this.transform.position, { x: -15 }, this.closeTweenTime); |
| 32 | } |
| 33 | |
| 34 | getSubButton (label) |
| 35 | { |
| 36 | return this.buttons.filter(e => e.label == label)[0].button; |
| 37 | } |
| 38 | |
| 39 | addedToScene () |
| 40 | { |
| 41 | this.isOpened = new Signal(); |
| 42 | this.isClosed = new Signal(); |
| 43 | this.isLoaded = new Signal(); |
| 44 | |
| 45 | this.moveToOpen(() => this.moveToClose()); |
| 46 | this.buttons = |
| 47 | [ |
| 48 | { label: "btnFruit", button: this.scene.addObject(new BtnFruit(0, 0, this.scene.game.images.btnFruit, 4)) }, |
| 49 | { label: "btnToys", button: this.scene.addObject(new BtnToys(0, 0, this.scene.game.images.btnToys, 4)) }, |
| 50 | { label: "btnGames", button: this.scene.addObject(new BtnGames(0, 0, this.scene.game.images.btnGames, 4)) }, |
| 51 | { label: "btnPlants", button: this.scene.addObject(new BtnPlants(0, 0, this.scene.game.images.btnPlants, 4)) } |
| 52 | ]; |
| 53 | |
| 54 | this.updateButtonPosition(); |
| 55 | |
| 56 | this.isLoaded.dispatch(); |
| 57 | } |
| 58 | |
| 59 | onClick (btn) |
| 60 | { |
| 61 | this.prevState == MenuManager.States.closed ? |
| 62 | this.moveToClose() : |
| 63 | this.moveToOpen(); |
| 64 | } |
| 65 | |
| 66 | moveToOpen (onDone) |
| 67 | { |
| 68 | this.isOpened.dispatch(); |
| 69 | this.prevState = MenuManager.States.closed; |
| 70 | |
| 71 | if (this.state == MenuManager.States.moving) |
| 72 | this.closeTween.stop(); |
| 73 | |
| 74 | this.openTween.to({ x: 60 }, this.openTweenTime * this.timeDiffPercent); |
| 75 | |
| 76 | this.state = MenuManager.States.moving; |
| 77 | |
| 78 | let context = this; |
| 79 | this.openTween |
| 80 | .onComplete(() => { this.state = MenuManager.States.open; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); }) |
| 81 | .start(); |
| 82 | } |
| 83 | |
| 84 | moveToClose (onDone) |
| 85 | { |
| 86 | this.isClosed.dispatch(); |
| 87 | this.prevState = MenuManager.States.open; |
| 88 | |
| 89 | if (this.state == MenuManager.States.moving) |
| 90 | this.openTween.stop(); |
| 91 | |
| 92 | this.closeTween.to({ x: -15 }, this.closeTweenTime * this.timeDiffPercent); |
| 93 | |
| 94 | this.state = MenuManager.States.moving; |
| 95 | |
| 96 | let context = this; |
| 97 | this.closeTween |
| 98 | .onComplete(() => { this.state = MenuManager.States.closed; this.tweenTimer = 0; this.timeDiffPercent = 1; onDone && onDone.call(context); }) |
| 99 | .start(); |
| 100 | } |
| 101 | |
| 102 | createOpenTween (target, newTarget, length) |
| 103 | { |
| 104 | return new TWEEN.Tween(target) |
| 105 | .to(newTarget, length) |
| 106 | .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.openTweenTime); this.tweenTimer = 0; }) |
| 107 | .onUpdate(() => |
| 108 | { |
| 109 | this.updateButtonPosition(); |
| 110 | this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate()); |
| 111 | }) |
| 112 | .easing(TWEEN.Easing.Bounce.Out); |
| 113 | } |
| 114 | |
| 115 | createCloseTween (target, newTarget, length) |
| 116 | { |
| 117 | return new TWEEN.Tween(target) |
| 118 | .to(newTarget, length) |
| 119 | .onStop(() => { this.timeDiffPercent = this.calculateTimeLeftPercent(this.closeTweenTime); this.tweenTimer = 0; }) |
| 120 | .onUpdate(() => |
| 121 | { |
| 122 | this.updateButtonPosition(); |
| 123 | }) |
| 124 | .easing(TWEEN.Easing.Elastic.Out); |
| 125 | } |
| 126 | |
| 127 | calculateTimeLeftPercent (expectedTimeLength) |
| 128 | { |
| 129 | let t = Math.round(this.tweenTimer * 1000); |
| 130 | if (t == 0) |
| 131 | return 1; |
| 132 | else if (t < expectedTimeLength) |
| 133 | return Math.max(t / expectedTimeLength, .5); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | updateButtonPosition () |
| 138 | { |
| 139 | for (let i = 0; i < this.buttons.length; i++) |
| 140 | { |
| 141 | if (i == 0) |
| 142 | { |
| 143 | this.buttons[i].button.transform.position.x = this.transform.position.x + this.buttonXOffset; |
| 144 | this.buttons[i].button.transform.position.y = this.transform.position.y + this.transform.size.y + this.buttonPadding; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | this.buttons[i].button.transform.position.x = this.transform.position.x + this.buttonXOffset; |
| 149 | this.buttons[i].button.transform.position.y = this.buttons[i - 1].button.transform.position.y + this.buttons[i - 1].button.transform.size.y + this.buttonPadding; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | update (deltaTime) |
| 155 | { |
| 156 | super.update(deltaTime); |
| 157 | |
| 158 | if (this.state == MenuManager.States.moving) |
| 159 | { |
| 160 | this.tweenTimer += deltaTime; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | if (this.tweenTimer > 0) |
| 165 | { |
| 166 | this.tweenTimer = 0; |
| 167 | this.timeDiffPercent = 1; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |