using System.Diagnostics;
namespace XFEExtension.NetCore.XFEConsole.Terminal;
///
/// 终端小游戏运行选项。
///
public sealed class XFETerminalGameOptions
{
/// 固定画布宽度;为空时使用窗口宽度。
public int? Width { get; set; }
/// 固定画布高度;为空时使用窗口高度。
public int? Height { get; set; }
/// 每秒目标帧数,默认为 30。
public int FramesPerSecond { get; set; } = 30;
/// 是否使用备用屏幕。
public bool UseAlternateScreen { get; set; } = true;
/// 是否捕获鼠标。
public bool CaptureMouse { get; set; } = true;
/// 当画布尺寸未固定时,是否随窗口改变尺寸。
public bool AutoResize { get; set; } = true;
/// 自动退出键;为空时由游戏代码决定。
public ConsoleKey? ExitKey { get; set; } = ConsoleKey.Escape;
}
///
/// 每一帧传给游戏代码的上下文。
///
public sealed class XFETerminalGameContext
{
private IReadOnlyList inputEvents = [];
internal XFETerminalGameContext(XFETerminalCanvas canvas)
{
Canvas = canvas;
}
/// 当前字符画布。
public XFETerminalCanvas Canvas { get; }
/// 当前帧收到的输入事件。
public IReadOnlyList InputEvents => inputEvents;
/// 上一帧到当前帧的时间。
public TimeSpan DeltaTime { get; internal set; }
/// 游戏启动后的总时间。
public TimeSpan Elapsed { get; internal set; }
/// 当前帧编号,从 0 开始。
public long FrameNumber { get; internal set; }
/// 是否已请求退出。
public bool IsExitRequested { get; private set; }
///
/// 判断当前帧是否收到指定按键的按下事件。
///
public bool IsKeyPressed(ConsoleKey key) => inputEvents
.OfType()
.Any(input => input.IsKeyDown && input.Key == key);
/// 请求在当前帧结束后退出游戏循环。
public void RequestExit() => IsExitRequested = true;
internal void SetInputEvents(IReadOnlyList value) => inputEvents = value;
}
///
/// 终端游戏每帧回调。
///
/// 游戏上下文。
/// 取消标记。
public delegate ValueTask XFETerminalGameFrame(
XFETerminalGameContext context,
CancellationToken cancellationToken);
///
/// 固定帧率终端小游戏/TUI 循环。
///
public static class XFETerminalGame
{
///
/// 运行终端游戏循环,并在退出或异常时自动恢复终端状态。
///
/// 每帧更新和绘制回调。
/// 运行选项。
/// 取消标记。
public static async Task RunAsync(
XFETerminalGameFrame frame,
XFETerminalGameOptions? options = null,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(frame);
options ??= new XFETerminalGameOptions();
if (options.FramesPerSecond is < 1 or > 240)
throw new ArgumentOutOfRangeException(nameof(options.FramesPerSecond), "帧率必须在 1 到 240 之间。");
if (!XFETerminal.Capabilities.IsInteractive)
throw new InvalidOperationException("终端游戏需要未重定向的交互输入和输出。");
var (windowWidth, windowHeight) = GetConsoleSize();
var canvas = new XFETerminalCanvas(options.Width ?? windowWidth, options.Height ?? windowHeight);
var context = new XFETerminalGameContext(canvas);
using var session = XFETerminal.BeginSession(new XFETerminalSessionOptions
{
UseAlternateScreen = options.UseAlternateScreen,
HideCursor = true,
ClearScreen = true,
DisableAutoWrap = true
});
using var input = new XFETerminalInputReader(new XFETerminalInputOptions
{
CaptureMouse = options.CaptureMouse,
CaptureWindowResize = options.AutoResize,
DisableLineInputAndEcho = true
});
var stopwatch = Stopwatch.StartNew();
var previousFrameTime = TimeSpan.Zero;
var targetFrameTime = TimeSpan.FromSeconds(1d / options.FramesPerSecond);
while (!context.IsExitRequested)
{
cancellationToken.ThrowIfCancellationRequested();
var frameStarted = stopwatch.Elapsed;
var events = input.ReadAvailable();
context.SetInputEvents(events);
context.Elapsed = frameStarted;
context.DeltaTime = frameStarted - previousFrameTime;
previousFrameTime = frameStarted;
if (options.AutoResize && (options.Width is null || options.Height is null))
{
var resize = events.OfType().LastOrDefault();
if (resize is not null)
canvas.Resize(options.Width ?? Math.Max(1, resize.Width), options.Height ?? Math.Max(1, resize.Height));
}
if (options.ExitKey is { } exitKey && context.IsKeyPressed(exitKey))
context.RequestExit();
if (!context.IsExitRequested)
{
await frame(context, cancellationToken);
canvas.Present(session.Writer);
context.FrameNumber++;
}
var remaining = targetFrameTime - (stopwatch.Elapsed - frameStarted);
if (remaining > TimeSpan.Zero)
await Task.Delay(remaining, cancellationToken);
}
}
private static (int Width, int Height) GetConsoleSize()
{
try
{
return (Math.Max(1, Console.WindowWidth), Math.Max(1, Console.WindowHeight));
}
catch (IOException)
{
return (80, 25);
}
}
}