basic developer console
This commit is contained in:
parent
4375350901
commit
04d7b42e60
4 changed files with 184 additions and 8 deletions
|
|
@ -1,9 +1,13 @@
|
|||
// Assets/_Project/Scripts/Dev/DebugConsole.cs
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.UIElements;
|
||||
using TD.UI;
|
||||
using TD.Gameplay.Draft;
|
||||
|
||||
namespace TD.Dev
|
||||
{
|
||||
|
|
@ -17,9 +21,9 @@ namespace TD.Dev
|
|||
/// a build entirely by not including that GameObject, the same way
|
||||
/// <see cref="DevWaveControls"/> is kept separate from shipping systems.
|
||||
///
|
||||
/// This first pass only wires up open/close/focus/text-capture. Submit
|
||||
/// currently just logs the typed text — command parsing/dispatch (e.g.
|
||||
/// unlocking spells) comes next.
|
||||
/// Supports one command so far: "get <type> <name>" grants a
|
||||
/// <see cref="TD.Gameplay.Draft.DraftOption"/> to the local player via
|
||||
/// <see cref="DebugCommandRelay"/>.
|
||||
/// </remarks>
|
||||
[RequireComponent(typeof(UIDocument))]
|
||||
public class DebugConsole : MonoBehaviour
|
||||
|
|
@ -204,15 +208,93 @@ namespace TD.Dev
|
|||
HUDController.SuppressChatOpenUntilFrame = Time.frameCount + 1;
|
||||
}
|
||||
|
||||
// Placeholder — command parsing/dispatch (unlocking spells, granting gold,
|
||||
// etc.) comes next. For now this just proves the text made it through.
|
||||
private void SubmitCommand()
|
||||
{
|
||||
string text = commandInput?.value ?? string.Empty;
|
||||
if (!string.IsNullOrWhiteSpace(text))
|
||||
Debug.Log($"[DebugConsole] Command entered: \"{text}\"");
|
||||
|
||||
CloseConsole();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(text)) return;
|
||||
ExecuteCommand(text.Trim());
|
||||
}
|
||||
|
||||
// Maps the type token in "get <type> <name>" to the DraftOption subclass it
|
||||
// should match against. Add an entry here whenever a new DraftOption subclass
|
||||
// should be gettable from the console.
|
||||
private static readonly Dictionary<string, Type> DraftTypeAliases =
|
||||
new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
{ "spell", typeof(BuilderSpellDraftOption) },
|
||||
{ "tower", typeof(NewTowerDraftOption) },
|
||||
{ "buff", typeof(BuilderEffectDraftOption) },
|
||||
{ "effect", typeof(BuilderEffectDraftOption) },
|
||||
};
|
||||
|
||||
private void ExecuteCommand(string text)
|
||||
{
|
||||
string[] tokens = text.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (tokens.Length < 3 || !string.Equals(tokens[0], "get", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Debug.Log($"[DebugConsole] Unrecognized command: \"{text}\". Try: get <type> <name>");
|
||||
return;
|
||||
}
|
||||
|
||||
string nameToken = string.Join(" ", tokens, 2, tokens.Length - 2);
|
||||
ExecuteGet(tokens[1], nameToken);
|
||||
}
|
||||
|
||||
// "get <type> <name>": grants any DraftOption of the given type whose DisplayName
|
||||
// matches <name>, via the local player's DebugCommandRelay. Reuses DraftOption's
|
||||
// existing type-agnostic ServerApply dispatch instead of hardcoding a grant path
|
||||
// per option type.
|
||||
private void ExecuteGet(string typeToken, string nameToken)
|
||||
{
|
||||
if (!DraftTypeAliases.TryGetValue(typeToken, out var optionType))
|
||||
{
|
||||
Debug.LogWarning($"[DebugConsole] Unknown draft option type \"{typeToken}\". " +
|
||||
$"Valid types: {string.Join(", ", DraftTypeAliases.Keys)}");
|
||||
return;
|
||||
}
|
||||
|
||||
var pool = DraftPool.Instance;
|
||||
if (pool == null)
|
||||
{
|
||||
Debug.LogWarning("[DebugConsole] No DraftPool in this scene.");
|
||||
return;
|
||||
}
|
||||
|
||||
string normalizedTarget = Normalize(nameToken);
|
||||
for (int i = 0; i < pool.Count; i++)
|
||||
{
|
||||
var option = pool.Get(i);
|
||||
if (option == null || !optionType.IsInstanceOfType(option)) continue;
|
||||
if (Normalize(option.DisplayName) != normalizedTarget) continue;
|
||||
|
||||
var relay = DebugCommandRelay.Local;
|
||||
if (relay == null)
|
||||
{
|
||||
Debug.LogWarning("[DebugConsole] No local DebugCommandRelay found — " +
|
||||
"is it on the Player prefab?");
|
||||
return;
|
||||
}
|
||||
|
||||
relay.DebugGrantDraftOptionRpc(i);
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.LogWarning($"[DebugConsole] No {typeToken} option named \"{nameToken}\" found.");
|
||||
}
|
||||
|
||||
// DisplayName is player-facing draft card text (its spacing/casing is chosen for
|
||||
// presentation, not for typing), so command matching strips everything but letters
|
||||
// and digits before comparing — "Slow Area", "slow_area", and "SlowArea" all match
|
||||
// the same option.
|
||||
private static string Normalize(string s)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s)) return string.Empty;
|
||||
var sb = new StringBuilder(s.Length);
|
||||
foreach (char c in s)
|
||||
if (char.IsLetterOrDigit(c)) sb.Append(char.ToLowerInvariant(c));
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue