-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeverdawnCamera.cs
270 lines (217 loc) · 6.27 KB
/
NeverdawnCamera.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
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
[ExecuteInEditMode]
public class NeverdawnCamera : MonoBehaviour {
/// <summary>
/// Global instance of the camera
/// </summary>
private static NeverdawnCamera instance;
/// <summary>
/// The unity camera component
/// </summary>
[SerializeField]
private Camera camera;
/// <summary>
/// The camera rendering the hexagon grid
/// </summary>
[SerializeField]
private HexCamera hexCamera;
/// <summary>
/// Tint for the hexagon grid
/// </summary>
[SerializeField]
private Color hexGridTint;
/// <summary>
/// Size of the hex grid orthogonal camera;
/// </summary>
[SerializeField]
private float hexGridSize;
/// <summary>
/// y-axis offset of the camera lookat
/// </summary>
[SerializeField]
private float targetOffset;
/// <summary>
/// camera offset from the base point
/// </summary>
[SerializeField]
private Vector3 cameraOffset;
/// <summary>
/// Camera lerp speed
/// </summary>
[SerializeField]
private float lerpSpeed;
private Vector3 lookAtOffset;
private Transform target;
private float lerp;
private List<Transform> targets;
private Vector3 targetPosition;
private Transform _cameraTransform;
private Transform _hexCameraTransform;
public Transform cameraTransform
{
get
{
if (!_cameraTransform)
{
_cameraTransform = camera.transform;
}
return _cameraTransform;
}
}
public Transform hexCameraTransform
{
get
{
if (!_hexCameraTransform)
{
_hexCameraTransform = hexCamera.transform;
}
return _hexCameraTransform;
}
}
// Use this for initialization
void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
#if UNITY_EDITOR
//Sets this to not be destroyed when reloading scene
if (Application.isEditor && EditorApplication.isPlaying)
{
#endif
DontDestroyOnLoad(gameObject);
#if UNITY_EDITOR
}
#endif
targets = new List<Transform>();
updateCameras();
}
private void updateCameras()
{
lookAtOffset = new Vector3(0.0f, targetOffset, 0.0f);
cameraTransform.SetParent(transform);
cameraTransform.localPosition = cameraOffset;
cameraTransform.forward = (lookAtOffset - cameraOffset).normalized;
hexCameraTransform.localPosition = cameraTransform.localPosition + new Vector3(0.0f, 0.0f, hexCamera.size / 2.0f);
hexCamera.gridTint = hexGridTint;
hexCamera.size = hexGridSize;
}
void OnDrawGizmos()
{
Gizmos.color = Color.cyan;
Gizmos.DrawWireSphere(transform.position + lookAtOffset, 0.1f);
Gizmos.DrawLine(transform.position + cameraOffset, transform.position + lookAtOffset);
Gizmos.DrawLine(cameraTransform.position, hexCameraTransform.position);
}
void OnValidate()
{
updateCameras();
}
public static bool exists
{
get { return FindObjectOfType<NeverdawnCamera>() != null; }
}
internal static void Destroy()
{
if (instance != null)
{
Destroy(instance.gameObject);
}
}
public static Vector3 position
{
get { return instance.targetPosition; }
set
{
instance.targetPosition = value;
instance.transform.position = value;
}
}
public static void AddTargetLerped(Transform target)
{
instance.lerp = 0.0f;
instance.targets.Add(target);
}
public static void RemoveTargetLerped(Transform target)
{
instance.lerp = 0.0f;
instance.targets.Remove(target);
}
public static void SetTargetLerped(Transform transform)
{
instance.targets.Clear();
instance.targets.Add(transform);
instance.lerp = 0.0f;
}
public static void SetTarget(Transform transform)
{
Clear();
AddTargetLerped(transform);
}
public static void Clear()
{
instance.targets.Clear();
}
public static void ShowGrid(float fadeTime = 0.0f)
{
instance.hexCamera.ShowGrid(fadeTime);
}
public static void HideGrid(float fadeTime = 0.0f)
{
instance.hexCamera.HideGrid(fadeTime);
}
// Update is called once per frame
void Update()
{
if (targets.Count > 0 && Camera.main != null)
{
targetPosition = Vector3.zero;
targets.RemoveAll(t => t == null);
if (targets.Count == 0)
return;
foreach (Transform transform in targets)
{
targetPosition += transform.position;
}
targetPosition /= targets.Count;
targetPosition += lookAtOffset;
Vector3 cameraPosition = cameraOffset + Vector3.up * getTargetSpread();
Vector3 cameraForward = (lookAtOffset - cameraPosition).normalized;
if (lerp < 1.0f)
{
Camera.main.transform.localPosition = Vector3.Lerp(Camera.main.transform.localPosition, cameraPosition, lerp);
Camera.main.transform.forward = Vector3.Lerp(Camera.main.transform.forward, cameraForward, lerp);
transform.position = Vector3.Lerp(transform.position, targetPosition, lerp);
lerp += (Time.deltaTime / lerpSpeed);
}
else
{
Camera.main.transform.forward = cameraForward;
Camera.main.transform.localPosition = cameraPosition;
transform.position = targetPosition;
}
}
}
private float getTargetSpread()
{
float spread = 0.0f;
foreach (Transform transform in targets)
{
float distance = Vector3.Distance(transform.position, targetPosition);
if (distance > spread)
spread = distance;
}
return spread;
}
}