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