-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraMovement.cs
287 lines (265 loc) · 8.08 KB
/
CameraMovement.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
/*alls GUI components need the variables from the various scripts for each of the planets*/
//defines the logic for Camera movement
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraMovement : MonoBehaviour {
public Camera MainCamera;
public float speed = 5;
public float scrollSpeed = 5;
public int speedMultiplier =10;
private int screenWidth;
private int screenHeight;
public int boundary =50;
private int clicks;
private GameObject clicked;
private GameObject nextClicked;
RaycastHit hit;
private bool zoomed;
public float timer=0;
private float timeToHover=.5f;
public bool displayInfo;
private float zoomedFieldOfView=10;
public List<GameObject> solarSystems;
public int maxInMovement = 200;
public int maxOutMovement = 1000;
public Vector3 lastCameraPos;
// Use this for initialization
void Start () {
//solarSystems = new List<GameObject>();
for(int i =0; i < solarSystems.Count; i++)
{
solarSystems[i].renderer.enabled= false;
solarSystems[i].collider.enabled= false;
}
screenWidth = Screen.width;
screenHeight = Screen.height;
clicks =0;
zoomed = false;
}
void OnGUI()
{
if(displayInfo)
{
displayBasicInfo();
}
if(zoomed)
{
displayDetailedInfo();
}
}
// Update is called once per frame
void Update () {
speed = MainCamera.orthographicSize*speedMultiplier;
//keyboard input
if(Input.GetKey(KeyCode.A)||Input.GetKey(KeyCode.LeftArrow))
{
if(zoomed)
exitZoom();
MainCamera.transform.position += new Vector3((speed*-Time.deltaTime),0,0);
}
if(Input.GetKey(KeyCode.D)||Input.GetKey(KeyCode.RightArrow))
{
if(zoomed)
exitZoom();
MainCamera.transform.position += new Vector3((speed*Time.deltaTime),0,0);
}
if(Input.GetKey(KeyCode.S)||Input.GetKey(KeyCode.DownArrow))
{
if(zoomed)
exitZoom();
MainCamera.transform.position += new Vector3(0,0,speed*-Time.deltaTime);
}
if(Input.GetKey(KeyCode.W)||Input.GetKey(KeyCode.UpArrow))
{
if(zoomed)
exitZoom();
MainCamera.transform.position += new Vector3(0,0,speed*Time.deltaTime);
}
//zoom
if(Input.GetAxis("Mouse ScrollWheel")!=0)
{
if(zoomed)
exitZoom();
//Limit movement
if(MainCamera.orthographicSize<=maxInMovement){
if(Input.GetAxis("Mouse ScrollWheel") < 0)
MainCamera.orthographicSize -= Input.GetAxis("Mouse ScrollWheel")*scrollSpeed;
}
else if(MainCamera.orthographicSize>=maxOutMovement){
if(Input.GetAxis("Mouse ScrollWheel") > 0)
MainCamera.orthographicSize -= Input.GetAxis("Mouse ScrollWheel")*scrollSpeed;
}
else{
MainCamera.orthographicSize -= Input.GetAxis("Mouse ScrollWheel")*scrollSpeed;
// set last quarter of movement to solar system view
if(MainCamera.orthographicSize >= maxOutMovement - maxOutMovement/4)
{
for(int i =0; i < solarSystems.Count; i++)
{
solarSystems[i].renderer.enabled=true;
solarSystems[i].collider.enabled=true;
}
}
else
{
//Debug.Log("HERE");
if(solarSystems[0].renderer.enabled == true)
{
for(int i =0; i < solarSystems.Count; i++)
{
solarSystems[i].renderer.enabled = false;
solarSystems[i].collider.enabled = false;
}
}
}
}
}
//Edge scrolling
/*if (Input.mousePosition.x > screenWidth - boundary)
{
MainCamera.transform.position += new Vector3((speed*Time.deltaTime),0,0); // move on +X axis
}
if (Input.mousePosition.x < 0 + boundary)
{
MainCamera.transform.position += new Vector3((speed*-Time.deltaTime),0,0); // move on -X axis
}
if (Input.mousePosition.y > screenHeight - boundary)
{
MainCamera.transform.position += new Vector3(0,0,speed*Time.deltaTime); // move on +Z axis
}
if (Input.mousePosition.y < 0 + boundary)
{
MainCamera.transform.position += new Vector3(0,0,speed*-Time.deltaTime); // move on -Z axis
}*/
zoomToPlanet();
showPlanetInfo();
}
//exits the zoomed planet mode
void exitZoom(){
//changes the camera back to orthographic view
MainCamera.isOrthoGraphic = true;
if(clicked!=null)
{
MainCamera.orthographicSize = clicked.transform.localScale.x*clicked.transform.parent.localScale.x;
MainCamera.transform.position = new Vector3(clicked.transform.position.x,100,clicked.transform.position.z);
}
else
{
MainCamera.transform.position = lastCameraPos;
MainCamera.orthographicSize = maxInMovement;
}
MainCamera.transform.eulerAngles =new Vector3(90,0,0);
zoomed = false;
clicks = 0;
//if solar system plane is visible turn it invisible
if(solarSystems[0].renderer.enabled == true)
{
for(int i =0; i < solarSystems.Count; i++)
{
solarSystems[i].renderer.enabled= false;
solarSystems[i].collider.enabled= false;
}
}
}
//enables the planet info GUI to be shown
void showPlanetInfo()
{
nextClicked =GetClickedGameObject();
if(nextClicked != null && nextClicked.tag == "Planet" && !zoomed)
{
timer += Time.deltaTime;
if(timer >= timeToHover)
{
displayInfo =true;
}
}
else
{
displayInfo=false;
timer =0;
}
}
//shows the planets detailed info
void displayDetailedInfo()
{
GUI.BeginGroup(new Rect(screenWidth/2+100, screenHeight/2-100,300,400));
if(clicked!=null && clicked.GetComponent<Planet>().owningPlayer != null)
{
GUI.Label(new Rect(0,20,100,100), "Owner: "+clicked.GetComponent<Planet>().owningPlayer.gameObject.name);
}
else
{
GUI.Label(new Rect(0,20,100,100), "Owner: None");
}
if(clicked!=null)
{
GUI.Label(new Rect(0,35,100,100), "Unit Count: "+clicked.GetComponent<Planet>().numOfUnits);
GUI.Label (new Rect(0,50,200,400),"Solar System: "+clicked.GetComponent<Planet>().solarSystem.gameObject.name);
GUI.Label(new Rect(0,65,200,400), "Solar System Control: "
+clicked.GetComponent<Planet>().solarSystem.GetComponent<SolarSystem>().calculateOwnedPlanets()
+"/"+clicked.GetComponent<Planet>().solarSystem.GetComponent<SolarSystem>().planets.Count);
}
GUI.EndGroup();
}
//zooms to a planet when it is double clicked
void zoomToPlanet(){
if(Input.GetMouseButtonDown(0))
{
if(clicks==0)
{
clicked = GetClickedGameObject();
clicks++;
}
else{
//checks if the same gameobject is clicked twice in a row
if(clicked!=null &&(clicked == GetClickedGameObject()))
{
lastCameraPos = MainCamera.transform.position;
Debug.Log(clicked.transform.position.x);
//changes the camera to perspective and sets up the angle to see the planet
MainCamera.transform.position = new Vector3(clicked.transform.position.x+
(clicked.transform.localScale.x*(clicked.transform.parent.localScale.x/2)) ,0,
clicked.transform.position.z-clicked.transform.localScale.x*clicked.transform.parent.localScale.x*10);
MainCamera.transform.eulerAngles =new Vector3(0,0,0);
MainCamera.isOrthoGraphic = false;
zoomedFieldOfView = 10;
MainCamera.fieldOfView = zoomedFieldOfView;
zoomed = true;
}
//if the same game object was not clicked twice
else
{
clicks =0;
}
}
}
}
//displays the gui for the basic info of a planet
void displayBasicInfo()
{
if(nextClicked.tag == "Planet" && nextClicked.GetComponent<Planet>().owningPlayer != null)
{
GUI.Box(new Rect(Input.mousePosition.x,Screen.height-Input.mousePosition.y,100,50), "Units: "
+nextClicked.GetComponent<Planet>().numOfUnits+" \nOwner: "+nextClicked.GetComponent<Planet>().owningPlayer.gameObject.name);
}
else
{
GUI.Box(new Rect(Input.mousePosition.x,Screen.height-Input.mousePosition.y,100,50), "Units: "
+nextClicked.GetComponent<Planet>().numOfUnits+" \nOwner: None");
}
//new Rect(
}
//returns the gameobject under the mouse
GameObject GetClickedGameObject()
{
// Builds a ray from camera point of view to the mouse position
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Casts the ray and get the first game object hit
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
return hit.transform.gameObject;
else
return null;
}
}