| 1 | class MainMenu extends MoveableMenuItem |
| 2 | { |
| 3 | static Instance; |
| 4 | |
| 5 | buttons = []; |
| 6 | buttonPadding = 10; |
| 7 | buttonXOffset = -32; |
| 8 | |
| 9 | openTargetX = 60; |
| 10 | |
| 11 | closedTargetX = -15; |
| 12 | |
| 13 | constructor (sx, sy, ex, ey, sprite, layer, isMenu, tOpen = 800, tClose = 1000) |
| 14 | { |
| 15 | super(sx, sy, ex, ey, sprite, layer, isMenu, tOpen, tClose); |
| 16 | MainMenu.Instance = this; |
| 17 | |
| 18 | } |
| 19 | |
| 20 | addedToScene () |
| 21 | { |
| 22 | |
| 23 | this.initialiseChildren(); |
| 24 | |
| 25 | this.menu.moveSelfOnOpen(); |
| 26 | this.menu.moveSelfOnClose(); |
| 27 | |
| 28 | //this.menu.open(() => this.menu.close()); |
| 29 | } |
| 30 | |
| 31 | onClick (btn) |
| 32 | { |
| 33 | this.menu.onClick(btn); |
| 34 | } |
| 35 | |
| 36 | initialiseChildren () |
| 37 | { |
| 38 | let vectorOrigin = this.getNextOriginPosition(); |
| 39 | this.menu.addChildItem("btnFruit", this.scene.addObject(new BtnFruit(vectorOrigin.x, vectorOrigin.y, this.targetPosition.x + this.buttonXOffset, vectorOrigin.y, this.scene.game.images.btnFruit, 4))); |
| 40 | |
| 41 | vectorOrigin = this.getNextOriginPosition(); |
| 42 | this.menu.addChildItem("btnToys", this.scene.addObject(new BtnToys(vectorOrigin.x, vectorOrigin.y, this.targetPosition.x + this.buttonXOffset, vectorOrigin.y, this.scene.game.images.btnToys, 4))); |
| 43 | |
| 44 | vectorOrigin = this.getNextOriginPosition(); |
| 45 | this.menu.addChildItem("btnGames", this.scene.addObject(new BtnGames(vectorOrigin.x, vectorOrigin.y, this.targetPosition.x + this.buttonXOffset, vectorOrigin.y, this.scene.game.images.btnGames, 4, true))); |
| 46 | |
| 47 | vectorOrigin = this.getNextOriginPosition(); |
| 48 | this.menu.addChildItem("btnPlants", this.scene.addObject(new BtnPlants(vectorOrigin.x, vectorOrigin.y, this.targetPosition.x + this.buttonXOffset, vectorOrigin.y, this.scene.game.images.btnPlants, 4)) |
| 49 | ); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | getNextOriginPosition () |
| 54 | { |
| 55 | let buttonArray = Object.values(this.menu.children); |
| 56 | if (buttonArray.length == 0) |
| 57 | { |
| 58 | return new vector(this.transform.position.x + this.buttonXOffset, this.transform.position.y + this.transform.size.y + this.buttonPadding); |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | return new vector(this.transform.position.x + this.buttonXOffset, buttonArray[buttonArray.length - 1].transform.position.y + buttonArray[buttonArray.length - 1].transform.size.y + this.buttonPadding); |
| 63 | |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | //override for peekOnHover during closed state |
| 68 | peekOnHover () |
| 69 | { |
| 70 | if (!GameInput.mousePosition) |
| 71 | return; |
| 72 | if (this.collider.isInBounds(GameInput.mousePosition)) |
| 73 | { |
| 74 | if (this.activeTween == null && (this.transform.position.x == this.originPosition.x && this.transform.position.y == this.originPosition.y)) |
| 75 | { |
| 76 | this.createHoverTween(400, null, this).easing(TWEEN.Easing.Elastic.Out).start(); |
| 77 | } |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | if (this.activeTween == null && |
| 82 | (this.transform.position.x != this.originPosition.x || this.transform.position.y != this.originPosition.y) && |
| 83 | this.menu.state == MenuStates.closed) |
| 84 | { |
| 85 | this.createInactiveStateTween(400, null, this).start(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | //override for small opening from closed state |
| 91 | createHoverTween (time, onDone, context) |
| 92 | { |
| 93 | return super.createHoverTween(time, onDone, context) |
| 94 | .to({ x: this.originPosition.x + 10 }, time); |
| 95 | } |
| 96 | } |