-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraController.cs
More file actions
341 lines (286 loc) · 8.38 KB
/
CameraController.cs
File metadata and controls
341 lines (286 loc) · 8.38 KB
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
341
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.ImageEffects;
using System.Collections;
using System.Collections.Generic;
public class CameraController : MonoBehaviour {
// Objects
private Transform myTransform;
private Camera myCamera;
// Camera zooming
private int zoomFactor = 0;
private int zoomFactorOld = 0;
private float zoomTarget = 0.0f;
private float zoomSpeed = 10.0f;
private List<float> zoom = new List<float>();
// Camera Rotation
private int rotateFactor = 0;
private float rotateSpeed = 10.0f;
private Quaternion rotateTarget = Quaternion.identity;
private List<float> rotate = new List<float>();
// Camera tilt
private float cameraTiltSpeed = 10.0f;
private Vector3 cameraTiltPosition = Vector3.zero;
private Vector3 cameraTiltPositionMin = new Vector3(0.0f, 20.0f, -70.0f);
private Vector3 cameraTiltPositionMax = new Vector3(0.0f, 50.0f, 0.0f);
private Vector3 cameraTiltPositionDefault = new Vector3(0.0f, 50, -70.0f);
private Vector3 cameraTiltRotation = Vector3.zero;
private Vector3 cameraTiltRotationMin = new Vector3(15.0f, 0.0f, 0.0f);
private Vector3 cameraTiltRotationMax = new Vector3(90.0f, 0.0f, 0.0f);
private Vector3 cameraTiltRotationDefault = new Vector3(35.0f, 0.0f, 0.0f);
// Focus on target
private bool focusing = true;
private float focusSpeed = 5.0f;
// Movement
private Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 0.0f);
private Vector3 cameraSpeed = new Vector3(20.0f, 0.0f, 20.0f);
// Camera bounds
private float cameraMaxZ = 75.0f;
private float cameraMinZ = -75.0f;
private float cameraMaxX = 75.0f;
private float cameraMinX = -75.0f;
// Default values for map screen
private float mapMaxZ = 75.0f;
private float mapMinZ = -75.0f;
private float mapMaxX = 75.0f;
private float mapMinX = -75.0f;
// Effects
private BlurOptimized blur;
private float blurValue = 0.0f;
private float blurMax = 3.0f;
private bool effects = false;
private float effectsSpeed = 0.0f;
// Flash
private Image flash;
private Color flashColor = new Color(1.0f, 1.0f, 1.0f, 0.0f);
private bool flashActive = false;
private float flashAlpha = 0.0f;
private float flashSpeed = 0.0f;
private float flashMax = 0.75f;
void Start () {
myTransform = transform;
myCamera = myTransform.Find("Camera").GetComponent<Camera>();
blur = myCamera.GetComponent<BlurOptimized>();
flash = myTransform.parent.Find("GUI/Effects/Flash").GetComponent<Image>();
// Add list of zooms
zoom.Add(5.0f);
zoom.Add(10.0f);
zoom.Add(20.0f);
zoom.Add(30.0f);
zoom.Add(50.0f);
// Add list of rotates
rotate.Add(0.0f);
rotate.Add(120.0f);
rotate.Add(240.0f);
// Default tilt
cameraTiltPosition = cameraTiltPositionDefault;
cameraTiltRotation = cameraTiltRotationDefault;
// Default camera bounds
cameraMaxZ = mapMaxZ;
cameraMinZ = mapMinZ;
cameraMaxX = mapMaxX;
cameraMinX = mapMinX;
}
void Update () {
if(!Global.system.isPaused) {
// Focus on player
Focus();
// Camera movement
Zoom();
Movement();
Rotation();
// Camera effects
TiltDisplay();
EffectsDisplay();
FlashDisplay();
}
}
// CAMERA BOUNDS
public void Bounds(float top, float bottom, float left, float right) {
cameraMaxZ = top;
cameraMinZ = bottom;
cameraMaxX = right;
cameraMinX = left;
}
// CAMERA FOCUS ON TARGET
void Focus() {
if(Input.GetKeyDown(KeyCode.F)) {
if(Global.system.focusTarget != null) {
focusing = true;
Vector3 newTarget = new Vector3(Global.system.focusTarget.position.x, myTransform.position.y, Global.system.focusTarget.position.z);
float dist = Vector3.Distance(myTransform.position, newTarget);
if(dist > 10.0f) {
EffectsActivate(focusSpeed);
}
}
}
if(focusing) {
if(Global.system.focusTarget != null) {
Vector3 newTarget = new Vector3(Global.system.focusTarget.position.x, myTransform.position.y, Global.system.focusTarget.position.z);
float dist = Vector3.Distance(myTransform.position, newTarget);
if(dist < 0.05f) {
myTransform.position = newTarget;
focusing = false;
}
else {
myTransform.position = Vector3.Lerp(myTransform.position, newTarget, focusSpeed * Time.deltaTime);
}
}
}
}
// CAMERA ZOOM
void Zoom() {
if(Input.GetAxis("Mouse ScrollWheel") < 0) {
ZoomChange(false);
}
else if(Input.GetAxis("Mouse ScrollWheel") > 0) {
ZoomChange(true);
}
zoomTarget = Mathf.Lerp(myCamera.orthographicSize, zoom[zoomFactor], zoomSpeed * Time.deltaTime);
myCamera.orthographicSize = zoomTarget;
}
void ZoomChange(bool lower) {
if(lower && zoomFactor > 0) {
zoomFactor--;
EffectsActivate(zoomSpeed);
}
else if(!lower && zoomFactor < zoom.Count - 1) {
zoomFactor++;
EffectsActivate(zoomSpeed);
}
zoomFactor = Mathf.Clamp(zoomFactor, 0, zoom.Count - 1);
if(zoomFactor == zoom.Count - 1) {
if(zoomFactorOld != zoomFactor) {
Global.system.weather.CloudsActivate(true);
}
}
else {
Global.system.weather.CloudsActivate(false);
}
FlashActivate(zoomSpeed);
TiltActivate();
zoomFactorOld = zoomFactor;
}
// CAMERA MOVEMENT
void Movement() {
cameraPosition.x = 0.0f;
cameraPosition.z = 0.0f;
if(Input.GetKey(KeyCode.W)) {
cameraPosition.z += cameraSpeed.x * Time.deltaTime;
focusing = false;
Speed();
}
if(Input.GetKey(KeyCode.S)) {
cameraPosition.z -= cameraSpeed.x * Time.deltaTime;
focusing = false;
Speed();
}
if(Input.GetKey(KeyCode.A)) {
cameraPosition.x -= cameraSpeed.x * Time.deltaTime;
focusing = false;
Speed();
}
if(Input.GetKey(KeyCode.D)) {
cameraPosition.x += cameraSpeed.x * Time.deltaTime;
focusing = false;
Speed();
}
// Move with mouse button and mouse movement
//if(Input.GetMouseButton(2)) {
// cameraPosition.x = -cameraSpeed.x * Input.GetAxis("Mouse X");
// cameraPosition.z = -cameraSpeed.z * Input.GetAxis("Mouse Y");
// myTransform.Translate(cameraPosition);
// CameraCheck();
//}
}
void Speed() {
if(cameraPosition.x != 0 && cameraPosition.z != 0) {
cameraPosition *= 0.7071f;
}
Check();
myTransform.Translate(cameraPosition);
}
void Check() {
Vector3 myPos = myTransform.localPosition;
myPos.z = Mathf.Clamp(myPos.z, cameraMinZ, cameraMaxZ);
myPos.x = Mathf.Clamp(myPos.x, cameraMinX, cameraMaxX);
myTransform.localPosition = myPos;
}
// CAMERA ROTATION
void Rotation() {
if(Input.GetKeyDown(KeyCode.Q)) {
rotateFactor++;
EffectsActivate(rotateSpeed);
FlashActivate(rotateSpeed);
}
if(Input.GetKeyDown(KeyCode.E)) {
rotateFactor--;
EffectsActivate(rotateSpeed);
FlashActivate(rotateSpeed);
}
if(rotateFactor < 0) {
rotateFactor = rotate.Count - 1;
}
else if(rotateFactor > rotate.Count - 1) {
rotateFactor = 0;
}
rotateTarget = Quaternion.Lerp(myTransform.rotation, Quaternion.Euler(0, rotate[rotateFactor], 0), rotateSpeed * Time.deltaTime);
myTransform.rotation = rotateTarget;
}
// CAMERA TILT
void TiltActivate() {
if(zoomFactor == 0) {
cameraTiltRotation = cameraTiltRotationMin;
cameraTiltPosition = cameraTiltPositionMin;
}
else if(zoomFactor == zoom.Count - 1) {
cameraTiltRotation = cameraTiltRotationMax;
cameraTiltPosition = cameraTiltPositionMax;
}
else {
cameraTiltRotation = cameraTiltRotationDefault;
cameraTiltPosition = cameraTiltPositionDefault;
}
}
void TiltDisplay() {
myCamera.transform.localEulerAngles = Vector3.Lerp(myCamera.transform.localEulerAngles, cameraTiltRotation, cameraTiltSpeed * Time.deltaTime);
myCamera.transform.localPosition = Vector3.Lerp(myCamera.transform.localPosition, cameraTiltPosition, cameraTiltSpeed * Time.deltaTime);
}
// CAMERA EFFECTS
void EffectsActivate(float speed) {
effectsSpeed = Mathf.Abs(speed) / 1.5f;
blurValue = blurMax;
blur.enabled = true;
effects = true;
}
void EffectsDisplay() {
if(effects) {
blurValue -= effectsSpeed * Time.deltaTime;
blurValue = Mathf.Clamp(blurValue, 0.0f, blurMax);
blur.blurSize = blurValue;
if(blurValue <= 0.0f) {
effects = false;
blur.enabled = false;
}
}
}
void FlashActivate(float speed) {
flashActive = true;
flashAlpha = flashMax;
flashSpeed = Mathf.Abs(speed) / 10.0f;
flashColor = Global.system.weather.ambientColor;
}
void FlashDisplay() {
if(flashActive) {
if(flashAlpha <= 0.0f) {
flashAlpha = 0.0f;
flashActive = false;
}
else {
flashAlpha -= flashSpeed * Time.deltaTime;
}
flashColor.a = flashAlpha;
flash.color = flashColor;
}
}
}