-
Notifications
You must be signed in to change notification settings - Fork 1
/
MiniMap_Patch.cs
124 lines (113 loc) · 4.31 KB
/
MiniMap_Patch.cs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
namespace Vehicle_Map_Marker
{
class Minimap_Patch
{
[HarmonyPatch(typeof(Minimap), "UpdatePins")]
[HarmonyPostfix]
private static void UpdatePins(float ___m_largeZoom)
{
if (Main.Markers.Count > 0)
{
RawImage rawImage = Minimap.instance.m_largeRoot.activeSelf ? Minimap.instance.m_mapImageLarge : Minimap.instance.m_mapImageSmall;
float markerSize = Minimap.instance.m_largeRoot.activeSelf ? Minimap.instance.m_pinSizeLarge : Minimap.instance.m_pinSizeSmall;
RectTransform rectTransform = Minimap.instance.m_largeRoot.activeSelf ? Minimap.instance.m_pinRootLarge : Minimap.instance.m_pinRootSmall;
foreach (MarkerData data in Main.Markers.Values)
{
Vector3 vehiclePos = data.ZDO.GetPosition();
if (IsPointVisible(vehiclePos, rawImage) && !IsInControlByPlayer(data.ZDO) && data.Type.Show)
{
DrawVehicleMarker(data, markerSize, rectTransform, rawImage, ___m_largeZoom);
}
else
{
if (data.Marker != null)
{
UnityEngine.Object.Destroy(data.Marker);
}
}
}
}
}
private static bool IsInControlByPlayer(ZDO zdo)
{
ZNetView zNetView = ZNetScene.instance.FindInstance(zdo);
if (zNetView != null)
{
GameObject gameObject = zNetView.gameObject;
if (gameObject != null)
{
Ship ship = gameObject.GetComponent<Ship>();
if (ship != null)
{
return ship.HaveControllingPlayer();
}
Vagon vagon = gameObject.GetComponent<Vagon>();
if (vagon != null) {
return vagon.IsAttached(Player.m_localPlayer);
}
}
}
return false;
}
private static void DrawVehicleMarker(MarkerData data, float size, RectTransform parent, RawImage rawImage, float largeMapZoom)
{
GameObject gameObject = data.Marker;
if (gameObject == null || gameObject.transform.parent != parent)
{
if (gameObject != null)
{
UnityEngine.Object.Destroy(gameObject);
}
gameObject = UnityEngine.Object.Instantiate<GameObject>(Minimap.instance.m_pinPrefab);
data.Marker = gameObject;
gameObject.GetComponent<Image>().sprite = data.Type.Sprite;
gameObject.transform.SetParent(parent);
(gameObject.transform as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size);
(gameObject.transform as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size);
}
float mx;
float my;
WorldToMapPoint(data.ZDO.GetPosition(), out mx, out my);
Vector2 anchoredPosition = MapPointToLocalGuiPos(mx, my, rawImage);
(gameObject.transform as RectTransform).anchoredPosition = anchoredPosition;
gameObject.transform.Find("Checked").gameObject.SetActive(false);
Text text = gameObject.transform.Find("Name").GetComponent<Text>();
if (Minimap.instance.m_largeRoot.activeSelf && largeMapZoom < Minimap.instance.m_showNamesZoom)
{
text.gameObject.SetActive(true);
text.text = Localization.instance.Localize(data.Type.Name);
}
else
{
text.gameObject.SetActive(false);
}
}
private static bool IsPointVisible(Vector3 p, RawImage map)
{
float num;
float num2;
WorldToMapPoint(p, out num, out num2);
return num > map.uvRect.xMin && num < map.uvRect.xMax && num2 > map.uvRect.yMin && num2 < map.uvRect.yMax;
}
private static void WorldToMapPoint(Vector3 p, out float mx, out float my)
{
int num = Minimap.instance.m_textureSize / 2;
mx = p.x / Minimap.instance.m_pixelSize + (float)num;
my = p.z / Minimap.instance.m_pixelSize + (float)num;
mx /= (float)Minimap.instance.m_textureSize;
my /= (float)Minimap.instance.m_textureSize;
}
private static Vector2 MapPointToLocalGuiPos(float mx, float my, RawImage img)
{
Vector2 result = default(Vector2);
result.x = (mx - img.uvRect.xMin) / img.uvRect.width;
result.y = (my - img.uvRect.yMin) / img.uvRect.height;
result.x *= img.rectTransform.rect.width;
result.y *= img.rectTransform.rect.height;
return result;
}
}
}