Files
zfxaction26_1/godot/scripts/debug/DebugOverlay.cs
2026-04-21 21:16:30 +02:00

77 lines
2.6 KiB
C#

#nullable enable
using System.Globalization;
using Godot;
using SideScrollerGame.Debug.Commands;
namespace SideScrollerGame.Debug;
public partial class DebugOverlay : CanvasLayer
{
public override void _Ready()
{
Layer = 100;
ProcessMode = ProcessModeEnum.Always;
EnsureLabel();
}
public void SetStatus(DebugSettings settings, string loadedSceneId)
{
m_Settings = settings;
m_LoadedSceneId = loadedSceneId;
Label label = EnsureLabel();
label.Text = $"Debug boot: {settings.BootMode}\nSeed: {settings.Seed}\nScene: {loadedSceneId}\nDebug: {OS.IsDebugBuild()}";
}
public void Bind(DebugCommandService service, DebugSettings settings, string loadedSceneId)
{
m_Service = service;
m_Settings = settings;
m_LoadedSceneId = loadedSceneId;
service.StateChanged += _ => Refresh();
Refresh();
}
private void Refresh()
{
Label label = EnsureLabel();
if (m_Service is null || m_Settings is null)
{
return;
}
DebugRuntimeState state = m_Service.State;
Visible = state.OverlayVisible;
label.Text = $"Debug boot: {m_Settings.BootMode}\n" + $"Scene: {m_LoadedSceneId}\n" + $"Seed: {state.Seed}\n" + $"Difficulty: {state.ActiveDifficultyId}\n" + $"Paused: {state.IsPaused}\n" + $"Time scale: {state.TimeScale.ToString(CultureInfo.InvariantCulture)}\n" + $"Marker: {DisplayOrNone(state.CurrentMarkerId)}\n" + $"Spawned: {state.SpawnedActorCount} ({DisplayOrNone(state.LastSpawnedActorId)})\n" + $"Flags: invuln={state.Invulnerable}, ammo={state.InfiniteSpecialAmmo}, nofire={state.NoEnemyFire}\n" + $"Debug draw: collisions={state.ShowCollisionShapes}, bounds={state.ShowGameplayBounds}";
}
private Label EnsureLabel()
{
if (m_StatusLabel is not null)
{
return m_StatusLabel;
}
m_StatusLabel = GetNodeOrNull<Label>("StatusLabel");
if (m_StatusLabel is null)
{
m_StatusLabel = new Label { Name = "StatusLabel" };
AddChild(m_StatusLabel);
}
m_StatusLabel.MouseFilter = Control.MouseFilterEnum.Ignore;
m_StatusLabel.Position = new Vector2(8.0f, 8.0f);
m_StatusLabel.AutowrapMode = TextServer.AutowrapMode.Off;
return m_StatusLabel;
}
private static string DisplayOrNone(string value)
{
return string.IsNullOrWhiteSpace(value) ? "none" : value;
}
private Label? m_StatusLabel;
private DebugCommandService? m_Service;
private DebugSettings? m_Settings;
private string m_LoadedSceneId = "none";
}