forked from theSpeare/RoverScience-Revisit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DrawWaypoint.cs
217 lines (170 loc) · 7.69 KB
/
DrawWaypoint.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
using System;
using UnityEngine;
namespace RoverScience
{
// All code taken from Waypoint Manager mod
// Still in experimental as I try to figure out what I'm doing here
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class DrawWaypoint : MonoBehaviour
{
System.Random rand = new System.Random();
private GameObject marker = null;
private GameObject interestingObject;
float markerSize = 30;
float markerSizeMax = 30;
float markerAlpha = 0.4f;
float maxAlpha = 0.4f;
float minAlpha = 0.05f;
public static DrawWaypoint Instance = null;
Color markerRed= Color.red;
Color markerGreen = Color.green;
string[] rockObjectNames = {"rock", "rock2"};
private void Start()
{
Utilities.LogVerbose("Attempting to create scienceSpot sphere");
Instance = this;
marker = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Destroy(marker.GetComponent("SphereCollider"));
// Set initial position
//marker.transform.localScale = new Vector3(markerSize, markerSize, markerSize);
marker.transform.localScale = new Vector3(markerSize, markerSize, markerSize);
marker.transform.position = FlightGlobals.currentMainBody.GetWorldSurfacePosition(0, 0, 0);
HideMarker(); // do not render marker yet
// Set marker material, color and alpha
marker.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Transparent/Diffuse"));
markerRed.a = markerAlpha; // max alpha
markerGreen.a = markerAlpha; // max alpha
marker.GetComponent<MeshRenderer>().material.color = markerRed; // set to red on awake
Utilities.LogVerbose("Reached end of marker creation");
}
public void DestroyInterestingObject()
{
if (interestingObject != null) Destroy(interestingObject);
}
public void SetMarkerLocation(double longitude, double latitude, bool spawningObject = true)
{
DestroyInterestingObject();
Vector3 bottomPoint = FlightGlobals.currentMainBody.GetWorldSurfacePosition(latitude, longitude, 0);
Vector3 topPoint = FlightGlobals.currentMainBody.GetWorldSurfacePosition(latitude, longitude, 1000);
double surfaceAltitude = GetSurfaceAltitude(longitude, latitude);
Utilities.LogVerbose("Drawing marker @ (long/lat/alt): " + longitude.ToString() + " " + latitude.ToString() + " " + surfaceAltitude.ToString());
marker.transform.position = FlightGlobals.currentMainBody.GetWorldSurfacePosition(latitude, longitude, surfaceAltitude);
//marker.transform.up = cylinderDirectionUp;
marker.transform.localScale = new Vector3(markerSizeMax, markerSizeMax, markerSizeMax);
markerRed.a = maxAlpha;
//attempt to get raycast surface altitude
if (spawningObject) SpawnObject(longitude, latitude);
}
private Vector3 GetUpDown(double longitude, double latitude, bool up = true)
{
double altitude = 20000;
Vector3d topPoint = FlightGlobals.currentMainBody.GetWorldSurfacePosition(latitude, longitude, altitude);
Vector3d bottomPoint = FlightGlobals.currentMainBody.GetWorldSurfacePosition(latitude, longitude, -altitude);
if (up)
{
return topPoint - bottomPoint;
} else {
return bottomPoint - topPoint;
}
}
public double GetSurfaceAltitude(double longitude, double latitude)
{
double altitude = 20000;
Vector3d topPoint = FlightGlobals.currentMainBody.GetWorldSurfacePosition(latitude, longitude, altitude);
Vector3d bottomPoint = FlightGlobals.currentMainBody.GetWorldSurfacePosition(latitude, longitude, -altitude);
if (Physics.Raycast(topPoint, (bottomPoint - topPoint), out RaycastHit hit, Mathf.Infinity, 1 << 15))
{
return (altitude - hit.distance);
} else
{
Utilities.LogVerbose("No surface intersect detected!");
}
return -1;
}
public void ShowMarker()
{
if (RoverScience.Instance.rover.scienceSpot.established)
{
marker.GetComponent<MeshRenderer>().enabled = true;
}
}
public void HideMarker()
{
marker.GetComponent<MeshRenderer>().enabled = false;
}
public void ToggleMarker()
{
if (!marker.GetComponent<MeshRenderer>().enabled)
{
ShowMarker();
} else
{
HideMarker();
}
}
private void ChangeSpherewithDistance(Rover rover)
{
float distanceToRover = (float)rover.DistanceFromScienceSpot;
// distance to rover 10
// min distance 3
// +2 = 5
// will keep reducing size as long as distance is over 5
if ((distanceToRover < markerSizeMax) && ((distanceToRover > (rover.scienceSpot.minDistance+4))))
{
// Reduce sphere size with proximity
markerSize = distanceToRover;
marker.transform.localScale = new Vector3(markerSize, markerSize, markerSize);
// Reduce alpha with proximity
markerAlpha = (float)(distanceToRover / markerSizeMax);
if (markerAlpha >= maxAlpha)
{
markerAlpha = maxAlpha;
} else if (markerAlpha <= minAlpha)
{
markerAlpha = minAlpha;
}
markerRed.a = markerAlpha;
markerGreen.a = markerAlpha;
}
if ((distanceToRover <= (rover.scienceSpot.minDistance)) && (distanceToRover >= 0))
{
marker.GetComponent<MeshRenderer>().material.color = markerGreen;
} else {
marker.GetComponent<MeshRenderer>().material.color = markerRed;
}
}
public void SpawnObject(double longitude, double latitude)
{
try
{
Utilities.LogVerbose("Attempting to spawn object");
string randomRockName = rockObjectNames[rand.Next(rockObjectNames.Length)];
GameObject test = GameDatabase.Instance.GetModel("RoverScience/rock/" + randomRockName);
Utilities.LogVerbose("Random rock name: " + randomRockName);
test.SetActive(true);
interestingObject = GameObject.Instantiate(test) as GameObject;
GameObject.Destroy(test);
GameObject.Destroy(interestingObject.GetComponent("MeshCollider"));
double srfAlt = DrawWaypoint.Instance.GetSurfaceAltitude(longitude, latitude);
interestingObject.transform.position = FlightGlobals.currentMainBody.GetWorldSurfacePosition(latitude, longitude, srfAlt);
interestingObject.transform.up = GetUpDown(longitude, latitude, true);
} catch (Exception e)
{
Utilities.Log($"Exception: Spawn object failed: {e.Message}");
Utilities.Log(e.StackTrace);
}
}
private void Update()
{
if (marker.GetComponent<MeshRenderer>().enabled)
{
ChangeSpherewithDistance(RoverScience.Instance.rover);
}
}
private void OnDestroy()
{
Destroy(marker);
DestroyInterestingObject();
}
}
}