class BtnGames extends GameObject { buttons = []; buttonPadding = 10; state = 0; prevState = 0; 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 () { let frogButton = new GameObject(-32, this.transform.position.y, this.scene.game.images.btnFrog); this.scene.addObject(frogButton); frogButton.startPos = new vector(-32, this.transform.position.y); frogButton.onClick = function () { console.log("clicked frog btn"); }; // todo instantiate guy frogButton.onUpdate = function () { console.log(this.transform.position); }; let frogButton2 = new GameObject(-32, this.transform.position.y, this.scene.game.images.btnFrog); this.scene.addObject(frogButton2); frogButton2.startPos = new vector(-32, this.transform.position.y); frogButton2.onClick = function () { console.log("clicked frog btn"); }; // todo instantiate guy frogButton2.onUpdate = function () { console.log(this.transform.position); }; let frogButton3 = new GameObject(-32, this.transform.position.y, this.scene.game.images.btnFrog); this.scene.addObject(frogButton3); frogButton3.startPos = new vector(-32, this.transform.position.y); frogButton3.onClick = function () { console.log("clicked frog btn"); }; // todo instantiate guy frogButton3.onUpdate = function () { console.log(this.transform.position); }; this.buttons = [ this.scene.addObject(frogButton), this.scene.addObject(frogButton2), this.scene.addObject(frogButton3) ]; 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); } } } 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.endPos.y] }, 1000).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.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.Bounce.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); } }