/
Game.cs
140 lines (110 loc) · 3.67 KB
/
Game.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
using UnityEngine;
using System.Collections;
public class Game : MonoBehaviour {
//Global static variables
public static GameObject SelectedObject;
public static bool lockSelection;
public static readonly float SPEED_OF_LIGHT = 200;
public static readonly float DOUBLE_CLICK_THRESHHOLD_INTERVAL = 0.25f;
//Persistent reference to the point light used to highlight selected objects
public static Highlight highlight;
public static CameraControls mainCamera;
public static Planet lastPlanet;
public static UI gui;
public static PlayerState state;
public static BuildableRegistry buildables;
private static Game gameInstance;
void Awake ()
{
highlight = GameObject.FindWithTag("Highlight").GetComponent<Highlight>();
mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraControls>();
gameInstance = this;
gui = new UI();
gui.Initialize();
buildables = GetComponent<BuildableRegistry>(); //must be before state, because state references this in initialization
state = new PlayerState();
}
void Start()
{
lastPlanet = GameObject.FindGameObjectWithTag("StartingPlanet").GetComponent<Planet>();
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
DeSelect();
}
}
#region selection
public static void SelectObject(GameObject target, bool centerCamera)
{
SelectObject(target, centerCamera, true, true);
}
public static void SelectObject(GameObject target, bool centerCamera, bool cameraMatchRotation, bool doHighlight)
{
if(lockSelection)
return;
if(centerCamera)
mainCamera.CenterOn(target, 0.25f, cameraMatchRotation);
else
mainCamera.UnCenter();
SelectedObject = target;
SelectedObject.SendMessage("OnPlayerSelect", SendMessageOptions.DontRequireReceiver);
if(doHighlight)
{
highlight.attach(target);
highlight.show();
}
}
public static void DeSelect()
{
if(lockSelection)
return;
highlight.hide();
mainCamera.UnCenter();
if(SelectedObject != null)
SelectedObject.SendMessage("OnPlayerDeSelect", SendMessageOptions.DontRequireReceiver);
SelectedObject = null;
}
#endregion
/// <summary>
/// Returns focus to the last selected planet.
/// </summary>
/// <param name='delay'>
/// Delay in seconds before returning
/// </param>
public static void ReturnToLastPlanet(float delay)
{
if(lastPlanet == null)
{
lastPlanet = GameObject.FindWithTag("StartingPlanet").GetComponent<Planet>();
}
SafeZone lastSafeZone = lastPlanet.transform.Find("SafeZone").GetComponent<SafeZone>();
gameInstance.StartCoroutine(gameInstance.RestoreLastPlanet(delay, lastSafeZone));
}
private IEnumerator RestoreLastPlanet(float delay, SafeZone lastSafeZone)
{
yield return new WaitForSeconds(delay);
mainCamera.CenterOn(lastSafeZone.gameObject, 0, false);
}
public static Vector3 ProjectScreenPointToWorldPlane(Vector3 screenPoint)
{
screenPoint.z = 1;
screenPoint = Camera.main.ScreenToWorldPoint(screenPoint);
Vector3 cameraPosition = mainCamera.transform.position;
Vector3 directionToWorld = (screenPoint - cameraPosition).normalized;
Plane playingField = new Plane(Vector3.up, Vector3.zero); //the plane y = 0;
Ray cameraToWorldByScreenPoint = new Ray(cameraPosition, directionToWorld);
float projectionDistance;
playingField.Raycast(cameraToWorldByScreenPoint, out projectionDistance);
Vector3 result = cameraPosition + (directionToWorld * projectionDistance);
//GameObject.Find("Marker").transform.position = result;
return result;
}
public static Vector3 GetMousePositon()
{
Vector3 mousePosition = Input.mousePosition;
return ProjectScreenPointToWorldPlane(mousePosition);
}
}