Add scene transitions (fade, wipe) to SceneManager
Switch(scene, transition) covers the old scene, swaps at full coverage (hiding slow OnLoad), then reveals the new one. Built-in Fade and Wipe transitions draw through TransitionRenderer (BasicEffect quad, no SpriteBatch); custom transitions subclass Transition. Runs on unscaled time so it works while gameplay is paused; Switch during a transition replaces the pending target. Sample: Tab now fades into the stress scene and wipes back. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
ff2231a8ab
commit
2b7d4c4fef
@@ -0,0 +1,108 @@
|
||||
using MrGameEng.Core;
|
||||
using Xunit;
|
||||
|
||||
namespace MrGameEng.Core.Tests;
|
||||
|
||||
public class SceneTransitionTests
|
||||
{
|
||||
private sealed class TrackingScene : Scene
|
||||
{
|
||||
public int LoadCount;
|
||||
public int UnloadCount;
|
||||
|
||||
protected override void OnLoad() => LoadCount++;
|
||||
|
||||
protected override void OnUnload() => UnloadCount++;
|
||||
}
|
||||
|
||||
private static void Tick(EngineContext context, float seconds)
|
||||
{
|
||||
context.Clock.Advance(seconds);
|
||||
context.Scenes.Update(context.Clock);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TransitionSwitch_KeepsOldScene_UntilFullyCovered()
|
||||
{
|
||||
var context = new EngineContext();
|
||||
var first = new TrackingScene();
|
||||
var second = new TrackingScene();
|
||||
context.Scenes.Switch(first);
|
||||
Tick(context, 0.016f);
|
||||
|
||||
// Fade(1.0) → фаза закрытия 0.5 c, фаза открытия 0.5 c.
|
||||
context.Scenes.Switch(second, Transition.Fade(1f));
|
||||
Tick(context, 0.2f);
|
||||
|
||||
Assert.True(context.Scenes.IsTransitioning);
|
||||
Assert.Same(first, context.Scenes.Current);
|
||||
Assert.Equal(0, second.LoadCount);
|
||||
|
||||
Tick(context, 0.4f); // суммарно 0.6 > 0.5 — экран закрыт, своп произошёл
|
||||
|
||||
Assert.Same(second, context.Scenes.Current);
|
||||
Assert.Equal(1, first.UnloadCount);
|
||||
Assert.Equal(1, second.LoadCount);
|
||||
Assert.True(context.Scenes.IsTransitioning); // идёт фаза открытия
|
||||
|
||||
Tick(context, 0.6f); // открытие завершено
|
||||
|
||||
Assert.False(context.Scenes.IsTransitioning);
|
||||
Assert.Same(second, context.Scenes.Current);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Transition_UsesUnscaledTime_WorksWhilePaused()
|
||||
{
|
||||
var context = new EngineContext();
|
||||
var first = new TrackingScene();
|
||||
var second = new TrackingScene();
|
||||
context.Scenes.Switch(first);
|
||||
Tick(context, 0.016f);
|
||||
context.Clock.TimeScale = 0f; // игра на паузе
|
||||
|
||||
context.Scenes.Switch(second, Transition.Fade(0.2f));
|
||||
Tick(context, 0.15f);
|
||||
Tick(context, 0.15f);
|
||||
|
||||
Assert.Same(second, context.Scenes.Current);
|
||||
Assert.False(context.Scenes.IsTransitioning);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SwitchDuringTransition_ReplacesPendingTarget()
|
||||
{
|
||||
var context = new EngineContext();
|
||||
var first = new TrackingScene();
|
||||
var second = new TrackingScene();
|
||||
var third = new TrackingScene();
|
||||
context.Scenes.Switch(first);
|
||||
Tick(context, 0.016f);
|
||||
|
||||
context.Scenes.Switch(second, Transition.Fade(1f));
|
||||
Tick(context, 0.1f);
|
||||
context.Scenes.Switch(third); // передумали, пока экран закрывается
|
||||
|
||||
Tick(context, 0.5f);
|
||||
|
||||
Assert.Same(third, context.Scenes.Current);
|
||||
Assert.Equal(0, second.LoadCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ZeroDurationTransition_SwapsOnNextUpdates()
|
||||
{
|
||||
var context = new EngineContext();
|
||||
var first = new TrackingScene();
|
||||
var second = new TrackingScene();
|
||||
context.Scenes.Switch(first);
|
||||
Tick(context, 0.016f);
|
||||
|
||||
context.Scenes.Switch(second, Transition.Fade(0f));
|
||||
Tick(context, 0.016f);
|
||||
Tick(context, 0.016f);
|
||||
|
||||
Assert.Same(second, context.Scenes.Current);
|
||||
Assert.False(context.Scenes.IsTransitioning);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user