-
Notifications
You must be signed in to change notification settings - Fork 1
/
StaticObject.cs
198 lines (163 loc) · 6.81 KB
/
StaticObject.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
/* LICENSE
* This source code is copyrighted.
* All rights reserved.
* Copyright © Ryan Irecki 2013
*/
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Kerbtown
{
public class StaticObject
{
public readonly string ConfigURL;
public readonly string ModelUrl;
public readonly string NameID;
public readonly string ObjectID;
public string CelestialBodyName = "";
public bool IsSpaceActive;
public double Latitude;
public string LaunchSiteName = "";
public string LaunchPadTransform = "";
public double Longitude;
public List<KtComponent> ModuleList;
//public StaticObjectModule ModuleReference;
public Vector3 Orientation;
public PQSCity PQSCityComponent;
public float RadOffset;
public Vector3 RadPosition;
public float RotAngle;
//public Vector3 Scale;
public GameObject StaticGameObject;
public float VisRange;
private List<Collider> _colliderComponents;
private List<Renderer> _rendererComponents;
//public StaticObject(Vector3 radialPosition, float rotationAngle, float radiusOffset,
// Vector3 objectOrientation, float visibilityRange, string modelUrl, string configUrl,
// string celestialBodyName, Vector3 scale, string objectID = "", string launchSiteName = "")
public StaticObject(Vector3 radialPosition, float rotationAngle, float radiusOffset,
Vector3 objectOrientation, float visibilityRange, string modelUrl, string configUrl,
string celestialBodyName, string objectID = "", string launchSiteName = "", string launchPadTransform = "")
{
RadPosition = radialPosition;
RotAngle = rotationAngle;
RadOffset = radiusOffset;
Orientation = objectOrientation;
VisRange = visibilityRange;
CelestialBodyName = celestialBodyName;
//Scale = scale;
ModelUrl = modelUrl;
ConfigURL = configUrl;
LaunchSiteName = launchSiteName;
LaunchPadTransform = launchPadTransform;
ObjectID = objectID;
if (string.IsNullOrEmpty(ObjectID))
ObjectID = (visibilityRange + rotationAngle + radiusOffset + objectOrientation.magnitude +
radialPosition.x + radialPosition.y + radialPosition.z +
Random.Range(0f, 1000000f)).ToString("N2");
NameID = string.Format("{0} ({1})", modelUrl.Substring(modelUrl.LastIndexOf('/') + 1), ObjectID);
}
public void Manipulate(bool objectInactive)
{
Manipulate(objectInactive, XKCDColors.BlueyGrey);
}
public void Manipulate(bool objectInactive, Color highlightColor)
{
if (StaticGameObject == null)
{
Extensions.LogWarning(NameID + " has no GameObject attached.");
return;
}
#region Colliders
if (_colliderComponents == null || _colliderComponents.Count == 0)
{
Collider[] colliderList = StaticGameObject.GetComponentsInChildren<Collider>();
if (colliderList.Length > 0)
{
_colliderComponents = new List<Collider>(colliderList);
}
else Extensions.LogWarning(NameID + " has no collider components.");
}
if (_colliderComponents != null && _colliderComponents.Count > 0)
{
foreach (Collider collider in _colliderComponents)
{
collider.enabled = !objectInactive;
}
}
#endregion
#region Highlight
if ((_rendererComponents == null || _rendererComponents.Count == 0))
{
Renderer[] rendererList = StaticGameObject.GetComponentsInChildren<Renderer>();
if (rendererList.Length == 0)
{
Extensions.PostScreenMessage("[KerbTown] Active Vessel not within visibility range.");
Extensions.LogWarning(NameID + " has no renderer components.");
return;
}
_rendererComponents = new List<Renderer>(rendererList);
}
if (!objectInactive) // Deactivate.
{
highlightColor = new Color(0, 0, 0, 0);
KtCamera.RestoreCameraParent();
}
else // Activate
{
if (
Vector3.Distance(PQSCityComponent.sphere.transform.position, PQSCityComponent.transform.position) >=
PQSCityComponent.lod[0].visibleRange)
KtCamera.SetCameraParent(StaticGameObject.transform);
else
Extensions.PostScreenMessage(
"[KerbTown] Ignoring camera switch. Static object is not within the visible range of your active vessel.");
}
foreach (Renderer renderer in _rendererComponents)
{
renderer.material.SetFloat("_RimFalloff", 1.8f);
renderer.material.SetColor("_RimColor", highlightColor);
}
#endregion
}
public void Reorientate()
{
if (PQSCityComponent == null) return;
PQSCityComponent.repositionRadial = RadPosition;
PQSCityComponent.repositionRadiusOffset = RadOffset;
PQSCityComponent.reorientFinalAngle = RotAngle;
PQSCityComponent.reorientInitialUp = Orientation;
PQSCityComponent.Orientate();
}
public bool MakeLaunchSite(bool isLaunchSite, string name = "", string padName = "")
{
if (!isLaunchSite)
{
LaunchSiteName = "";
return true;
}
// Still require the object to contain a spawn point.
// TODO: Add support for multiple spawn points.
if (padName == "./" || StaticGameObject.transform.Find(padName) != null)
{
LaunchSiteName = name;
LaunchPadTransform = padName;
return true;
}
Extensions.LogError("Unable to find the launch spawning transform.");
return false;
}
public override string ToString()
{
return
string.Format(
"NameID: {0}, ObjectID: {1}, CelestialBodyName: {2}, ModelUrl: {3}, ConfigUrl: {4}, RPos: {5}",
NameID, ObjectID, CelestialBodyName, ModelUrl, ConfigURL, RadPosition);
}
//public void Rescale()
//{
// if (StaticGameObject == null) return;
// StaticGameObject.transform.localScale = Scale;
//}
}
}