110 lines
2.7 KiB
C#
110 lines
2.7 KiB
C#
#nullable enable
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using Godot;
|
|
using SideScrollerGame.Content.Samples;
|
|
using SideScrollerGame.Debug.Commands;
|
|
|
|
namespace SideScrollerGame.Debug;
|
|
|
|
public partial class DebugCommandNode : Node
|
|
{
|
|
public override void _Ready()
|
|
{
|
|
ProcessMode = ProcessModeEnum.Always;
|
|
_ = Service;
|
|
ApplyEngineState();
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (m_PendingFrameStep)
|
|
{
|
|
GetTree().Paused = State.IsPaused;
|
|
m_PendingFrameStep = false;
|
|
return;
|
|
}
|
|
|
|
if (State.FrameStepRequestCount <= m_AppliedFrameStepCount)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_AppliedFrameStepCount = State.FrameStepRequestCount;
|
|
GetTree().Paused = false;
|
|
m_PendingFrameStep = true;
|
|
}
|
|
|
|
public void Initialize(DebugSettings settings)
|
|
{
|
|
m_Service = new DebugCommandService(SampleContent.CreateRegistry(), settings.Seed);
|
|
m_Service.StateChanged += _ => ApplyEngineState();
|
|
GD.Seed((ulong)settings.Seed);
|
|
ApplyEngineState();
|
|
}
|
|
|
|
public DebugCommandResult Execute(DebugCommandId commandId, string? argument = null)
|
|
{
|
|
DebugCommandResult result = Service.Execute(commandId, argument);
|
|
if (!result.Succeeded)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
if (commandId == DebugCommandId.SetSeed && int.TryParse(argument, out int seed))
|
|
{
|
|
GD.Seed((ulong)seed);
|
|
}
|
|
else if (commandId == DebugCommandId.ReloadScene)
|
|
{
|
|
ReloadScene();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public DebugCommandService Service
|
|
{
|
|
get
|
|
{
|
|
if (m_Service is null)
|
|
{
|
|
m_Service = new DebugCommandService(SampleContent.CreateRegistry(), 1);
|
|
m_Service.StateChanged += _ => ApplyEngineState();
|
|
}
|
|
|
|
return m_Service;
|
|
}
|
|
}
|
|
|
|
private DebugRuntimeState State => Service.State;
|
|
|
|
private void ApplyEngineState()
|
|
{
|
|
Engine.TimeScale = State.TimeScale;
|
|
if (!m_PendingFrameStep)
|
|
{
|
|
GetTree().Paused = State.IsPaused;
|
|
}
|
|
}
|
|
|
|
private void ReloadScene()
|
|
{
|
|
if (IsHeadlessSmokeScript())
|
|
{
|
|
return;
|
|
}
|
|
|
|
GetTree().ReloadCurrentScene();
|
|
}
|
|
|
|
private static bool IsHeadlessSmokeScript()
|
|
{
|
|
return DisplayServer.GetName().Equals("headless", StringComparison.OrdinalIgnoreCase) && OS.GetCmdlineUserArgs().Any(argument => argument.Equals("--debug-script=foundation-smoke", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
private DebugCommandService? m_Service;
|
|
private int m_AppliedFrameStepCount;
|
|
private bool m_PendingFrameStep;
|
|
} |