Skip to content

Commit 0616253

Browse files
committed
feat(core): add tutorial 32 code
1 parent 38b4e3a commit 0616253

9 files changed

Lines changed: 334 additions & 24 deletions

File tree

Assets/Resources/ScriptableObjects/Parameters/03_Keyboard Mapping.asset

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ MonoBehaviour:
2323
- displayName: Build a tower
2424
key: t
2525
inputEvent: Build:tower
26+
- displayName: Show the debug console
27+
key: '`'
28+
inputEvent: ShowDebugConsole

Assets/Scenes/SampleScene.unity

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7714,6 +7714,7 @@ GameObject:
77147714
- component: {fileID: 1464451463}
77157715
- component: {fileID: 1464451464}
77167716
- component: {fileID: 1464451468}
7717+
- component: {fileID: 1464451469}
77177718
- component: {fileID: 1464451466}
77187719
- component: {fileID: 1464451467}
77197720
m_Layer: 0
@@ -8074,6 +8075,18 @@ MonoBehaviour:
80748075
audioSource: {fileID: 1464451467}
80758076
soundParameters: {fileID: 11400000, guid: 5ad80013b15374ddf9beb1caf495c3d1, type: 2}
80768077
masterMixer: {fileID: 24100000, guid: f6952c5e2db5c4627acbfed417008fa3, type: 2}
8078+
--- !u!114 &1464451469
8079+
MonoBehaviour:
8080+
m_ObjectHideFlags: 0
8081+
m_CorrespondingSourceObject: {fileID: 0}
8082+
m_PrefabInstance: {fileID: 0}
8083+
m_PrefabAsset: {fileID: 0}
8084+
m_GameObject: {fileID: 1464451458}
8085+
m_Enabled: 1
8086+
m_EditorHideFlags: 0
8087+
m_Script: {fileID: 11500000, guid: ca525d3ed639b483daea446a3647e1f3, type: 3}
8088+
m_Name:
8089+
m_EditorClassIdentifier:
80778090
--- !u!1 &1466245624
80788091
GameObject:
80798092
m_ObjectHideFlags: 0

Assets/Scripts/DebugConsole.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Adapted from this video tutorial from Game Dev Guide:
2+
* https://www.youtube.com/watch?v=VzOEM-4A2OM */
3+
using System;
4+
using System.Collections.Generic;
5+
6+
public class DebugCommandBase
7+
{
8+
public static Dictionary<string, DebugCommandBase> DebugCommands;
9+
10+
private string _id;
11+
private string _description;
12+
private string _format;
13+
14+
public DebugCommandBase(string id, string description, string format)
15+
{
16+
_id = id;
17+
_description = description;
18+
_format = format;
19+
20+
if (DebugCommands == null)
21+
DebugCommands = new Dictionary<string, DebugCommandBase>();
22+
string mainKeyword = format.Split(' ')[0];
23+
DebugCommands[mainKeyword] = this;
24+
}
25+
26+
public string Id => _id;
27+
public string Description => _description;
28+
public string Format => _format;
29+
30+
}
31+
32+
public class DebugCommand : DebugCommandBase
33+
{
34+
private Action _action;
35+
36+
public DebugCommand(string id, string description, string format, Action action)
37+
: base(id, description, format)
38+
{
39+
_action = action;
40+
}
41+
42+
public void Invoke()
43+
{
44+
_action.Invoke();
45+
}
46+
}
47+
48+
public class DebugCommand<T> : DebugCommandBase
49+
{
50+
private Action<T> _action;
51+
52+
public DebugCommand(string id, string description, string format, Action<T> action)
53+
: base(id, description, format)
54+
{
55+
_action = action;
56+
}
57+
58+
public void Invoke(T value)
59+
{
60+
_action.Invoke(value);
61+
}
62+
}

