| 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 | openTargetX = 60; |
| 12 | openTweenTime = 800; |
| 13 | |
| 14 | closedTargetX = -15; |
| 15 | closeTweenTime = 1400; |
| 16 | |
| 17 | isOpened = null; |
| 18 | isClosed = null; |
| 19 | isLoaded = null; |
| 20 | |
| 21 | activeTween = 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 | |
| 31 | getSubButton (label) |
| 32 | { |
| 33 | return this.buttons.filter(e => e.label == label)[0].button; |
| 34 | } |
| 35 | |
| 36 | addedToScene () |
| 37 | { |
| 38 | this.isOpened = new Signal(); |
| 39 | this.isClosed = new Signal(); |
| 40 | this.isLoaded = new Signal(); |
| 41 | |
| 42 | this.moveToOpen(() => this.moveToClose()); |
| 43 | this.buttons = |
| 44 | [ |
| 45 | { label: "btnFruit", button: this.scene.addObject(new BtnFruit(0, 0, this.scene.game.images.btnFruit, 4)) }, |
| 46 | { label: "btnToys", button: this.scene.addObject(new BtnToys(0, 0, this.scene.game.images.btnToys, 4)) }, |
| 47 | { label: "btnGames", button: this.scene.addObject(new BtnGames(0, 0, this.scene.game.images.btnGames, 4)) }, |
| 48 | { label: "btnPlants", button: this.scene.addObject(new BtnPlants(0, 0, this.scene.game.images.btnPlants, 4)) } |
| 49 | ]; |
| 50 | |
| 51 | this.updateButtonPosition(); |
| 52 | |
| 53 | this.isLoaded.dispatch(); |
| 54 | } |
| 55 | |
| 56 | onClick (btn) |
| 57 | { |
| 58 | this.prevState == MenuManager.States.closed ? |
| 59 | this.moveToClose() : |
| 60 | this.moveToOpen(); |
| 61 | } |
| 62 | |
| 63 | moveToOpen (onDone) |
| 64 | { |
| 65 | this.isOpened.dispatch(); |
| 66 | this.prevState = MenuManager.States.closed; |
| 67 | |
| 68 | this.state = MenuManager.States.moving; |
| 69 | |
| 70 | this.createOpenTween(onDone); |
| 71 | } |
| 72 | |
| 73 | moveToClose (onDone) |
| 74 | { |
| 75 | this.isClosed.dispatch(); |
| 76 | this.prevState = MenuManager.States.open; |
| 77 | |
| 78 | this.state = MenuManager.States.moving; |
| 79 | |
| 80 | this.createCloseTween(onDone); |
| 81 | } |
| 82 | |
| 83 | createOpenTween (onDone) |
| 84 | { |
| 85 | let context = this; |
| 86 | let tween = new TWEEN.Tween(this.transform.position) |
| 87 | .to({ x: this.openTargetX }, this.openTweenTime) |
| 88 | .onUpdate(() => |
| 89 | { |
| 90 | this.updateButtonPosition(); |
| 91 | this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate()); |
| 92 | }) |
| 93 | .easing(TWEEN.Easing.Bounce.Out) |
| 94 | .onComplete(() => { this.state = MenuManager.States.open; this.activeTween = null; onDone && onDone.call(context); }) |
| 95 | .start(); |
| 96 | |
| 97 | return tween.onStart(() => this.stopActiveTween(tween)); |
| 98 | } |
| 99 | |
| 100 | createCloseTween (onDone) |
| 101 | { |
| 102 | let context = this; |
| 103 | let tween = new TWEEN.Tween(this.transform.position) |
| 104 | .to({ x: this.closedTargetX }, this.closeTweenTime) |
| 105 | .onUpdate(() => |
| 106 | { |
| 107 | this.updateButtonPosition(); |
| 108 | this.buttons.forEach(e => e.button.onUpdate && e.button.onUpdate()); |
| 109 | }) |
| 110 | .easing(TWEEN.Easing.Elastic.Out) |
| 111 | .onComplete(() => { this.state = MenuManager.States.closed; this.activeTween = null; onDone && onDone.call(context); }) |
| 112 | .start(); |
| 113 | |
| 114 | return tween.onStart(() => this.stopActiveTween(tween)); |
| 115 | } |
| 116 | |
| 117 | stopActiveTween (tween) |
| 118 | { |
| 119 | if (this.activeTween) |
| 120 | { |
| 121 | this.activeTween.stop(); |
| 122 | } |
| 123 | this.activeTween = tween; |
| 124 | } |
| 125 | |
| 126 | updateButtonPosition () |
| 127 | { |
| 128 | for (let i = 0; i < this.buttons.length; i++) |
| 129 | { |
| 130 | if (i == 0) |
| 131 | { |
| 132 | this.buttons[i].button.transform.position.x = this.transform.position.x + this.buttonXOffset; |
| 133 | this.buttons[i].button.transform.position.y = this.transform.position.y + this.transform.size.y + this.buttonPadding; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | this.buttons[i].button.transform.position.x = this.transform.position.x + this.buttonXOffset; |
| 138 | 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; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |