/
SelectionManager.cs
306 lines (276 loc) · 8.79 KB
/
SelectionManager.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections.Generic;
using SinglePlayer;
using Analytics;
namespace MultiPlayer {
public class SelectionManager : NetworkBehaviour {
public const int MAX_UNIT_COUNT = 50;
public List<GameObject> selectedObjects;
public List<GameObject> allObjects;
public List<GameObject> removeList;
public Rect selectionBox;
public NetworkConnection authorityOwner;
public Camera minimapCamera;
public Vector3 initialClick;
public Vector3 screenPoint;
public Text unitCountText;
public int currentMaxUnitCount = MAX_UNIT_COUNT;
public bool isSelecting;
public bool isBoxSelecting;
public bool isDead;
public override void OnStartClient() {
base.OnStartClient();
if (this.minimapCamera == null) {
GameObject obj = GameObject.FindGameObjectWithTag("Minimap");
if (obj != null) {
this.minimapCamera = obj.GetComponent<Camera>();
if (this.minimapCamera == null) {
Debug.LogError("Failure to obtain minimap camera.");
}
}
}
}
void Start() {
//If you need to use a different design instead of checking for hasAuthority, then it means
//you will have to figure out how to do what you need to do, and this example will not
//be sufficient enough to teach you more than given.
if (!this.hasAuthority) {
return;
}
this.isDead = false;
this.InitializeList();
this.selectionBox = new Rect();
GameObject[] selectionManagers = GameObject.FindGameObjectsWithTag("SelectionManager");
foreach (GameObject manager in selectionManagers) {
SelectionManager selectManager = manager.GetComponent<SelectionManager>();
if (selectManager == null || !selectManager.hasAuthority) {
continue;
}
GameObject[] units = GameObject.FindGameObjectsWithTag("Unit");
foreach (GameObject unit in units) {
GameUnit gameUnit = unit.GetComponent<GameUnit>();
if (gameUnit != null && !gameUnit.hasAuthority) {
continue;
}
selectManager.allObjects.Add(unit);
}
}
}
void Update() {
if (!this.hasAuthority) {
return;
}
if (this.minimapCamera == null) {
return;
}
if (this.allObjects.Count <= 0) {
if (!this.isDead) {
GameMetricLogger.ShowPrintLog();
AIManager.Instance.startAIFlag = false;
this.isDead = true;
}
return;
}
//This handles all the input actions the player has done in the minimap.
this.screenPoint = Camera.main.ScreenToViewportPoint(Input.mousePosition);
if (this.minimapCamera.rect.Contains(this.screenPoint) && Input.GetMouseButtonDown(1)) {
if (this.selectedObjects.Count > 0) {
float mainX = (this.screenPoint.x - this.minimapCamera.rect.xMin) / (1.0f - this.minimapCamera.rect.xMin);
float mainY = (this.screenPoint.y) / (this.minimapCamera.rect.yMax);
Vector3 minimapScreenPoint = new Vector3(mainX, mainY, 0f);
foreach (GameObject obj in this.selectedObjects) {
GameUnit unit = obj.GetComponent<GameUnit>();
if (unit != null) {
unit.CastRay(true, minimapScreenPoint, this.minimapCamera);
}
}
}
}
else {
if (this.minimapCamera.rect.Contains(this.screenPoint)) {
return;
}
//This handles all the input actions the player has done to box select in the game.
//Currently, it doesn't handle clicking to select.
if (Input.GetMouseButtonDown(0)) {
if (!(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))) {
ClearSelectObjects();
}
this.isSelecting = true;
this.initialClick = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0)) {
if (!(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))) {
ClearSelectObjects();
}
SelectObjectAtPoint();
SelectObjectsInRect();
SelectObjects();
this.isSelecting = false;
this.isBoxSelecting = false;
this.initialClick = -Vector3.one * 9999f;
}
}
if (this.isSelecting && Input.GetMouseButton(0)) {
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) {
this.isBoxSelecting = true;
}
this.selectionBox.Set(this.initialClick.x, Screen.height - this.initialClick.y, Input.mousePosition.x - this.initialClick.x, (Screen.height - Input.mousePosition.y) - (Screen.height - this.initialClick.y));
if (this.selectionBox.width < 0) {
this.selectionBox.x += this.selectionBox.width;
this.selectionBox.width *= -1f;
}
if (this.selectionBox.height < 0) {
this.selectionBox.y += this.selectionBox.height;
this.selectionBox.height *= -1f;
}
TempRectSelectObjects();
}
foreach (GameObject obj in this.allObjects) {
if (obj == null && !this.removeList.Contains(obj)) {
this.removeList.Add(obj);
}
}
for (int i = 0; i < this.selectedObjects.Count; i++) {
if (this.selectedObjects[i] == null) {
this.selectedObjects.RemoveAt(i);
}
}
if (this.removeList.Count > 0) {
foreach (GameObject obj in this.removeList) {
if (this.allObjects.Contains(obj)) {
this.allObjects.Remove(obj);
}
}
foreach (GameObject obj in this.removeList) {
CmdDestroy(obj);
}
this.removeList.Clear();
}
}
void OnGUI() {
if (Taskbar.Instance != null) {
Taskbar.Instance.taskbarText.text = "Unit Count / Max Unit Count : " + this.allObjects.Count + "/" + this.currentMaxUnitCount;
}
}
public void InitializeList() {
if (this.selectedObjects == null) {
this.selectedObjects = new List<GameObject>(100);
}
if (this.allObjects == null) {
this.allObjects = new List<GameObject>(100);
}
}
public void AddToRemoveList(GameObject obj) {
if (!this.removeList.Contains(obj)) {
this.removeList.Add(obj);
}
}
[Command]
public void CmdDestroy(GameObject obj) {
NetworkServer.Destroy(obj);
}
//----------- Private class methods may all need refactoring --------------------
private void TempRectSelectObjects() {
foreach (GameObject obj in this.allObjects) {
if (obj == null) {
//Because merging units will actually destroy units (as a resource), we now added a check to make sure
//we don't call on NULL referenced objects, and remove them from the list.
this.removeList.Add(obj);
continue;
}
Vector3 projectedPosition = Camera.main.WorldToScreenPoint(obj.transform.position);
projectedPosition.y = Screen.height - projectedPosition.y;
GameUnit unit = obj.GetComponent<GameUnit>();
if (this.selectionBox.Contains(projectedPosition)) {
unit.isSelected = true;
}
}
}
private void SelectObjects() {
foreach (GameObject obj in this.allObjects) {
if (obj == null) {
this.removeList.Add(obj);
continue;
}
GameUnit unit = obj.GetComponent<GameUnit>();
if (unit != null) {
if (this.selectedObjects.Contains(obj)) {
unit.isSelected = true;
}
}
}
}
private void SelectObjectsInRect() {
foreach (GameObject obj in this.allObjects) {
if (obj == null) {
continue;
}
GameUnit unit = obj.GetComponent<GameUnit>();
if (unit != null) {
if (this.isBoxSelecting) {
Vector3 projectedPosition = Camera.main.WorldToScreenPoint(obj.transform.position);
projectedPosition.y = Screen.height - projectedPosition.y;
if (this.selectionBox.Contains(projectedPosition)) {
if (this.selectedObjects.Contains(obj)) {
unit.isSelected = false;
this.selectedObjects.Remove(obj);
}
else {
unit.isSelected = true;
this.selectedObjects.Add(obj);
}
}
}
else {
if (unit.isSelected) {
if (!this.selectedObjects.Contains(obj)) {
this.selectedObjects.Add(obj);
}
}
}
}
}
}
private void ClearSelectObjects() {
foreach (GameObject obj in this.selectedObjects) {
if (obj == null) {
continue;
}
GameUnit unit = obj.GetComponent<GameUnit>();
unit.isSelected = false;
}
this.selectedObjects.Clear();
}
private void SelectObjectAtPoint() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hits = Physics.RaycastAll(ray);
foreach (RaycastHit hit in hits) {
GameObject obj = hit.collider.gameObject;
if (obj.tag.Equals("Unit")) {
GameUnit unit = obj.GetComponent<GameUnit>();
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) {
if (this.allObjects.Contains(obj)) {
if (!this.selectedObjects.Contains(obj)) {
unit.isSelected = true;
this.selectedObjects.Add(obj);
}
else if (this.selectedObjects.Contains(obj)) {
unit.isSelected = false;
this.selectedObjects.Remove(obj);
}
}
}
else {
if (unit != null) {
unit.isSelected = true;
this.selectedObjects.Add(obj);
}
}
}
}
}
}
}