Something

This commit is contained in:
MaddoScientisto 2025-01-19 16:25:03 +01:00
commit 7839e5616d
2 changed files with 312 additions and 314 deletions

View file

@ -142,18 +142,16 @@ radius = 17.2627
[sub_resource type="CircleShape2D" id="CircleShape2D_e6woi"] [sub_resource type="CircleShape2D" id="CircleShape2D_e6woi"]
radius = 1.41421 radius = 1.41421
[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("Muzzle", "EquippedWeapon") groups=["Destroyable", "player"]] [node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("Muzzle") groups=["Destroyable", "player"]]
collision_layer = 2 collision_layer = 2
collision_mask = 99 collision_mask = 99
script = ExtResource("1_m27vu") script = ExtResource("1_m27vu")
Speed = 2500 Speed = 2500
CrosshairDistance = 20.0 CrosshairDistance = 20.0
BulletScene = ExtResource("2_ov36d")
SelectorScene = ExtResource("3_8wt6s") SelectorScene = ExtResource("3_8wt6s")
GameOverScene = "res://Scenes/GameOver.tscn" GameOverScene = "res://Scenes/GameOver.tscn"
Muzzle = NodePath("Muzzle") Muzzle = NodePath("Muzzle")
Health = 32.0 Health = 32.0
EquippedWeapon = NodePath("Weapon")
metadata/_edit_group_ = true metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]

View file

