forked from FelixReuthlinger/AutoMapPins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPinComponent.cs
More file actions
72 lines (60 loc) · 1.79 KB
/
PinComponent.cs
File metadata and controls
72 lines (60 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Collections;
using System.Reflection;
using AutoMapPins.Patches;
using HarmonyLib;
using UnityEngine;
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
namespace AutoMapPins.Model;
internal class PinComponent : MonoBehaviour
{
private static readonly MethodInfo? IsExploredMethod =
AccessTools.DeclaredMethod(typeof(Minimap), "IsExplored", new[] { typeof(Vector3) });
private PinComponent()
{
}
private Vector3 Position;
private bool IsVisible;
private Coroutine Routine = null!;
internal void Awake()
{
Position = gameObject.transform.position;
IsVisible = Minimap.instance && CallIsExplored(Minimap.instance, transform.position);
SetVisiblePin();
}
internal void OnDestroy()
{
if (Routine != null) StopCoroutine(Routine);
MinimapPatch.UnpinObject(gameObject);
}
private IEnumerator VisibleCheck()
{
while (!IsVisible)
{
IsVisible = Minimap.instance && CallIsExplored(Minimap.instance, Position);
if (IsVisible)
{
SetVisiblePin();
yield break;
}
yield return new WaitForFixedUpdate();
}
}
private void SetVisiblePin()
{
if (IsVisible)
{
MinimapPatch.UpsertPin(gameObject);
}
else
{
if (Routine != null) StopCoroutine(Routine);
Routine = StartCoroutine(VisibleCheck());
}
}
private static bool CallIsExplored(Minimap minimap, Vector3 position)
{
if (IsExploredMethod == null) return false;
object? result = IsExploredMethod.Invoke(minimap, new object[] { position });
return result is bool explored && explored;
}
}