forked from space-commits/SPT-Realism-Mod-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgizmos.cs
341 lines (292 loc) · 12.8 KB
/
gizmos.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using BepInEx.Logging;
using EFT;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
namespace RealismMod
{
public class DebugGizmos
{
public static bool DrawGizmos = Plugin.EnableLogging.Value;
public class SingleObjects
{
public static GameObject Sphere(Vector3 position, float size, Color color, bool temporary = false, float expiretime = 1f)
{
if (!DrawGizmos)
{
return null;
}
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.GetComponent<Renderer>().material.color = color;
sphere.GetComponent<Collider>().enabled = false;
sphere.transform.position = position;
sphere.transform.localScale = new Vector3(size, size, size);
if (temporary)
{
TempCoroutine.DestroyAfterDelay(sphere, expiretime);
}
return sphere;
}
public static GameObject Line(Vector3 startPoint, Vector3 endPoint, Color color, float lineWidth = 0.1f, bool temporary = false, float expiretime = 1f, bool taperLine = false)
{
if (!DrawGizmos)
{
return null;
}
var lineObject = new GameObject();
var lineRenderer = lineObject.AddComponent<LineRenderer>();
// Set the color and width of the line
lineRenderer.material.color = color;
lineRenderer.startWidth = lineWidth;
lineRenderer.endWidth = taperLine ? lineWidth / 4f : lineWidth;
// Set the start and end points of the line
lineRenderer.SetPosition(0, startPoint);
lineRenderer.SetPosition(1, endPoint);
if (temporary)
{
TempCoroutine.DestroyAfterDelay(lineObject, expiretime);
}
return lineObject;
}
public static GameObject Ray(Vector3 startPoint, Vector3 direction, Color color, float length = 0.35f, float lineWidth = 0.1f, bool temporary = false, float expiretime = 1f, bool taperLine = false)
{
if (!DrawGizmos)
{
return null;
}
var rayObject = new GameObject();
var lineRenderer = rayObject.AddComponent<LineRenderer>();
// Set the color and width of the line
lineRenderer.material.color = color;
lineRenderer.startWidth = lineWidth;
lineRenderer.endWidth = taperLine ? lineWidth / 4f : lineWidth;
// Set the start and end points of the line to draw a rays
lineRenderer.SetPosition(0, startPoint);
lineRenderer.SetPosition(1, startPoint + direction.normalized * length);
if (temporary)
{
TempCoroutine.DestroyAfterDelay(rayObject, expiretime);
}
return rayObject;
}
}
public class DrawLists
{
private static ManualLogSource Logger;
private Color ColorA;
private Color ColorB;
public DrawLists(Color colorA, Color colorB, string LogName = "", bool randomColor = false)
{
LogName += "[Drawer]";
if (randomColor)
{
ColorA = new Color(Random.value, Random.value, Random.value);
ColorB = new Color(Random.value, Random.value, Random.value);
}
else
{
ColorA = colorA;
ColorB = colorB;
}
Logger = BepInEx.Logging.Logger.CreateLogSource(LogName);
}
/* public void DrawTempPath(NavMeshPath Path, bool active, Color colorActive, Color colorInActive, float lineSize = 0.05f, float expireTime = 0.5f, bool useDrawerSetColors = false)
{
if (!DrawGizmos)
{
return;
}
for (int i = 0; i < Path.corners.Length - 1; i++)
{
Vector3 corner1 = Path.corners[i];
Vector3 corner2 = Path.corners[i + 1];
Color color;
if (useDrawerSetColors)
{
color = active ? ColorA : ColorB;
}
else
{
color = active ? colorActive : colorInActive;
}
SingleObjects.Line(corner1, corner2, color, lineSize, true, expireTime);
}
}*/
public void Draw(List<Vector3> list, bool destroy, float size = 0.1f, bool rays = false, float rayLength = 0.35f)
{
if (!DrawGizmos)
{
DestroyDebug();
return;
}
if (destroy)
{
DestroyDebug();
}
else if (list.Count > 0 && DebugObjects == null)
{
Logger.LogWarning($"Drawing {list.Count} Vector3s");
DebugObjects = Create(list, size, rays, rayLength);
}
}
public void Draw(Vector3[] array, bool destroy, float size = 0.1f, bool rays = false, float rayLength = 0.35f)
{
if (!DrawGizmos)
{
DestroyDebug();
return;
}
if (destroy)
{
DestroyDebug();
}
else if (array.Length > 0 && DebugObjects == null)
{
Logger.LogWarning($"Drawing {array.Length} Vector3s");
DebugObjects = Create(array, size, rays, rayLength);
}
}
private GameObject[] Create(List<Vector3> list, float size = 0.1f, bool rays = false, float rayLength = 0.35f)
{
List<GameObject> debugObjects = new List<GameObject>();
foreach (var point in list)
{
if (rays)
{
size *= Random.Range(0.5f, 1.5f);
rayLength *= Random.Range(0.5f, 1.5f);
var ray = SingleObjects.Ray(point, Vector3.up, ColorA, rayLength, size);
debugObjects.Add(ray);
}
else
{
var sphere = SingleObjects.Sphere(point, size, ColorA);
debugObjects.Add(sphere);
}
}
return debugObjects.ToArray();
}
private GameObject[] Create(Vector3[] array, float size = 0.1f, bool rays = false, float rayLength = 0.35f)
{
List<GameObject> debugObjects = new List<GameObject>();
foreach (var point in array)
{
if (rays)
{
size *= Random.Range(0.5f, 1.5f);
rayLength *= Random.Range(0.5f, 1.5f);
var ray = SingleObjects.Ray(point, Vector3.up, ColorA, rayLength, size);
debugObjects.Add(ray);
}
else
{
var sphere = SingleObjects.Sphere(point, size, ColorA);
debugObjects.Add(sphere);
}
}
return debugObjects.ToArray();
}
private void DestroyDebug()
{
if (DebugObjects != null)
{
foreach (var point in DebugObjects)
{
Object.Destroy(point);
}
DebugObjects = null;
}
}
private GameObject[] DebugObjects;
}
public class Components
{
/// <summary>
/// Creates a line between two game objects and adds a script to update the line's DrawPosition and color every frame.
/// </summary>
/// <param name="startObject">The starting game object.</param>
/// <param name="endObject">The ending game object.</param>
/// <param name="lineWidth">The width of the line.</param>
/// <param name="color">The color of the line.</param>
/// <returns>The game object containing the line renderer.</returns>
public static GameObject FollowLine(GameObject startObject, GameObject endObject, float lineWidth, Color color)
{
var lineObject = new GameObject();
var lineRenderer = lineObject.AddComponent<LineRenderer>();
// Set the color and width of the line
lineRenderer.material.color = color;
lineRenderer.startWidth = lineWidth;
lineRenderer.endWidth = lineWidth;
// Set the initial start and end points of the line
lineRenderer.SetPosition(0, startObject.transform.position);
lineRenderer.SetPosition(1, endObject.transform.position);
// AddorUpdateColorScheme a script to update the line's DrawPosition and color every frame
var followLineScript = lineObject.AddComponent<FollowLineScript>();
followLineScript.startObject = startObject;
followLineScript.endObject = endObject;
followLineScript.lineRenderer = lineRenderer;
return lineObject;
}
public class FollowLineScript : MonoBehaviour
{
public GameObject startObject;
public GameObject endObject;
public LineRenderer lineRenderer;
public float yOffset = 1f;
private void Update()
{
lineRenderer.SetPosition(0, startObject.transform.position + new Vector3(0, yOffset, 0));
lineRenderer.SetPosition(1, endObject.transform.position + new Vector3(0, yOffset, 0));
}
/// <summary>
/// Sets the color of the line renderer material.
/// </summary>
/// <param name="color">The color to set.</param>
public void SetColor(Color color)
{
lineRenderer.material.color = color;
}
}
}
internal class TempCoroutine : MonoBehaviour
{
/// <summary>
/// Class to run coroutines on a MonoBehaviour.
/// </summary>
internal class TempCoroutineRunner : MonoBehaviour { }
/// <summary>
/// Destroys the specified GameObject after a given delay.
/// </summary>
/// <param name="obj">The GameObject to be destroyed.</param>
/// <param name="delay">The delay before the GameObject is destroyed.</param>
public static void DestroyAfterDelay(GameObject obj, float delay)
{
if (obj != null)
{
var runner = new GameObject("TempCoroutineRunner").AddComponent<TempCoroutineRunner>();
runner.StartCoroutine(RunDestroyAfterDelay(obj, delay));
}
}
/// <summary>
/// Runs a coroutine to destroy a GameObject after a delay.
/// </summary>
/// <param name="obj">The GameObject to destroy.</param>
/// <param name="delay">The delay before destroying the GameObject.</param>
/// <returns>The coroutine.</returns>
private static IEnumerator RunDestroyAfterDelay(GameObject obj, float delay)
{
yield return new WaitForSeconds(delay);
if (obj != null)
{
Destroy(obj);
}
TempCoroutineRunner runner = obj?.GetComponentInParent<TempCoroutineRunner>();
if (runner != null)
{
Destroy(runner.gameObject);
}
}
}
}
}