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