| 1 | class BaseMenuButton extends GameObject |
| 2 | { |
| 3 | targetPosition = null; |
| 4 | originPosition = null; |
| 5 | |
| 6 | parent = null; |
| 7 | |
| 8 | activeTween = null; |
| 9 | |
| 10 | shouldPeekOnHover = true; |
| 11 | |
| 12 | constructor (sx, sy, ex, ey, sprite, layer) |
| 13 | { |
| 14 | super(sx, sy, sprite, layer); |
| 15 | this.originPosition = new vector(sx, sy); |
| 16 | this.targetPosition = new vector(ex, ey); |
| 17 | } |
| 18 | |
| 19 | peekOnHover () |
| 20 | { |
| 21 | if (!GameInput.mousePosition) |
| 22 | return; |
| 23 | if (this.collider.isInBounds(GameInput.mousePosition)) |
| 24 | { |
| 25 | if (this.activeTween == null && (this.transform.position.x == this.targetPosition.x && this.transform.position.y == this.targetPosition.y)) |
| 26 | { |
| 27 | this.createHoverTween(400, null, this).easing(TWEEN.Easing.Elastic.Out).start(); |
| 28 | } |
| 29 | } |
| 30 | else |
| 31 | { |
| 32 | if (this.activeTween == null && |
| 33 | (this.transform.position.x != this.targetPosition.x || this.transform.position.y != this.targetPosition.y) && |
| 34 | this.parent && this.parent.state == MenuStates.open) |
| 35 | { |
| 36 | this.createActiveStateTween(400, null, this).start(); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | update (deltaTime) |
| 42 | { |
| 43 | super.update(deltaTime); |
| 44 | if (this.shouldPeekOnHover) |
| 45 | { |
| 46 | this.peekOnHover(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | moveOnParentOpen () |
| 51 | { |
| 52 | this.parent.isOpened.add((a, b) => this.moveToActiveState(a, b), this); |
| 53 | } |
| 54 | |
| 55 | moveOnParentClose () |
| 56 | { |
| 57 | this.parent.isClosed.add((a, b) => this.moveToInactiveState(a, b), this); |
| 58 | } |
| 59 | |
| 60 | moveToActiveState (time, onDone) |
| 61 | { |
| 62 | return this.createActiveStateTween(time, onDone, this).start(); |
| 63 | } |
| 64 | |
| 65 | moveToInactiveState (time, onDone) |
| 66 | { |
| 67 | return this.createInactiveStateTween(time, onDone, this).start(); |
| 68 | } |
| 69 | |
| 70 | createActiveStateTween (time, onDone, context) |
| 71 | { |
| 72 | let tween = new TWEEN.Tween(context.transform.position) |
| 73 | .to({ x: context.targetPosition.x, y: context.targetPosition.y }, time) |
| 74 | .easing(TWEEN.Easing.Bounce.Out) |
| 75 | .onComplete(() => { context.activeTween = null; onDone && onDone.call(context); }); |
| 76 | return tween.onStart(() => context.stopActiveTween(tween)); |
| 77 | } |
| 78 | |
| 79 | createInactiveStateTween (time, onDone, context) |
| 80 | { |
| 81 | let tween = new TWEEN.Tween(context.transform.position) |
| 82 | .to({ x: context.originPosition.x, y: context.originPosition.y }, time) |
| 83 | .easing(TWEEN.Easing.Elastic.Out) |
| 84 | .onComplete(() => { context.activeTween = null; onDone && onDone.call(context); }); |
| 85 | return tween.onStart(() => context.stopActiveTween(tween)); |
| 86 | } |
| 87 | |
| 88 | createHoverTween (time, onDone, context) |
| 89 | { |
| 90 | let tween = new TWEEN.Tween(context.transform.position) |
| 91 | .to({ x: context.targetPosition.x + 10, y: context.targetPosition.y }, time) |
| 92 | .easing(TWEEN.Easing.Elastic.In) |
| 93 | .onComplete(() => { context.activeTween = null; onDone && onDone.call(context); }); |
| 94 | return tween.onStart(() => context.stopActiveTween(tween)); |
| 95 | } |
| 96 | |
| 97 | stopActiveTween (tween) |
| 98 | { |
| 99 | if (this.activeTween) |
| 100 | { |
| 101 | this.activeTween.stop(); |
| 102 | } |
| 103 | this.activeTween = tween; |
| 104 | } |
| 105 | } |