Skip to content

Commit ab21dab

Browse files
committed
- added elevation markers for all icons denoting where they are located vertically from player
1 parent 7a4b653 commit ab21dab

17 files changed

Lines changed: 363 additions & 66 deletions

MiniMapLibrary/Config/Interfaces/ISettingConfigGroup.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace MiniMapLibrary.Config
88
public interface ISettingConfigGroup
99
{
1010
IConfigEntry<bool> Enabled { get; }
11+
IConfigEntry<bool> EnabledElevationMarker { get; }
12+
IConfigEntry<Vector2> ElevationMarkerOffset { get; }
13+
IConfigEntry<float> ElevationMarkerHeight { get; }
14+
IConfigEntry<float> ElevationMarkerWidth { get; }
15+
IConfigEntry<Color> ElevationMarkerColor { get; }
1116
IConfigEntry<float> Height { get; }
1217
IConfigEntry<float> Width { get; }
1318
IConfigEntry<Color> ActiveColor { get; }

MiniMapLibrary/Config/SettingConfigGroup.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,25 @@ public SettingConfigGroup(IConfigEntry<bool> enabled, IConfigEntry<float> height
1919
IconPath = iconPath;
2020
}
2121

22+
public SettingConfigGroup(IConfigEntry<bool> enabled, IConfigEntry<float> height, IConfigEntry<float> width, IConfigEntry<Color> activeColor, IConfigEntry<Color> inactiveColor, IConfigEntry<string> iconPath, IConfigEntry<bool> enabledElevationMarker, IConfigEntry<Vector2> elevationMarkerOffset, IConfigEntry<float> elevationMarkerHeight, IConfigEntry<float> elevationMarkerWidth, IConfigEntry<Color> elevationMarkerColor) : this(enabled, height, width, activeColor, inactiveColor, iconPath)
23+
{
24+
EnabledElevationMarker = enabledElevationMarker;
25+
ElevationMarkerOffset = elevationMarkerOffset;
26+
ElevationMarkerHeight = elevationMarkerHeight;
27+
ElevationMarkerWidth = elevationMarkerWidth;
28+
ElevationMarkerColor = elevationMarkerColor;
29+
}
30+
2231
public IConfigEntry<bool> Enabled { get; }
2332
public IConfigEntry<float> Height { get; }
2433
public IConfigEntry<float> Width { get; }
2534
public IConfigEntry<Color> ActiveColor { get; }
2635
public IConfigEntry<Color> InactiveColor { get; }
2736
public IConfigEntry<string> IconPath { get; }
37+
public IConfigEntry<bool> EnabledElevationMarker { get; }
38+
public IConfigEntry<Vector2> ElevationMarkerOffset { get; }
39+
public IConfigEntry<float> ElevationMarkerHeight { get; }
40+
public IConfigEntry<float> ElevationMarkerWidth { get; }
41+
public IConfigEntry<Color> ElevationMarkerColor { get; }
2842
}
2943
}

MiniMapLibrary/Helpers/Sprites.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime;
4+
using System.Text;
5+
using UnityEngine;
6+
using UnityEngine.UI;
7+
8+
namespace MiniMapLibrary.Helpers
9+
{
10+
public static class Sprites
11+
{
12+
public static GameObject CreateIcon(Sprite sprite, float width, float height, Color color)
13+
{
14+
GameObject icon = new GameObject();
15+
16+
var transform = icon.AddComponent<RectTransform>();
17+
18+
transform.sizeDelta = new Vector2(width, height);
19+
20+
icon.AddComponent<CanvasRenderer>();
21+
22+
var image = icon.AddComponent<Image>();
23+
24+
image.sprite = sprite;
25+
26+
image.color = color;
27+
28+
return icon;
29+
}
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using UnityEngine;
5+
6+
namespace MiniMapLibrary.Helpers
7+
{
8+
public static class Transforms
9+
{
10+
public static void SetParent(Transform child, Transform parent)
11+
{
12+
child.transform.SetParent(parent);
13+
14+
child.transform.localRotation = Quaternion.identity;
15+
16+
child.transform.localPosition = new Vector3(0, 0, 0);
17+
18+
child.transform.localScale = new Vector3(1, 1, 1);
19+
}
20+
}
21+
}

MiniMapLibrary/Interactible/InteractibleSetting.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public class InteractibleSetting
1313
public Color InactiveColor { get; set; }
1414
public string Description { get; set; }
1515
public string IconPath { get; set; }
16+
public Dimension2D ElevationMarkerOffset { get; set; }
17+
public Dimension2D ElevationMarkerDimensions { get; set; }
18+
public bool ElevationMarkerEnabled { get; set; }
19+
public Color ElevationMarkerColor { get; set; }
1620
public ISettingConfigGroup Config;
1721
}
1822
}

MiniMapLibrary/Interfaces/ISpriteManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace MiniMapLibrary
88
/// </summary>
99
public interface ISpriteManager : IDisposable
1010
{
11+
Sprite GetOrCache(string Path);
12+
1113
/// <summary>
1214
/// Uses the provided <see cref="InteractableKind"/> to generate/provide and return a sprite that should represent that kind in the minimap
1315
/// </summary>

MiniMapLibrary/MiniMapLibrary.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
<Reference Include="UnityEngine.UI">
1515
<HintPath>..\MiniMapMod\libs\UnityEngine.UI.dll</HintPath>
1616
</Reference>
17+
<Reference Include="UnityEngine.UIModule">
18+
<HintPath>..\MiniMapMod\libs\UnityEngine.UIModule.dll</HintPath>
19+
</Reference>
1720
</ItemGroup>
1821

1922
</Project>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

MiniMapLibrary/Scanner/Interfaces/ITrackedObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public interface ITrackedObject
1212
InteractableKind InteractableType { get; set; }
1313
RectTransform MinimapTransform { get; set; }
1414
bool DynamicObject { get; set; }
15+
bool Active { get; }
1516

1617
void CheckActive();
1718
void Destroy();

MiniMapLibrary/Scanner/MultiKindScanner.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ public class MultiKindScanner<T> : ITrackedObjectScanner
1111
private readonly IInteractibleSorter<T> sorter;
1212
private readonly bool dynamic;
1313
private readonly Range3D range;
14+
private readonly ISpriteManager spriteManager;
15+
private readonly Func<float> playerHeightRetriever;
1416

15-
public MultiKindScanner(bool dynamic, IScanner<T> scanner, IInteractibleSorter<T> sorter, Range3D range)
17+
public MultiKindScanner(bool dynamic, IScanner<T> scanner, IInteractibleSorter<T> sorter, Range3D range, ISpriteManager spriteManager, Func<float> playerHeightRetriever)
1618
{
1719
this.scanner = scanner;
1820
this.sorter = sorter;
1921
this.dynamic = dynamic;
2022
this.range = range;
23+
this.spriteManager = spriteManager;
24+
this.playerHeightRetriever = playerHeightRetriever;
2125
}
2226

2327
public void ScanScene(IList<ITrackedObject> list)
@@ -28,11 +32,12 @@ public void ScanScene(IList<ITrackedObject> list)
2832
{
2933
if (sorter.TrySort(item, out InteractableKind kind, out GameObject gameObject, out Func<T, bool> activeChecker))
3034
{
31-
list.Add(new TrackedObject<T>(kind, gameObject, null) {
35+
list.Add(new ElevationTrackedObject(new TrackedObject<T>(kind, gameObject, null)
36+
{
3237
BackingObject = item,
3338
ActiveChecker = activeChecker,
3439
DynamicObject = dynamic
35-
});
40+
}, spriteManager, playerHeightRetriever));
3641

3742
range.CheckValue(gameObject.transform.position);
3843
}

0 commit comments

Comments
 (0)