Split the platform out of Core: new Host library + HeadlessHost
CI / build-test (push) Successful in 1m24s
CI / build-test (push) Successful in 1m24s
Core now depends only on Friflo.Engine.ECS — no MonoGame, no platform. The windowed MonoGame host moves to the new MrGameEng.Host library: GameHost, GameHostOptions, visual scene transitions (OverlayTransition, Transitions.Fade/Wipe, TransitionRenderer) and the Input feature (namespace MrGameEng.Input is unchanged). Transition timing stays in Core (SceneManager exposes ActiveTransition/TransitionCoverage/ TransitionPhase; the host draws the overlay). EngineContext loses its GraphicsDevice property: hosts publish the device as a service and graphics code reads it via context.GetGraphicsDevice() in Graphics. Core gains HeadlessHost: a fixed-timestep loop without a window or GPU (Tick/RunTicks, Run with wall-clock pacing and lag resync) for dedicated servers, batch simulation and tests. Graphics and Audio now carry their own MonoGame.Framework.DesktopGL reference instead of inheriting it from Core. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
79d406a9f4
commit
2ac074004a
@@ -0,0 +1,117 @@
|
||||
using MrGameEng.Core;
|
||||
using Xunit;
|
||||
|
||||
namespace MrGameEng.Core.Tests;
|
||||
|
||||
public class HeadlessHostTests
|
||||
{
|
||||
private sealed class CountingScene : Scene
|
||||
{
|
||||
public int LoadCount;
|
||||
public int UnloadCount;
|
||||
public int UpdateCount;
|
||||
|
||||
protected override void OnLoad() => LoadCount++;
|
||||
|
||||
protected override void OnUnload() => UnloadCount++;
|
||||
|
||||
public override void Update(GameClock clock)
|
||||
{
|
||||
UpdateCount++;
|
||||
base.Update(clock);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RunTicks_AdvancesClock_ByExactFixedStep()
|
||||
{
|
||||
var scene = new CountingScene();
|
||||
using var host = new HeadlessHost(new HeadlessHostOptions { TicksPerSecond = 10f }, scene);
|
||||
|
||||
host.RunTicks(30);
|
||||
|
||||
Assert.Equal(0.1f, host.FixedDeltaTime, 3);
|
||||
Assert.Equal(30, host.TickCount);
|
||||
Assert.Equal(3.0, host.Context.Clock.TotalTime, 3);
|
||||
Assert.Equal(30, scene.UpdateCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FirstTick_LoadsTheInitialScene()
|
||||
{
|
||||
var scene = new CountingScene();
|
||||
using var host = new HeadlessHost(new HeadlessHostOptions(), scene);
|
||||
|
||||
Assert.Equal(0, scene.LoadCount);
|
||||
|
||||
host.Tick();
|
||||
|
||||
Assert.Equal(1, scene.LoadCount);
|
||||
Assert.Same(scene, host.Context.Scenes.Current);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_UnloadsTheActiveScene()
|
||||
{
|
||||
var scene = new CountingScene();
|
||||
var host = new HeadlessHost(new HeadlessHostOptions(), scene);
|
||||
host.Tick();
|
||||
|
||||
host.Dispose();
|
||||
|
||||
Assert.Equal(1, scene.UnloadCount);
|
||||
Assert.Null(host.Context.Scenes.Current);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Run_StopsWhenCancelled()
|
||||
{
|
||||
using var cancellation = new CancellationTokenSource();
|
||||
var scene = new CancellingScene(cancellation, afterTicks: 5);
|
||||
using var host = new HeadlessHost(new HeadlessHostOptions { Realtime = false }, scene);
|
||||
|
||||
host.Run(cancellation.Token);
|
||||
|
||||
Assert.Equal(5, scene.UpdateCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TimeScale_StillApplies_OnTopOfFixedStep()
|
||||
{
|
||||
var scene = new CountingScene();
|
||||
using var host = new HeadlessHost(new HeadlessHostOptions { TicksPerSecond = 10f }, scene);
|
||||
host.Context.Clock.TimeScale = 3f;
|
||||
|
||||
host.RunTicks(10);
|
||||
|
||||
Assert.Equal(3.0, host.Context.Clock.TotalTime, 3);
|
||||
Assert.Equal(1.0, host.Context.Clock.UnscaledTotalTime, 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NonPositiveTickRate_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
new HeadlessHost(new HeadlessHostOptions { TicksPerSecond = 0f }, new CountingScene())
|
||||
);
|
||||
}
|
||||
|
||||
private sealed class CancellingScene(CancellationTokenSource cancellation, int afterTicks)
|
||||
: Scene
|
||||
{
|
||||
public int UpdateCount;
|
||||
|
||||
protected override void OnLoad() { }
|
||||
|
||||
public override void Update(GameClock clock)
|
||||
{
|
||||
UpdateCount++;
|
||||
if (UpdateCount >= afterTicks)
|
||||
{
|
||||
cancellation.Cancel();
|
||||
}
|
||||
|
||||
base.Update(clock);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MrGameEng.Input;
|
||||
using Xunit;
|
||||
|
||||
namespace MrGameEng.Input.Tests;
|
||||
|
||||
public class ActionMapTests
|
||||
{
|
||||
private enum GameAction
|
||||
{
|
||||
Jump,
|
||||
MoveLeft,
|
||||
MoveRight,
|
||||
}
|
||||
|
||||
private static void Frame(InputManager input, params Keys[] keys) =>
|
||||
input.Apply(new KeyboardState(keys), default, GamePadState.Default);
|
||||
|
||||
[Fact]
|
||||
public void IsDown_TrueWhenAnyBindingIsHeld()
|
||||
{
|
||||
var input = new InputManager();
|
||||
var map = new ActionMap<GameAction>(input)
|
||||
.Bind(GameAction.Jump, Keys.Space)
|
||||
.Bind(GameAction.Jump, Keys.W);
|
||||
|
||||
Frame(input, Keys.W);
|
||||
|
||||
Assert.True(map.IsDown(GameAction.Jump));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsPressed_EdgeTriggered()
|
||||
{
|
||||
var input = new InputManager();
|
||||
var map = new ActionMap<GameAction>(input).Bind(GameAction.Jump, Keys.Space);
|
||||
|
||||
Frame(input, Keys.Space);
|
||||
Assert.True(map.IsPressed(GameAction.Jump));
|
||||
|
||||
Frame(input, Keys.Space);
|
||||
Assert.False(map.IsPressed(GameAction.Jump));
|
||||
Assert.True(map.IsDown(GameAction.Jump));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Unbind_RemovesAllBindings()
|
||||
{
|
||||
var input = new InputManager();
|
||||
var map = new ActionMap<GameAction>(input).Bind(GameAction.Jump, Keys.Space);
|
||||
|
||||
map.Unbind(GameAction.Jump);
|
||||
Frame(input, Keys.Space);
|
||||
|
||||
Assert.False(map.IsDown(GameAction.Jump));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAxis_CombinesTwoActions()
|
||||
{
|
||||
var input = new InputManager();
|
||||
var map = new ActionMap<GameAction>(input)
|
||||
.Bind(GameAction.MoveLeft, Keys.A)
|
||||
.Bind(GameAction.MoveRight, Keys.D);
|
||||
|
||||
Frame(input, Keys.A);
|
||||
Assert.Equal(-1f, map.GetAxis(GameAction.MoveLeft, GameAction.MoveRight));
|
||||
|
||||
Frame(input, Keys.A, Keys.D);
|
||||
Assert.Equal(0f, map.GetAxis(GameAction.MoveLeft, GameAction.MoveRight));
|
||||
|
||||
Frame(input, Keys.D);
|
||||
Assert.Equal(1f, map.GetAxis(GameAction.MoveLeft, GameAction.MoveRight));
|
||||
}
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MrGameEng.Input;
|
||||
using Xunit;
|
||||
|
||||
namespace MrGameEng.Input.Tests;
|
||||
|
||||
public class InputManagerTests
|
||||
{
|
||||
private static MouseState Mouse(
|
||||
int x = 0,
|
||||
int y = 0,
|
||||
int wheel = 0,
|
||||
ButtonState left = ButtonState.Released
|
||||
) =>
|
||||
new(
|
||||
x,
|
||||
y,
|
||||
wheel,
|
||||
left,
|
||||
ButtonState.Released,
|
||||
ButtonState.Released,
|
||||
ButtonState.Released,
|
||||
ButtonState.Released
|
||||
);
|
||||
|
||||
private static void Frame(
|
||||
InputManager input,
|
||||
KeyboardState keyboard = default,
|
||||
MouseState mouse = default
|
||||
) => input.Apply(keyboard, mouse, GamePadState.Default);
|
||||
|
||||
[Fact]
|
||||
public void KeyPressed_OnlyOnTheFrameItGoesDown()
|
||||
{
|
||||
var input = new InputManager();
|
||||
|
||||
Frame(input, new KeyboardState(Keys.Space));
|
||||
Assert.True(input.IsKeyPressed(Keys.Space));
|
||||
Assert.True(input.IsKeyDown(Keys.Space));
|
||||
|
||||
Frame(input, new KeyboardState(Keys.Space));
|
||||
Assert.False(input.IsKeyPressed(Keys.Space));
|
||||
Assert.True(input.IsKeyDown(Keys.Space));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void KeyReleased_OnlyOnTheFrameItGoesUp()
|
||||
{
|
||||
var input = new InputManager();
|
||||
|
||||
Frame(input, new KeyboardState(Keys.A));
|
||||
Frame(input);
|
||||
|
||||
Assert.True(input.IsKeyReleased(Keys.A));
|
||||
Frame(input);
|
||||
Assert.False(input.IsKeyReleased(Keys.A));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Capture_SuppressesInput_KeepsMousePositionAndWheel()
|
||||
{
|
||||
var capture = new MrGameEng.Core.InputCapture();
|
||||
var input = new InputManager(capture);
|
||||
|
||||
Frame(input, new KeyboardState(Keys.W), Mouse(x: 10, y: 20, wheel: 120));
|
||||
Assert.True(input.IsKeyDown(Keys.W));
|
||||
|
||||
capture.Captured = true;
|
||||
input.Update(); // ввод захвачен оверлеем — устройства не опрашиваются
|
||||
|
||||
Assert.False(input.IsKeyDown(Keys.W));
|
||||
Assert.True(input.IsKeyReleased(Keys.W)); // одно корректное событие отпускания
|
||||
Assert.Equal(new Point(10, 20), input.MousePosition);
|
||||
Assert.Equal(0, input.WheelDelta);
|
||||
Assert.Equal(Point.Zero, input.MouseDelta);
|
||||
|
||||
input.Update();
|
||||
Assert.False(input.IsKeyReleased(Keys.W)); // и больше никаких фантомных событий
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseDeltaAndWheelDelta_ComputedBetweenFrames()
|
||||
{
|
||||
var input = new InputManager();
|
||||
|
||||
Frame(input, mouse: Mouse(x: 10, y: 10, wheel: 0));
|
||||
Frame(input, mouse: Mouse(x: 25, y: 5, wheel: 120));
|
||||
|
||||
Assert.Equal(new Point(15, -5), input.MouseDelta);
|
||||
Assert.Equal(120, input.WheelDelta);
|
||||
Assert.Equal(new Point(25, 5), input.MousePosition);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MousePressed_DetectsLeftButtonEdge()
|
||||
{
|
||||
var input = new InputManager();
|
||||
|
||||
Frame(input, mouse: Mouse());
|
||||
Frame(input, mouse: Mouse(left: ButtonState.Pressed));
|
||||
|
||||
Assert.True(input.IsMousePressed(MouseButton.Left));
|
||||
Assert.False(input.IsMousePressed(MouseButton.Right));
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,14 @@ public class SceneTransitionTests
|
||||
protected override void OnUnload() => UnloadCount++;
|
||||
}
|
||||
|
||||
// Тайминг-машина живёт в ядре и не зависит от визуала перехода —
|
||||
// тестируем на голой заглушке с длительностями, как у Fade.
|
||||
private sealed class TimedTransition(float outDuration, float inDuration)
|
||||
: Transition(outDuration, inDuration);
|
||||
|
||||
private static Transition Fade(float duration) =>
|
||||
new TimedTransition(duration / 2f, duration / 2f);
|
||||
|
||||
private static void Tick(EngineContext context, float seconds)
|
||||
{
|
||||
context.Clock.Advance(seconds);
|
||||
@@ -31,7 +39,7 @@ public class SceneTransitionTests
|
||||
Tick(context, 0.016f);
|
||||
|
||||
// Fade(1.0) → фаза закрытия 0.5 c, фаза открытия 0.5 c.
|
||||
context.Scenes.Switch(second, Transition.Fade(1f));
|
||||
context.Scenes.Switch(second, Fade(1f));
|
||||
Tick(context, 0.2f);
|
||||
|
||||
Assert.True(context.Scenes.IsTransitioning);
|
||||
@@ -61,7 +69,7 @@ public class SceneTransitionTests
|
||||
Tick(context, 0.016f);
|
||||
context.Clock.TimeScale = 0f; // игра на паузе
|
||||
|
||||
context.Scenes.Switch(second, Transition.Fade(0.2f));
|
||||
context.Scenes.Switch(second, Fade(0.2f));
|
||||
Tick(context, 0.15f);
|
||||
Tick(context, 0.15f);
|
||||
|
||||
@@ -79,7 +87,7 @@ public class SceneTransitionTests
|
||||
context.Scenes.Switch(first);
|
||||
Tick(context, 0.016f);
|
||||
|
||||
context.Scenes.Switch(second, Transition.Fade(1f));
|
||||
context.Scenes.Switch(second, Fade(1f));
|
||||
Tick(context, 0.1f);
|
||||
context.Scenes.Switch(third); // передумали, пока экран закрывается
|
||||
|
||||
@@ -99,12 +107,12 @@ public class SceneTransitionTests
|
||||
context.Scenes.Switch(first);
|
||||
Tick(context, 0.016f);
|
||||
|
||||
context.Scenes.Switch(second, Transition.Fade(1f)); // 0.5 c закрытие + 0.5 c открытие
|
||||
context.Scenes.Switch(second, Fade(1f)); // 0.5 c закрытие + 0.5 c открытие
|
||||
Tick(context, 0.6f); // закрыто, своп на second, началось открытие
|
||||
Assert.Same(second, context.Scenes.Current);
|
||||
|
||||
Tick(context, 0.25f); // открытие наполовину (coverage ~0.5)
|
||||
context.Scenes.Switch(third, Transition.Fade(1f)); // передумали во время открытия
|
||||
context.Scenes.Switch(third, Fade(1f)); // передумали во время открытия
|
||||
|
||||
Tick(context, 0.05f); // экран снова закрывается — свопа ещё нет
|
||||
Assert.Same(second, context.Scenes.Current);
|
||||
@@ -124,7 +132,7 @@ public class SceneTransitionTests
|
||||
context.Scenes.Switch(first);
|
||||
Tick(context, 0.016f);
|
||||
|
||||
context.Scenes.Switch(second, Transition.Fade(0f));
|
||||
context.Scenes.Switch(second, Fade(0f));
|
||||
Tick(context, 0.016f);
|
||||
Tick(context, 0.016f);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user