@ -1,317 +1,317 @@
using Godot; using Godot;
using System.IO; using System.IO;
using WeavUtils; using WeavUtils;
public partial class DebugGUI : Control public partial class DebugGUI : Control
{ {
// Other scripts may use us right off the bat, so we make sure we initialize first // Other scripts may use us right off the bat, so we make sure we initialize first
public DebugGUI() public DebugGUI()
{ {
ProcessPhysicsPriority = int.MinValue; ProcessPhysicsPriority = int.MinValue;
} }
static DebugGUI Instance; static DebugGUI Instance;
#region Settings #region Settings
public static class Settings public static class Settings
{ {
const string DEBUGGUI_SETTINGS_DIR = "DebugGUI/Settings/"; const string DEBUGGUI_SETTINGS_DIR = "DebugGUI/Settings/";
public static void Init() public static void Init()
{ {
if (!Engine.IsEditorHint()) return; if (!Engine.IsEditorHint()) return;
// Inits defaults or load current if present // Inits defaults or load current if present
Load(); Load();
ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(enableGraphs)}", enableGraphs); ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(enableGraphs)}", enableGraphs);
ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(enableLogs)}", enableLogs); ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(enableLogs)}", enableLogs);
ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(backgroundColor)}", backgroundColor); ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(backgroundColor)}", backgroundColor);
ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(scrubberColor)}", scrubberColor); ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(scrubberColor)}", scrubberColor);
ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(graphWidth)}", graphWidth); ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(graphWidth)}", graphWidth);
ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(graphHeight)}", graphHeight); ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(graphHeight)}", graphHeight);
ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(temporaryLogLifetime)}", temporaryLogLifetime); ProjectSettings.SetSetting($"{DEBUGGUI_SETTINGS_DIR}{nameof(temporaryLogLifetime)}", temporaryLogLifetime);
ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(enableGraphs)}", true); ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(enableGraphs)}", true);
ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(enableLogs)}", true); ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(enableLogs)}", true);
ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(backgroundColor)}", true); ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(backgroundColor)}", true);
ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(scrubberColor)}", true); ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(scrubberColor)}", true);
ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(graphWidth)}", true); ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(graphWidth)}", true);
ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(graphHeight)}", true); ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(graphHeight)}", true);
ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(temporaryLogLifetime)}", true); ProjectSettings.SetAsBasic($"{DEBUGGUI_SETTINGS_DIR}{nameof(temporaryLogLifetime)}", true);
var err = ProjectSettings.Save(); var err = ProjectSettings.Save();
if(err != Error.Ok) if(err != Error.Ok)
{ {
GD.PrintErr(err); GD.PrintErr(err);
} }
} }
public static void Load() public static void Load()
{ {
textFont = ThemeDB.FallbackFont; textFont = ThemeDB.FallbackFont;
enableGraphs = ProjectSettings.GetSetting( enableGraphs = ProjectSettings.GetSetting(
$"{DEBUGGUI_SETTINGS_DIR}{nameof(enableGraphs)}", $"{DEBUGGUI_SETTINGS_DIR}{nameof(enableGraphs)}",
true true
).AsBool(); ).AsBool();
enableLogs = ProjectSettings.GetSetting( enableLogs = ProjectSettings.GetSetting(
$"{DEBUGGUI_SETTINGS_DIR}{nameof(enableLogs)}", $"{DEBUGGUI_SETTINGS_DIR}{nameof(enableLogs)}",
true true
).AsBool(); ).AsBool();
backgroundColor = ProjectSettings.GetSetting( backgroundColor = ProjectSettings.GetSetting(
$"{DEBUGGUI_SETTINGS_DIR}{nameof(backgroundColor)}", $"{DEBUGGUI_SETTINGS_DIR}{nameof(backgroundColor)}",
new Color(0f, 0f, 0f, 0.7f) new Color(0f, 0f, 0f, 0.7f)
).AsColor(); ).AsColor();
scrubberColor = ProjectSettings.GetSetting( scrubberColor = ProjectSettings.GetSetting(
$"{DEBUGGUI_SETTINGS_DIR}{nameof(scrubberColor)}", $"{DEBUGGUI_SETTINGS_DIR}{nameof(scrubberColor)}",
new Color(1f, 1f, 0f, 0.7f) new Color(1f, 1f, 0f, 0.7f)
).AsColor(); ).AsColor();
graphWidth = ProjectSettings.GetSetting( graphWidth = ProjectSettings.GetSetting(
$"{DEBUGGUI_SETTINGS_DIR}{nameof(graphWidth)}", $"{DEBUGGUI_SETTINGS_DIR}{nameof(graphWidth)}",
300 300
).AsInt32(); ).AsInt32();
graphHeight = ProjectSettings.GetSetting( graphHeight = ProjectSettings.GetSetting(
$"{DEBUGGUI_SETTINGS_DIR}{nameof(graphHeight)}", $"{DEBUGGUI_SETTINGS_DIR}{nameof(graphHeight)}",
100 100
).AsInt32(); ).AsInt32();
temporaryLogLifetime = ProjectSettings.GetSetting( temporaryLogLifetime = ProjectSettings.GetSetting(
$"{DEBUGGUI_SETTINGS_DIR}{nameof(temporaryLogLifetime)}", $"{DEBUGGUI_SETTINGS_DIR}{nameof(temporaryLogLifetime)}",
5 5
).AsDouble(); ).AsDouble();
} }
public static bool enableGraphs; public static bool enableGraphs;
public static bool enableLogs; public static bool enableLogs;
public static Color backgroundColor; public static Color backgroundColor;
public static Color scrubberColor; public static Color scrubberColor;
public static int graphWidth; public static int graphWidth;
public static int graphHeight; public static int graphHeight;
public static double temporaryLogLifetime; public static double temporaryLogLifetime;
public static Font textFont; public static Font textFont;
} }
#endregion #endregion
#region Graph #region Graph
/// <summary> /// <summary>
/// Set the properties of a graph. /// Set the properties of a graph.
/// </summary> /// </summary>
/// <param name="key">The graph's key</param> /// <param name="key">The graph's key</param>
/// <param name="label">The graph's label</param> /// <param name="label">The graph's label</param>
/// <param name="min">Value at the bottom of the graph box</param> /// <param name="min">Value at the bottom of the graph box</param>
/// <param name="max">Value at the top of the graph box</param> /// <param name="max">Value at the top of the graph box</param>
/// <param name="group">The graph's ordinal position on screen</param> /// <param name="group">The graph's ordinal position on screen</param>
/// <param name="color">The graph's color</param> /// <param name="color">The graph's color</param>
public static void SetGraphProperties(object key, string label, float min, float max, int group, Color color, bool autoScale) public static void SetGraphProperties(object key, string label, float min, float max, int group, Color color, bool autoScale)
{ {
if (Settings.enableGraphs) if (Settings.enableGraphs)
Instance?.graphWindow.SetGraphProperties(key, label, min, max, group, color, autoScale); Instance?.graphWindow.SetGraphProperties(key, label, min, max, group, color, autoScale);
} }
/// <summary> /// <summary>
/// Set the properties of a graph. /// Set the properties of a graph.
/// </summary> /// </summary>
/// <param name="key">The graph's key</param> /// <param name="key">The graph's key</param>
/// <param name="label">The graph's label</param> /// <param name="label">The graph's label</param>
/// <param name="min">Value at the bottom of the graph box</param> /// <param name="min">Value at the bottom of the graph box</param>
/// <param name="max">Value at the top of the graph box</param> /// <param name="max">Value at the top of the graph box</param>
/// <param name="group">The graph's ordinal position on screen</param> /// <param name="group">The graph's ordinal position on screen</param>
/// <param name="color">The graph's color</param> /// <param name="color">The graph's color</param>
public static void SetGraphProperties(GodotObject key, string label, float min, float max, int group, Color color, bool autoScale) public static void SetGraphProperties(GodotObject key, string label, float min, float max, int group, Color color, bool autoScale)
{ {
SetGraphProperties((object)key, label, min, max, group, color, autoScale); SetGraphProperties((object)key, label, min, max, group, color, autoScale);
} }
/// <summary> /// <summary>
/// Add a data point to a graph. /// Add a data point to a graph.
/// </summary> /// </summary>
/// <param name="key">The graph's key</param> /// <param name="key">The graph's key</param>
/// <param name="val">Value to be added</param> /// <param name="val">Value to be added</param>
public static void Graph(object key, float val) public static void Graph(object key, float val)
{ {
if (Settings.enableGraphs) if (Settings.enableGraphs)
Instance?.graphWindow.Graph(key, val); Instance?.graphWindow.Graph(key, val);
} }
/// <summary> /// <summary>
/// Add a data point to a graph. /// Add a data point to a graph.
/// </summary> /// </summary>
/// <param name="key">The graph's key</param> /// <param name="key">The graph's key</param>
/// <param name="val">Value to be added</param> /// <param name="val">Value to be added</param>
public static void Graph(GodotObject key, float val) public static void Graph(GodotObject key, float val)
{ {
Graph((object)key, val); Graph((object)key, val);
} }
/// <summary> /// <summary>
/// Remove an existing graph. /// Remove an existing graph.
/// </summary> /// </summary>
/// <param name="key">The graph's key</param> /// <param name="key">The graph's key</param>
public static void RemoveGraph(object key) public static void RemoveGraph(object key)
{ {
if (Settings.enableGraphs) if (Settings.enableGraphs)
Instance?.graphWindow.RemoveGraph(key); Instance?.graphWindow.RemoveGraph(key);
} }
/// <summary> /// <summary>
/// Remove an existing graph. /// Remove an existing graph.
/// </summary> /// </summary>
/// <param name="key">The graph's key</param> /// <param name="key">The graph's key</param>
public static void RemoveGraph(GodotObject key) public static void RemoveGraph(GodotObject key)
{ {
RemoveGraph((object)key); RemoveGraph((object)key);
} }
/// <summary> /// <summary>
/// Resets a graph's data. /// Resets a graph's data.
/// </summary> /// </summary>
/// <param name="key">The graph's key</param> /// <param name="key">The graph's key</param>
public static void ClearGraph(object key) public static void ClearGraph(object key)
{ {
if (Settings.enableGraphs) if (Settings.enableGraphs)
Instance?.graphWindow.ClearGraph(key); Instance?.graphWindow.ClearGraph(key);
} }
/// <summary> /// <summary>
/// Resets a graph's data. /// Resets a graph's data.
/// </summary> /// </summary>
/// <param name="key">The graph's key</param> /// <param name="key">The graph's key</param>
public static void ClearGraph(GodotObject key) public static void ClearGraph(GodotObject key)
{ {
ClearGraph((object)key); ClearGraph((object)key);
} }
/// <summary> /// <summary>
/// Export graphs to a json file. See path in log. /// Export graphs to a json file. See path in log.
/// </summary> /// </summary>
public static void ExportGraphs() public static void ExportGraphs()
{ {
if (Instance == null || !Settings.enableGraphs) if (Instance == null || !Settings.enableGraphs)
return; return;
string dateTimeStr = Time.GetDatetimeStringFromSystem().Replace(':', '-'); string dateTimeStr = Time.GetDatetimeStringFromSystem().Replace(':', '-');
string filename = $"debuggui_graph_export_{dateTimeStr}.json"; string filename = $"debuggui_graph_export_{dateTimeStr}.json";
using var file = Godot.FileAccess.Open( using var file = Godot.FileAccess.Open(
"user://" + filename, "user://" + filename,
Godot.FileAccess.ModeFlags.Write Godot.FileAccess.ModeFlags.Write
); );
if (file == null) if (file == null)
{ {
GD.Print("DebugGUI graph export failed: " + Godot.FileAccess.GetOpenError()); GD.Print("DebugGUI graph export failed: " + Godot.FileAccess.GetOpenError());
} }
else else
{ {
file.StoreString(Instance.graphWindow.ToJson()); file.StoreString(Instance.graphWindow.ToJson());
GD.Print($"Wrote graph data to {Path.Combine(OS.GetUserDataDir(), filename)}"); GD.Print($"Wrote graph data to {Path.Combine(OS.GetUserDataDir(), filename)}");
} }
} }
#endregion #endregion
#region Log #region Log
/// <summary> /// <summary>
/// Create or update an existing message with the same key. /// Create or update an existing message with the same key.
/// </summary> /// </summary>
public static void LogPersistent(object key, string message) public static void LogPersistent(object key, string message)
{ {
if (Settings.enableLogs) if (Settings.enableLogs)
Instance?.logWindow.LogPersistent(key, message); Instance?.logWindow.LogPersistent(key, message);
} }
/// <summary> /// <summary>
/// Create or update an existing message with the same key. /// Create or update an existing message with the same key.
/// </summary> /// </summary>
public static void LogPersistent(GodotObject key, string message) public static void LogPersistent(GodotObject key, string message)
{ {
LogPersistent((object)key, message); LogPersistent((object)key, message);
} }
/// <summary> /// <summary>
/// Remove an existing persistent message. /// Remove an existing persistent message.
/// </summary> /// </summary>
public static void RemovePersistent(object key) public static void RemovePersistent(object key)
{ {
if (Settings.enableLogs) if (Settings.enableLogs)
Instance?.logWindow.RemovePersistent(key); Instance?.logWindow.RemovePersistent(key);
} }
/// <summary> /// <summary>
/// Remove an existing persistent message. /// Remove an existing persistent message.
/// </summary> /// </summary>
public static void RemovePersistent(GodotObject key) public static void RemovePersistent(GodotObject key)
{ {
RemovePersistent((object)key); RemovePersistent((object)key);
} }
/// <summary> /// <summary>
/// Clears all persistent logs. /// Clears all persistent logs.
/// </summary> /// </summary>
public static void ClearPersistent() public static void ClearPersistent()
{ {
if (Settings.enableLogs) if (Settings.enableLogs)
Instance?.logWindow.ClearPersistent(); Instance?.logWindow.ClearPersistent();
} }
/// <summary> /// <summary>
/// Print a temporary message. /// Print a temporary message.
/// </summary> /// </summary>
public static void Log(object message) public static void Log(object message)
{ {
Log(message.ToString()); Log(message.ToString());
} }
/// <summary> /// <summary>
/// Print a temporary message. /// Print a temporary message.
/// </summary> /// </summary>
public static void Log(string message) public static void Log(string message)
{ {
if (Settings.enableLogs) if (Settings.enableLogs)
Instance?.logWindow.Log(message); Instance?.logWindow.Log(message);
} }
#endregion #endregion
/// <summary> /// <summary>
/// Re-scans for DebugGUI attribute holders (i.e. [DebugGUIGraph] and [DebugGUIPrint]) /// Re-scans for DebugGUI attribute holders (i.e. [DebugGUIGraph] and [DebugGUIPrint])
/// </summary> /// </summary>
public static void ForceReinitializeAttributes() public static void ForceReinitializeAttributes()
{ {
if (Instance == null) return; if (Instance == null) return;
Instance.graphWindow.ReinitializeAttributes(); Instance.graphWindow.ReinitializeAttributes();
Instance.logWindow.ReinitializeAttributes(); Instance.logWindow.ReinitializeAttributes();
} }
GraphWindow graphWindow; GraphWindow graphWindow;
LogWindow logWindow; LogWindow logWindow;
public override void _Ready() public override void _Ready()
{ {
Instance = this; Instance = this;
Settings.Load(); Settings.Load();
if (Settings.enableGraphs) if (Settings.enableGraphs)
{ {
CanvasLayer canvasLayer = new(); CanvasLayer canvasLayer = new();
canvasLayer.AddChild(graphWindow = new()); canvasLayer.AddChild(graphWindow = new());
AddChild(canvasLayer); AddChild(canvasLayer);
} }
if (Settings.enableGraphs) if (Settings.enableGraphs)
{ {
CanvasLayer canvasLayer = new(); CanvasLayer canvasLayer = new();
canvasLayer.AddChild(logWindow = new()); canvasLayer.AddChild(logWindow = new());
AddChild(canvasLayer); AddChild(canvasLayer);
} }
} }
} }