class BtnGames extends GameObject { buttons = []; buttonPadding = 10; state = 0; prevState = 0; subBtnStartPosX = -32; constructor (x, y, sprite, layer) { super(x, y, sprite, layer); } addedToScene () { MenuManager.Instance.isLoaded.add(() => this.initializeButtons(), this); MenuManager.Instance.isClosed.add(() => this.moveToClose(), this); } initializeButtons () { this.createSubButton(this.scene.game.images.btnFrog, function () { MinigameInitialiser.StartFrogGame(this.scene); MenuManager.Instance.moveToClose(); }); for (let i = 0; i < this.buttons.length; i++) { if (i == 0) { this.buttons[i].endPos = new vector(this.transform.position.x + this.transform.size.x * 2 + this.buttonPadding, this.transform.position.y + this.buttonPadding); } else { this.buttons[i].endPos = new vector( this.buttons[i - 1].endPos.x, this.buttons[i - 1].endPos.y + this.buttons[i - 1].transform.size.y + this.buttonPadding); } } } createSubButton (image, onClick) { let button = new GameObject(this.subBtnStartPosX, this.transform.position.y, image, 3); this.scene.addObject(button); button.startPos = new vector(-32, this.transform.position.y); button.onClick = onClick; this.buttons.push(button); } onClick (btn) { this.state == MenuManager.States.closed ? this.moveToOpen() : this.moveToClose(); } moveToOpen () { if (this.state == MenuManager.States.open) return; this.state = MenuManager.States.open; this.buttons.forEach(e => this.createOpenTween(e.transform.position, { x: [e.endPos.x], y: [e.startPos.y, e.startPos.y, e.startPos.y, e.endPos.y] }, 500).start()); } moveToClose () { if (this.state == MenuManager.States.closed) return; this.state = MenuManager.States.closed; this.buttons.forEach(e => this.createCloseTween(e.transform.position, { x: [e.endPos.x, e.endPos.x, e.endPos.x, e.endPos.x, e.startPos.x], y: [e.startPos.y] }, 1000).start()); } createOpenTween (target, newTarget, length) { return new TWEEN.Tween(target) .to(newTarget, length) .onUpdate(() => { this.updateButtonPosition && this.updateButtonPosition(); this.buttons.forEach(e => e.onUpdate && e.onUpdate()); }) .easing(TWEEN.Easing.Back.Out); } createCloseTween (target, newTarget, length) { return new TWEEN.Tween(target) .to(newTarget, length) .onUpdate(() => { this.updateButtonPosition && this.updateButtonPosition(); this.buttons.forEach(e => e.onUpdate && e.onUpdate()); }) .easing(TWEEN.Easing.Quintic.Out); } update (deltaTime) { super.update(deltaTime); } }