Skip to content

Commit 8b7f499

Browse files
committed
more refactoring
1 parent 20d7742 commit 8b7f499

File tree

7 files changed

+102
-9
lines changed

7 files changed

+102
-9
lines changed

MiniMapLibrary/Scanner/DefaultSorter.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using UnityEngine;
45

56
#nullable enable
67
namespace MiniMapLibrary.Scanner
@@ -10,13 +11,24 @@ public class DefaultSorter<T> : ISorter<T>
1011
public InteractableKind Kind { get; set; }
1112

1213
private readonly Func<T, bool>? selector;
14+
private readonly Func<T, GameObject> converter;
15+
private readonly Func<T, bool>? activeChecker;
1316

14-
public DefaultSorter(InteractableKind kind, Func<T, bool>? selector = null)
17+
public DefaultSorter(InteractableKind kind,
18+
Func<T, GameObject> converter,
19+
Func<T, bool>? selector = null,
20+
Func<T, bool>? activeChecker = null)
1521
{
1622
Kind = kind;
1723
this.selector = selector;
24+
this.converter = converter;
25+
this.activeChecker = activeChecker;
1826
}
1927

2028
public bool IsKind(T value) => selector?.Invoke(value) ?? false;
29+
30+
public GameObject GetGameObject(T value) => converter(value);
31+
32+
public bool CheckActive(T value) => activeChecker?.Invoke(value) ?? true;
2133
}
2234
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using UnityEngine;
45

56
namespace MiniMapLibrary.Scanner
67
{
78
public interface IInteractibleSorter<T>
89
{
910
/// <summary>
1011
/// Attempts to find the applicable <see cref="InteractableKind"/> that the provided instance
11-
/// of <see cref="T"/> should be considered
12+
/// of <see cref="T"/> should be considered and returns that objects gameobject that should
13+
/// be tracked to update it's position on the minimap
1214
/// </summary>
1315
/// <param name="objectToBeSorted"></param>
1416
/// <returns></returns>
15-
bool TrySort(T objectToBeSorted, out InteractableKind kind);
17+
bool TrySort(T objectToBeSorted, out InteractableKind kind, out GameObject gameObject, out Func<T, bool> activeChecker);
1618
}
1719
}

MiniMapLibrary/Scanner/Interfaces/ISorter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using UnityEngine;
45

56
namespace MiniMapLibrary.Scanner
67
{
@@ -14,5 +15,19 @@ public interface ISorter<T>
1415
/// <param name="value"></param>
1516
/// <returns>true when the provided <paramref name="value"/> should be <see cref="Kind"/></returns>
1617
bool IsKind(T value);
18+
19+
/// <summary>
20+
/// Retrieves the gameobject from the provided T value
21+
/// </summary>
22+
/// <param name="value"></param>
23+
/// <returns></returns>
24+
GameObject GetGameObject(T value);
25+
26+
/// <summary>
27+
/// Checks if the provided value is active and should be displayed as active on the minimap
28+
/// </summary>
29+
/// <param name="value"></param>
30+
/// <returns></returns>
31+
bool CheckActive(T value);
1732
}
1833
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MiniMapLibrary.Scanner
6+
{
7+
public interface ITrackedObjectScanner
8+
{
9+
/// <summary>
10+
/// Scans the scene for any objects that should be placed on the minimap
11+
/// add the found objects to the privided list
12+
/// </summary>
13+
void ScanScene(IList<ITrackedObject> list);
14+
}
15+
}

MiniMapLibrary/Scanner/MonoBehaviourSorter.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using UnityEngine;
45

6+
#nullable enable
57
namespace MiniMapLibrary.Scanner
68
{
79
public class MonoBehaviourSorter<T> : IInteractibleSorter<T>
@@ -13,16 +15,24 @@ public MonoBehaviourSorter(ISorter<T>[] sorters)
1315
this.sorters = sorters;
1416
}
1517

16-
public bool TrySort(T value, out InteractableKind kind)
18+
public bool TrySort(T value, out InteractableKind kind, out GameObject? gameObject, out Func<T, bool> activeChecker)
1719
{
1820
kind = InteractableKind.none;
21+
gameObject = null;
22+
activeChecker = (x) => true;
1923

20-
// for eah nicer looking but slghtly slower
24+
// iterate though our sorters
2125
foreach (ISorter<T> sorter in sorters)
2226
{
27+
// check if the provided value meets the criteria to be of this sorters
28+
// kind
2329
if (sorter.IsKind(value))
2430
{
31+
// set the out kind and grab the gameobject
2532
kind = sorter.Kind;
33+
gameObject = sorter.GetGameObject(value);
34+
activeChecker = sorter.CheckActive;
35+
2636
return true;
2737
}
2838
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using UnityEngine;
5+
6+
namespace MiniMapLibrary.Scanner
7+
{
8+
public class TrackedObjectScanner<T> : ITrackedObjectScanner
9+
{
10+
private readonly IScanner<T> scanner;
11+
private readonly IInteractibleSorter<T> sorter;
12+
private readonly bool dynamic;
13+
14+
public TrackedObjectScanner(bool dynamic, IScanner<T> scanner, IInteractibleSorter<T> sorter)
15+
{
16+
this.scanner = scanner;
17+
this.sorter = sorter;
18+
this.dynamic = dynamic;
19+
}
20+
21+
public void ScanScene(IList<ITrackedObject> list)
22+
{
23+
IEnumerable<T> foundObjects = scanner.Scan();
24+
25+
foreach (var item in foundObjects)
26+
{
27+
if (sorter.TrySort(item, out InteractableKind kind, out GameObject gameObject, out Func<T, bool> activeChecker))
28+
{
29+
list.Add(new TrackedObject<T>(kind, gameObject, null) {
30+
BackingObject = item,
31+
ActiveChecker = activeChecker,
32+
DynamicObject = dynamic
33+
});
34+
}
35+
}
36+
}
37+
}
38+
}

MiniMapMod/MiniMapPlugin.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ public void Awake()
8080
// responsible for finding the InteractibleKind for each chest
8181
IInteractibleSorter<ChestBehavior> chestSorter = new MonoBehaviourSorter<ChestBehavior>(
8282
new ISorter<ChestBehavior>[] {
83-
new DefaultSorter<ChestBehavior>(InteractableKind.Chest, (x) => true),
84-
new DefaultSorter<ChestBehavior>(InteractableKind.LunarPod, (x) => true),
83+
new DefaultSorter<ChestBehavior>(InteractableKind.Chest, (x) => x.gameObject, (x) => true),
84+
new DefaultSorter<ChestBehavior>(InteractableKind.LunarPod, (x) => x.gameObject, (x) => true),
8585
}
8686
);
8787

88-
// TODO: Create interface/class resposible for converting these chests and their kind
89-
// into tracked objects
88+
ITrackedObjectScanner sceneScanner = new TrackedObjectScanner<ChestBehavior>(false, chestScanner, chestSorter);
89+
90+
ssceneScanner.ScanScene();
9091
}
9192

9293
//The Update() method is run on every frame of the game.

0 commit comments

Comments
 (0)