-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIControl.cs
342 lines (288 loc) · 10.9 KB
/
UIControl.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
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
342
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//This class is, for all intents and purposes authored by me. The 'draw mesh' of the ghost targets was taken from
//'Util' code authored by Vicente Soler.
public class UIControl : MonoBehaviour
{
[SerializeField]
GUISkin _skin;
public BuildCity city;
int maxW = 3;
int maxH = 3;
BuildWindow[,] windowArray;
BusinessLogic gameStats;
GameObject mainController;
GameObject gameTimeObj;
GameTime gameTimeInst;
float elapsedGameTime;
float startTime;
List<Job> _allJobs;
int jobCount;
//game stats
int _fleetSize;
float _unitCostIdle;
float _unitCostActive;
float _fleetCost;
float _revenue;
float _profit;
float _globalPayout;
List<Rect> rectangles = new List<Rect>();
Rect _windowRect = new Rect(20, 20, 150, 160);
int windowCount;
bool gameStarted;
bool gameWon;
public void Awake()
{
mainController = GameObject.Find("MainController");
gameTimeObj = GameObject.Find("GameTime");
gameStats = mainController.GetComponent<BusinessLogic>();
gameTimeInst = gameTimeObj.GetComponent<GameTime>();
InitWindowArray();
_allJobs = new List<Job>();
_globalPayout = new float();
gameStarted = false;
startTime = 0.0f;
gameWon = false;
_fleetCost = 10.0f;
_revenue = 1.0f;
}
// Start is called before the first frame update
public void Start()
{
city = new BuildCity(7, 7);
city.Start();
StartCoroutine(city.WakeCity());
jobCount = -1;
}
public BuildCity getCity()
{
return city;
}
public void InitWindowArray()
{
windowArray = new BuildWindow[maxW, maxH];
int count = 0;
for (int h = 0; h < maxH; h++)
{
for (int w = 0; w < maxW; w++)
{
windowArray[w, h] = new BuildWindow();
windowArray[w, h].ActiveJob = false;
windowArray[w, h].On = false;
windowArray[w, h].Position = new Vector3Int(w, h, 0);
windowArray[w, h].ID = count;
windowArray[w, h].Name = count.ToString();
windowArray[w, h].Rectangle = new Rect(windowArray[w, h].Position.x * 180 + 50, windowArray[w, h].Position.y *200 + 50, 180 , 180 );
count++;
}
}
}
public void OnGUI()
{
GUI.skin = _skin;
var maxHM = (int)(_allJobs.Count / maxH);
var maxWM = (int)(_allJobs.Count % maxW);
var modH = 0;
if (maxWM == 0)
modH = maxHM;
else
modH = maxHM + 1;
var modW = _allJobs.Count >= 3 ? 3 : _allJobs.Count;
int count = 0;
for (int h = 0; h < modH; h++)
{
for (int w = 0; w < modW; w++)
{
if (count >= _allJobs.Count)
break;
else
{
if(windowArray[w,h].ActiveJob)
{
GUI.color = Color.green;
GUI.Window(windowArray[w, h].ID, windowArray[w, h].Rectangle, WindowFunction, windowArray[w, h].Name);
}
else
{
GUI.color = Color.gray;
GUI.Window(windowArray[w, h].ID, windowArray[w, h].Rectangle, WindowFunction, windowArray[w, h].Name);
}
count++;
}
}
}
if (gameWon)
GUI.Label(new Rect(600,300,800,600), "NOW THAT IS A BUSINESS!");
//gamstats
Rect rectangle = new Rect(50, 600, 300, 300);
GUI.color = Color.gray;
GUI.Window(800, new Rect(rectangle), statFunc, "Game Stats");
//reset city
Rect rectangle1 = new Rect(50, 915, 120, 80);
GUI.color = Color.gray;
if (GUI.Button(rectangle1, "RESET CITY"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Time.timeScale = 1.0f;
}
//start fleet coroutine
Rect rectangle2 = new Rect(240, 915, 100, 80);
GUI.color = Color.gray;
if (GUI.Button(rectangle2, "START"))
{
StartCoroutine(city.army.MoveDudes());
}
}
//game stats
void statFunc(int ID)
{
int i = 1;
int m = 30;
int s = 35;
GUI.TextField(new Rect(m, s * i++, 240, m), "Fleet Size " + string.Format("{0:0}", _fleetSize));
GUI.TextField(new Rect(m, s * i++, 240, m), "Idle Unit Cost " + string.Format("${0:0.00}", _unitCostIdle));
GUI.TextField(new Rect(m, s * i++, 240, m), "Active Unit Cost " + string.Format("${0:0.00}", _unitCostActive));
GUI.TextField(new Rect(m, s * i++, 240, m), "Fleet Cost " + string.Format("${0:0.00}", _fleetCost));
GUI.TextField(new Rect(m, s * i++, 240, m), "Revenue " + string.Format("${0:0.00}", _revenue));
GUI.TextField(new Rect(m, s * i++, 240, m), "Profit " + string.Format("${0:0.00}", _profit));
GUI.TextField(new Rect(m, s * i++, 240, m), "Elapsed Time " + string.Format("{0:0.00}", elapsedGameTime));
}
void WindowFunction(int windowID)
{
var maxHM = (int)(_allJobs.Count / maxH);
var maxWM = (int)(_allJobs.Count % maxW);
var modH = 0;
if (maxWM == 0)
modH = maxHM;
else
modH = maxHM + 1;
var modW = _allJobs.Count >= 3 ? 3 : _allJobs.Count;
int count = 0;
for (int h = 0; h < modH; h++)
{
for (int w = 0; w < modW; w++)
{
int i = 1;
int s = 25;
if (windowArray[w, h].ID == windowID)
{
if (GUI.Button(new Rect(s, s * i++, 125, 20), "Pursue"))
{
{
windowArray[w, h].Job.pursue = true;
windowArray[w, h].ActiveJob = true;
}
}
if (GUI.Button(new Rect(s, s * i++, 125, 20), "Abort"))
{
{
windowArray[w, h].Job.abort = true;
windowArray[w, h].ActiveJob = false;
}
}
var payout = windowArray[w, h].Payout;
var timeReq = windowArray[w, h].TimeReq;
var timePassed = windowArray[w, h].TimeElapsed;
var fleetReq = windowArray[w, h].FleetReq;
var fleetInit = windowArray[w, h].FleetInitCount;
GUI.TextArea(new Rect(s, s * i++, 125, 20), "F_REQ " + string.Format("{0:0}/{1:0}", fleetReq, fleetInit));
GUI.TextArea(new Rect(s, s * i++, 125, 20), "PAYOUT " + string.Format("${0:0.00}", payout));
GUI.TextArea(new Rect(s, s * i++, 125, 20), "TIME REQ " + string.Format("{0:0.00}", timeReq));
GUI.TextArea(new Rect(s, s * i++, 125, 20), "TIME LEFT " + string.Format("{0:0.00}", timePassed));
count++;
}
}
}
}
void Update()
{
city.Update();
//end game if this condition is met
if (_revenue>= _fleetCost)
{
gameWon = true;
Debug.Log("Your business broke even!");
Time.timeScale = 0.0f;
}
//draw all active targets
var strTargets = city.totalJobIndices;
for (int i = 0; i < strTargets.Count; i++)
{
city.army.grid.Voxels.TryGetValue(strTargets[i], out var end);
var center = end.Center;
Drawing.DrawCube(center, 1.8f, 0.1f);
}
findJobsNew();
RedrawWindows();
_fleetSize = gameStats.getFleetSize();
_unitCostIdle = gameStats.getUnitCostIdle();
_unitCostActive = gameStats.getUnitCostActive();
_fleetCost = gameStats.getFleetCost();
_revenue = gameStats.getRevenue();
_profit = gameStats.getProfit();
elapsedGameTime = gameTimeInst.getTime();
}
public void findJobsNew()
{
var jobs = city.UpdateAllJobs();
_allJobs = jobs;
}
public void RedrawWindows()
{
int count = 0;
var maxHM = (int)(_allJobs.Count / maxH);
var maxWM = (int)(_allJobs.Count % maxW);
var modH = 0;
if (maxWM == 0)
modH = maxHM;
else
modH = maxHM + 1;
var modW = _allJobs.Count >= 3 ? 3 : _allJobs.Count;
for (int h = 0; h < modH; h++)
{
for (int w = 0; w < modW; w++)
{
windowArray[w, h].ID = count;
for (int i = 0; i < _allJobs.Count; i++)
{
if (windowArray[w, h].ID == i)
{
{
windowArray[w, h].Job = _allJobs[i];
windowArray[w, h].Name = windowArray[w, h].Job.name;
windowArray[w, h].Payout = windowArray[w, h].Job.payout;
windowArray[w, h].TimeReq = windowArray[w, h].Job.timeReq;
windowArray[w, h].TimeElapsed = windowArray[w, h].Job.timePassed;
windowArray[w, h].FleetReq = windowArray[w, h].Job.indices.Count;
windowArray[w, h].FleetInitCount = windowArray[w, h].Job.initIndicesCount;
windowArray[w, h].ActiveJob = windowArray[w, h].Job.pursue;
}
count++;
}
}
}
}
}
}
//window basec class
public class BuildWindow
{
BuildCity mapGrid;
public bool On { get; set; }
public Vector3Int Position { get; set; }
public Vector3 ScreenPosition { get; set; }
public Rect Rectangle { get; set; }
public string Name { get; set; }
public int ID { get; set; }
public Job Job{ get; set; }
public int FleetReq { get; set; }
public int FleetInitCount { get; set; }
public float Payout { get; set; }
public float TimeReq { get; set; }
public float TimeElapsed { get; set; }
public bool ActiveJob { get; set; }
public BuildWindow()
{
}
}