forked from MinaPecheux/UnityTutorials-RTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugConsole.cs
More file actions
279 lines (254 loc) · 9.92 KB
/
Copy pathDebugConsole.cs
File metadata and controls
279 lines (254 loc) · 9.92 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
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DebugConsole : MonoBehaviour
{
enum DisplayType
{
None,
Help,
Autocomplete,
Output
}
private static GUIStyle _logStyle;
private bool _showConsole = false;
private string _consoleInput;
private DisplayType _displayType;
private List<string> _commandOutput;
private void Awake()
{
new DebugCommand("?", "Lists all available debug commands.", "?", () =>
{
_displayType = DisplayType.Help;
});
new DebugCommand("toggle_fov", "Toggles the FOV parameter on/off.", "toggle_fov", () =>
{
bool fov = !GameManager.instance.gameGlobalParameters.enableFOV;
GameManager.instance.gameGlobalParameters.enableFOV = fov;
EventManager.TriggerEvent("UpdateGameParameter:enableFOV", fov);
});
new DebugCommand<int>("add_gold", "Adds a given amount of gold to the current player.", "add_gold <amount>", (x) =>
{
Globals.GAME_RESOURCES[GameManager.instance.gamePlayersParameters.myPlayerId][InGameResource.Gold].AddAmount(x);
EventManager.TriggerEvent("UpdatedResources");
});
new DebugCommand<int>("add_wood", "Adds a given amount of wood to the current player.", "add_wood <amount>", (x) =>
{
Globals.GAME_RESOURCES[GameManager.instance.gamePlayersParameters.myPlayerId][InGameResource.Wood].AddAmount(x);
EventManager.TriggerEvent("UpdatedResources");
});
new DebugCommand<int>("add_stone", "Adds a given amount of stone to the current player.", "add_stone <amount>", (x) =>
{
Globals.GAME_RESOURCES[GameManager.instance.gamePlayersParameters.myPlayerId][InGameResource.Stone].AddAmount(x);
EventManager.TriggerEvent("UpdatedResources");
});
new DebugCommand("list_players", "Lists all current players (with their IDs).", "list_players", () =>
{
if (_commandOutput == null)
_commandOutput = new List<string>();
else
_commandOutput.Clear();
int i = 0;
foreach (PlayerData p in GameManager.instance.gamePlayersParameters.players)
_commandOutput.Add($"Player #{i++} - {p.name}");
_displayType = DisplayType.Output;
});
new DebugCommand<int>("set_player_id", "Sets the current player (by ID).", "set_player_id <id>", (x) =>
{
GameManager.instance.gamePlayersParameters.myPlayerId = x;
EventManager.TriggerEvent("SetPlayer", x);
});
new DebugCommand<string, int>(
"instantiate_characters",
"Instantiates multiple instances of a character unit (by reference code), using a Poisson disc sampling for random positioning.",
"instantiate_characters <code> <amount>", (code, amount) =>
{
CharacterData d = Globals.CHARACTER_DATA[code];
int owner = GameManager.instance.gamePlayersParameters.myPlayerId;
List<Vector3> positions = Utils.SamplePositions(amount, 1.5f, Vector2.one * 15, Utils.MiddleOfScreenPointToWorld());
foreach (Vector3 pos in positions)
{
Character c = new Character(d, owner);
c.ComputeProduction();
c.Transform.GetComponent<UnityEngine.AI.NavMeshAgent>().Warp(pos);
}
});
new DebugCommand<int>("set_unit_formation_type", "Sets the unit formation type (by index).", "set_unit_formation_type <formation_index>", (x) =>
{
Globals.UNIT_FORMATION_TYPE = (UnitFormationType)x;
EventManager.TriggerEvent("UpdatedUnitFormationType");
});
new DebugCommand<int>("set_construction_hp", "Sets the selected unit construction HP.", "set_construction_hp <hp>", (x) =>
{
if (Globals.SELECTED_UNITS.Count == 0) return;
Building b = (Building) Globals.SELECTED_UNITS[0].GetComponent<BuildingManager>().Unit;
if (b == null) return;
b.SetConstructionHP(x);
});
new DebugCommand<string>("unlock_tech", "Unlocks the given technology tree node.", "unlock_tech <code>", (x) =>
{
TechnologyNodeData node;
if (TechnologyNodeData.TECH_TREE_NODES.TryGetValue(x, out node))
node.Unlock();
});
_displayType = DisplayType.None;
}
private void OnEnable()
{
EventManager.AddListener("<Input>ShowDebugConsole", _OnShowDebugConsole);
}
private void OnDisable()
{
EventManager.RemoveListener("<Input>ShowDebugConsole", _OnShowDebugConsole);
}
private void _OnShowDebugConsole()
{
_showConsole = true;
EventManager.TriggerEvent("PausedGame");
}
private void OnGUI()
{
if (_logStyle == null)
{
_logStyle = new GUIStyle(GUI.skin.label);
_logStyle.fontSize = 12;
}
if (_showConsole)
{
// add fake boxes in the background to increase the opacity
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
// show main input field
string newInput = GUI.TextField(new Rect(0, 0, Screen.width, 24), _consoleInput);
// show log area
float y = 24;
GUI.Box(new Rect(0, y, Screen.width, Screen.height - 24), "");
if (_displayType == DisplayType.Help)
_ShowHelp(y);
else if (_displayType == DisplayType.Autocomplete)
_ShowAutocomplete(y, newInput);
else if (_displayType == DisplayType.Output)
_ShowOutput(y);
// reset display state to "none" if input changes
if (_displayType != DisplayType.None && _consoleInput.Length != newInput.Length)
_displayType = DisplayType.None;
// update input variable
_consoleInput = newInput;
// check for special keys
Event e = Event.current;
if (e.isKey)
{
if (e.keyCode == KeyCode.Tab)
_displayType = DisplayType.Autocomplete;
else if (e.keyCode == KeyCode.Return && _consoleInput.Length > 0)
_OnReturn();
else if (e.keyCode == KeyCode.Escape)
{
_showConsole = false;
EventManager.TriggerEvent("ResumedGame");
}
}
}
}
private void _ShowHelp(float y)
{
foreach (DebugCommandBase command in DebugCommandBase.DebugCommands.Values)
{
GUI.Label(
new Rect(2, y, Screen.width, 20),
$"{command.Format} - {command.Description}",
_logStyle
);
y += 16;
}
}
private void _ShowAutocomplete(float y, string newInput)
{
IEnumerable<string> autocompleteCommands =
DebugCommandBase.DebugCommands.Keys
.Where(k => k.StartsWith(newInput.ToLower()));
foreach (string k in autocompleteCommands)
{
DebugCommandBase c = DebugCommandBase.DebugCommands[k];
GUI.Label(
new Rect(2, y, Screen.width, 20),
$"{c.Format} - {c.Description}",
_logStyle
);
y += 16;
}
}
private void _ShowOutput(float y)
{
foreach (string line in _commandOutput)
{
GUI.Label(new Rect(2, y, Screen.width, 20), line, _logStyle);
y += 16;
}
}
private void _OnReturn()
{
_HandleConsoleInput();
_consoleInput = "";
}
private void _HandleConsoleInput()
{
// parse input
string[] inputParts = _consoleInput.Split(' ');
string mainKeyword = inputParts[0];
// check against available commands
DebugCommandBase command;
if (DebugCommandBase.DebugCommands.TryGetValue(mainKeyword.ToLower(), out command))
{
// try to invoke command if it exists
if (command is DebugCommand dc)
dc.Invoke();
else
{
if (inputParts.Length < 2)
{
Debug.LogError("Missing parameter!");
return;
}
if (command is DebugCommand<string> dcString)
{
dcString.Invoke(inputParts[1]);
}
else if (command is DebugCommand<int> dcInt)
{
int i;
if (int.TryParse(inputParts[1], out i))
dcInt.Invoke(i);
else
{
Debug.LogError($"'{command.Id}' requires an int parameter!");
return;
}
}
else if (command is DebugCommand<float> dcFloat)
{
float f;
if (float.TryParse(inputParts[1], out f))
dcFloat.Invoke(f);
else
{
Debug.LogError($"'{command.Id}' requires a float parameter!");
return;
}
}
else if (command is DebugCommand<string, int> dcStringInt)
{
int i;
if (int.TryParse(inputParts[2], out i))
dcStringInt.Invoke(inputParts[1], i);
else
{
Debug.LogError($"'{command.Id}' requires a string and an int parameter!");
return;
}
}
}
}
}
}