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