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