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,73 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MrGameEng.Core;
|
||||
|
||||
/// <summary>Phase of a scene transition.</summary>
|
||||
public enum TransitionPhase
|
||||
{
|
||||
/// <summary>The old scene is being covered (coverage grows 0 → 1).</summary>
|
||||
Out,
|
||||
|
||||
/// <summary>The new scene is being revealed (coverage shrinks 1 → 0).</summary>
|
||||
In,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Visual transition between scenes. The scene switch itself happens at full coverage,
|
||||
/// so a slow <c>OnLoad</c> of the next scene is hidden behind the overlay.
|
||||
/// Transitions are stateless and reusable; progress is tracked by <see cref="SceneManager"/>.
|
||||
/// Runs on unscaled time, so it works while gameplay is paused.
|
||||
/// </summary>
|
||||
public abstract class Transition
|
||||
{
|
||||
/// <summary>Seconds the covering phase takes.</summary>
|
||||
public float OutDuration { get; }
|
||||
|
||||
/// <summary>Seconds the revealing phase takes.</summary>
|
||||
public float InDuration { get; }
|
||||
|
||||
/// <summary>Creates a transition with explicit phase durations.</summary>
|
||||
protected Transition(float outDuration, float inDuration)
|
||||
{
|
||||
OutDuration = Math.Max(0f, outDuration);
|
||||
InDuration = Math.Max(0f, inDuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws the overlay. <paramref name="coverage"/> is 0 (scene fully visible) to
|
||||
/// 1 (scene fully covered); <paramref name="phase"/> tells which side of the switch this is.
|
||||
/// </summary>
|
||||
public abstract void Draw(TransitionRenderer renderer, float coverage, TransitionPhase phase);
|
||||
|
||||
/// <summary>Fade through a solid color (black by default). Total duration is split between out and in.</summary>
|
||||
public static Transition Fade(float duration = 0.6f, Color? color = null) =>
|
||||
new FadeTransition(duration / 2f, duration / 2f, color ?? Color.Black);
|
||||
|
||||
/// <summary>A curtain wiping across the screen (black by default). Total duration is split between out and in.</summary>
|
||||
public static Transition Wipe(float duration = 0.6f, Color? color = null) =>
|
||||
new WipeTransition(duration / 2f, duration / 2f, color ?? Color.Black);
|
||||
|
||||
private sealed class FadeTransition(float outDuration, float inDuration, Color color)
|
||||
: Transition(outDuration, inDuration)
|
||||
{
|
||||
public override void Draw(TransitionRenderer renderer, float coverage, TransitionPhase phase) =>
|
||||
renderer.Fill(0f, 0f, 1f, 1f, color, coverage);
|
||||
}
|
||||
|
||||
private sealed class WipeTransition(float outDuration, float inDuration, Color color)
|
||||
: Transition(outDuration, inDuration)
|
||||
{
|
||||
public override void Draw(TransitionRenderer renderer, float coverage, TransitionPhase phase)
|
||||
{
|
||||
// Out: шторка растёт слева направо; In: уезжает дальше вправо.
|
||||
if (phase == TransitionPhase.Out)
|
||||
{
|
||||
renderer.Fill(0f, 0f, coverage, 1f, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer.Fill(1f - coverage, 0f, coverage, 1f, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user