Assets/Scripts/DebugConsole/DebugCommand.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using UnityEngine;
5+
6+
public class DebugConsole : MonoBehaviour
7+
{
8+
enum DisplayType
9+
{
10+
None,
11+
Help,
12+
Autocomplete,
13+
Output
14+
}
15+
16+
private static GUIStyle _logStyle;
17+
18+
private bool _showConsole = false;
19+
private string _consoleInput;
20+
21+
private DisplayType _displayType;
22+
23+
private List<string> _commandOutput;
24+
25+
private void Awake()
26+
{
27+
new DebugCommand("?", "Lists all available debug commands.", "?", () =>
28+
{
29+
_displayType = DisplayType.Help;
30+
});
31+
new DebugCommand("toggle_fov", "Toggles the FOV parameter on/off.", "toggle_fov", () =>
32+
{
33+
bool fov = !GameManager.instance.gameGlobalParameters.enableFOV;
34+
GameManager.instance.gameGlobalParameters.enableFOV = fov;
35+
EventManager.TriggerEvent("UpdateGameParameter:enableFOV", fov);
36+
});
37+
new DebugCommand<int>("add_gold", "Adds a given amount of gold to the current player.", "add_gold <amount>", (x) =>
38+
{
39+
Globals.GAME_RESOURCES[GameManager.instance.gamePlayersParameters.myPlayerId][InGameResource.Gold].AddAmount(x);
40+
EventManager.TriggerEvent("UpdateResourceTexts");
41+
});
42+
new DebugCommand<int>("add_wood", "Adds a given amount of wood to the current player.", "add_wood <amount>", (x) =>
43+
{
44+
Globals.GAME_RESOURCES[GameManager.instance.gamePlayersParameters.myPlayerId][InGameResource.Wood].AddAmount(x);
45+
EventManager.TriggerEvent("UpdateResourceTexts");
46+
});
47+
new DebugCommand<int>("add_stone", "Adds a given amount of stone to the current player.", "add_stone <amount>", (x) =>
48+
{
49+
Globals.GAME_RESOURCES[GameManager.instance.gamePlayersParameters.myPlayerId][InGameResource.Stone].AddAmount(x);
50+
EventManager.TriggerEvent("UpdateResourceTexts");
51+
});
52+
new DebugCommand("list_players", "Lists all current players (with their IDs).", "list_players", () =>
53+
{
54+
if (_commandOutput == null)
55+
_commandOutput = new List<string>();
56+
else
57+
_commandOutput.Clear();
58+
int i = 0;
59+
foreach (PlayerData p in GameManager.instance.gamePlayersParameters.players)
60+
_commandOutput.Add($"Player #{i++} - {p.name}");
61+
_displayType = DisplayType.Output;
62+
});
63+
new DebugCommand<int>("set_player_id", "Sets the current player (by ID).", "set_player_id <id>", (x) =>
64+
{
65+
GameManager.instance.gamePlayersParameters.myPlayerId = x;
66+
EventManager.TriggerEvent("SetPlayer", x);
67+
});
68+
69+
_displayType = DisplayType.None;
70+
}
71+
72+
private void OnEnable()
73+
{
74+
EventManager.AddListener("<Input>ShowDebugConsole", _OnShowDebugConsole);
75+
}
76+
77+
private void OnDisable()
78+
{
79+
EventManager.RemoveListener("<Input>ShowDebugConsole", _OnShowDebugConsole);
80+
}
81+
82+
private void _OnShowDebugConsole()
83+
{
84+
_showConsole = true;
85+
EventManager.TriggerEvent("PauseGame");
86+
}
87+
88+
private void OnGUI()
89+
{
90+
if (_logStyle == null)
91+
{
92+
_logStyle = new GUIStyle(GUI.skin.label);
93+
_logStyle.fontSize = 12;
94+
}
95+
96+
if (_showConsole)
97+
{
98+
// add fake boxes in the background to increase the opacity
99+
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
100+
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "");
101+
102+
// show main input field
103+
string newInput = GUI.TextField(new Rect(0, 0, Screen.width, 24), _consoleInput);
104+
105+
// show log area
106+
float y = 24;
107+
GUI.Box(new Rect(0, y, Screen.width, Screen.height - 24), "");
108+
if (_displayType == DisplayType.Help)
109+
_ShowHelp(y);
110+
else if (_displayType == DisplayType.Autocomplete)
111+
_ShowAutocomplete(y, newInput);
112+
else if (_displayType == DisplayType.Output)
113+
_ShowOutput(y);
114+
115+
// reset display state to "none" if input changes
116+
if (_displayType != DisplayType.None && _consoleInput.Length != newInput.Length)
117+
_displayType = DisplayType.None;
118+
119+
// update input variable
120+
_consoleInput = newInput;
121+
122+
// check for special keys
123+
Event e = Event.current;
124+
if (e.isKey)
125+
{
126+
if (e.keyCode == KeyCode.Tab)
127+
_displayType = DisplayType.Autocomplete;
128+
else if (e.keyCode == KeyCode.Return && _consoleInput.Length > 0)
129+
_OnReturn();
130+
else if (e.keyCode == KeyCode.Escape)
131+
{
132+
_showConsole = false;
133+
EventManager.TriggerEvent("ResumeGame");
134+
}
135+
}
136+
}
137+
}
138+
139+
private void _ShowHelp(float y)
140+
{
141+
foreach (DebugCommandBase command in DebugCommandBase.DebugCommands.Values)
142+
{
143+
GUI.Label(
144+
new Rect(2, y, Screen.width, 20),
145+
$"{command.Format} - {command.Description}",
146+
_logStyle
147+
);
148+
y += 16;
149+
}
150+
}
151+
152+
private void _ShowAutocomplete(float y, string newInput)
153+
{
154+
IEnumerable<string> autocompleteCommands =
155+
DebugCommandBase.DebugCommands.Keys
156+
.Where(k => k.StartsWith(newInput.ToLower()));
157+
foreach (string k in autocompleteCommands)
158+
{
159+
DebugCommandBase c = DebugCommandBase.DebugCommands[k];
160+
GUI.Label(
161+
new Rect(2, y, Screen.width, 20),
162+
$"{c.Format} - {c.Description}",
163+
_logStyle
164+
);
165+
y += 16;
166+
}
167+
}
168+
169+
private void _ShowOutput(float y)
170+
{
171+
foreach (string line in _commandOutput)
172+
{
173+
GUI.Label(new Rect(2, y, Screen.width, 20), line, _logStyle);
174+
y += 16;
175+
}
176+
}
177+
178+
private void _OnReturn()
179+
{
180+
_HandleConsoleInput();
181+
_consoleInput = "";
182+
}
183+
184+
private void _HandleConsoleInput()
185+
{
186+
// parse input
187+
string[] inputParts = _consoleInput.Split(' ');
188+
string mainKeyword = inputParts[0];
189+
// check against available commands
190+
DebugCommandBase command;
191+
if (DebugCommandBase.DebugCommands.TryGetValue(mainKeyword.ToLower(), out command))
192+
{
193+
// try to invoke command if it exists
194+
if (command is DebugCommand dc)
195+
dc.Invoke();
196+
else
197+
{
198+
if (inputParts.Length < 2)
199+
{
200+
Debug.LogError("Missing parameter!");
201+
return;
202+
}
203+
if (command is DebugCommand<int> dcInt)
204+
{
205+
int i;
206+
if (int.TryParse(inputParts[1], out i))
207+
dcInt.Invoke(i);
208+
else
209+
{
210+
Debug.LogError($"'{command.Id}' requires an int parameter!");
211+
return;
212+
}
213+
}
214+
}
215+
}
216+
}
217+
}

Assets/Scripts/DebugConsole/DebugConsole.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Managers/GameManager.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using UnityEngine;
54
using UnityEngine.AI;
65

@@ -72,27 +71,6 @@ private void Update()
7271
}
7372
}
7473

75-
#if UNITY_EDITOR
76-
private void OnGUI()
77-
{
78-
GUILayout.BeginArea(new Rect(0f, 40f, 100f, 100f));
79-
80-
int newMyPlayerId = GUILayout.SelectionGrid(
81-
gamePlayersParameters.myPlayerId,
82-
gamePlayersParameters.players.Select((p, i) => i.ToString()).ToArray(),
83-
gamePlayersParameters.players.Length
84-
);
85-
86-
GUILayout.EndArea();
87-
88-
if (newMyPlayerId != gamePlayersParameters.myPlayerId)
89-
{
90-
gamePlayersParameters.myPlayerId = newMyPlayerId;
91-
EventManager.TriggerEvent("SetPlayer", newMyPlayerId);
92-
}
93-
}
94-
#endif
95-
9674
private void OnEnable()
9775
{
9876
EventManager.AddListener("PauseGame", _OnPauseGame);

0 commit comments

Comments
 (0)