|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace MiniMapLibrary.Scanner |
| 7 | +{ |
| 8 | + public class ElevationTrackedObject : ITrackedObject |
| 9 | + { |
| 10 | + private const float margin = 5.0f; |
| 11 | + |
| 12 | + private readonly ITrackedObject backing; |
| 13 | + private readonly ISpriteManager spriteManager; |
| 14 | + private readonly Func<float> heightRetriever; |
| 15 | + private static readonly Quaternion upDirection; |
| 16 | + private static readonly Quaternion downDirection; |
| 17 | + |
| 18 | + private Transform arrowTransform; |
| 19 | + private GameObject arrow; |
| 20 | + |
| 21 | + private bool flag_initialized; |
| 22 | + private bool flag_disabled; |
| 23 | + |
| 24 | + static ElevationTrackedObject() { |
| 25 | + upDirection = Quaternion.AngleAxis(-90.0f, Vector3.forward); |
| 26 | + downDirection = Quaternion.AngleAxis(90.0f, Vector3.forward); |
| 27 | + } |
| 28 | + |
| 29 | + public ElevationTrackedObject(ITrackedObject backing, ISpriteManager spriteManager, Func<float> heightRetriever) |
| 30 | + { |
| 31 | + this.backing = backing; |
| 32 | + this.spriteManager = spriteManager; |
| 33 | + this.heightRetriever = heightRetriever; |
| 34 | + } |
| 35 | + |
| 36 | + public GameObject gameObject { get => backing.gameObject; set => backing.gameObject = value; } |
| 37 | + |
| 38 | + public InteractableKind InteractableType { get => backing.InteractableType; set => backing.InteractableType = value; } |
| 39 | + |
| 40 | + public RectTransform MinimapTransform { get => backing.MinimapTransform; set => backing.MinimapTransform = value; } |
| 41 | + |
| 42 | + public bool DynamicObject { get => backing.DynamicObject; set => backing.DynamicObject = value; } |
| 43 | + |
| 44 | + public bool Active => backing.Active; |
| 45 | + |
| 46 | + private bool Initialized() |
| 47 | + { |
| 48 | + // minimap transform, is lazily loaded by minimap at runtime and isn't available |
| 49 | + // until first update call after the scan that produced this object was created |
| 50 | + if (backing.MinimapTransform is null) |
| 51 | + { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + // only run this method once |
| 56 | + if (flag_initialized) |
| 57 | + { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + // rotate down |
| 62 | + InteractibleSetting setting = Settings.GetSetting(backing.InteractableType); |
| 63 | + |
| 64 | + if (setting.Config.EnabledElevationMarker.Value is false) |
| 65 | + { |
| 66 | + flag_disabled = true; |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + Sprite arrowSprite = spriteManager.GetOrCache(Settings.Icons.Arrow); |
| 71 | + |
| 72 | + arrow = Helpers.Sprites.CreateIcon(arrowSprite, |
| 73 | + setting.Config.ElevationMarkerWidth.Value, |
| 74 | + setting.Config.ElevationMarkerHeight.Value, |
| 75 | + setting.Config.ElevationMarkerColor.Value); |
| 76 | + |
| 77 | + // attach arrow |
| 78 | + Helpers.Transforms.SetParent(arrow.transform, backing.MinimapTransform); |
| 79 | + |
| 80 | + arrowTransform = arrow.transform; |
| 81 | + |
| 82 | + // move arrow |
| 83 | + arrowTransform.localPosition += new Vector3(setting.Config.ElevationMarkerOffset.Value.x, setting.Config.ElevationMarkerOffset.Value.y, 0); |
| 84 | + |
| 85 | + // rotate arrow upright |
| 86 | + arrowTransform.localRotation = upDirection; |
| 87 | + |
| 88 | + // hide arrow |
| 89 | + arrow.SetActive(false); |
| 90 | + |
| 91 | + flag_initialized = true; |
| 92 | + |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + public void CheckActive() |
| 97 | + { |
| 98 | + // check color from backing object |
| 99 | + backing.CheckActive(); |
| 100 | + |
| 101 | + // if the config does not allow arrows don't bother trying to update it, it doesn't exist |
| 102 | + if (flag_disabled) |
| 103 | + { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (Initialized()) |
| 108 | + { |
| 109 | + // check if we need to show an arrow denoting height |
| 110 | + if (backing.Active) |
| 111 | + { |
| 112 | + // since the object is active |
| 113 | + // show arrow if player is too smol |
| 114 | + float y = heightRetriever(); |
| 115 | + |
| 116 | + if (y > (backing.gameObject.transform.position.y + margin)) |
| 117 | + { |
| 118 | + // item is below player show down arrow |
| 119 | + arrow.SetActive(true); |
| 120 | + |
| 121 | + arrowTransform.localRotation = downDirection; |
| 122 | + } |
| 123 | + else if (y < (backing.gameObject.transform.position.y - margin)) |
| 124 | + { |
| 125 | + // item is above player show up arrow |
| 126 | + arrow.SetActive(true); |
| 127 | + |
| 128 | + arrowTransform.localRotation = upDirection; |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + arrow.SetActive(false); |
| 133 | + } |
| 134 | + } |
| 135 | + // hide the arrow when the object is no longer active |
| 136 | + else if (arrow.activeSelf) |
| 137 | + { |
| 138 | + arrow.SetActive(false); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public void Destroy() => backing.Destroy(); |
| 144 | + } |
| 145 | +} |
0 commit comments