Skip to content

Commit

Permalink
fading out now support buffer frames
Browse files Browse the repository at this point in the history
  • Loading branch information
saint11 committed Nov 28, 2023
1 parent 5ad3124 commit 5a05bb9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Murder/Components/Effects/FadeScreenComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ public enum FadeType
{
In,
Out,
OutBuffer,
Flash
}

[DoNotPersistOnSave]
public readonly struct FadeScreenComponent : IComponent
{
public readonly float StartedTime;
public readonly float StartedTime { get; init; }

public readonly float Duration;

Expand All @@ -26,17 +27,20 @@ public enum FadeType

public readonly float Sorting = 0;

public readonly int BufferFrames { get; init; } = 0;

/// <summary>
/// Fades the screen using the FadeScreenSystem. Duration will be a minimum of 0.1
/// </summary>
public FadeScreenComponent(FadeType fade, float startedTime, float duration, Color color, string customTexture = "", float sorting = 0)
public FadeScreenComponent(FadeType fade, float startedTime, float duration, Color color, string customTexture = "", float sorting = 0, int bufferFrames = 0)
{
StartedTime = startedTime;
Duration = MathF.Max(duration - 0.1f, 0.1f);
Fade = fade;
Color = color;
CustomTexture = customTexture;
Sorting = sorting;
BufferFrames = bufferFrames;
}
}
}
14 changes: 12 additions & 2 deletions src/Murder/Services/EffectsServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,25 @@ public static void FadeIn(World world, float time, Color color, float sorting =
/// <summary>
/// Add an entity which will apply a "fade-out" effect. Clearing the screen.
/// </summary>
public static void FadeOut(World world, float time, Color color, float delay = 0)
public static void FadeOut(World world, float time, Color color, float delay = 0, int bufferDrawFrames = 0)
{
foreach (var old in world.GetEntitiesWith(typeof(FadeScreenComponent)))
{
old.Destroy();
}

var e = world.AddEntity();
e.SetFadeScreen(new(FadeType.Out, Game.NowUnscaled + delay, time, color));

if (bufferDrawFrames > 0)
{
// With buffer frames we must wait until we get Game.Now otherwise we will get an value
// specially at lower frame rates
e.SetFadeScreen(new(FadeType.Out, delay, time, color, string.Empty, 0, bufferDrawFrames));
}
else
{
e.SetFadeScreen(new(FadeType.Out, Game.NowUnscaled + delay, time, color, string.Empty, 0, bufferDrawFrames));
}
}

public static void ApplyHighlight(World world, Entity e, HighlightSpriteComponent highlight)
Expand Down

0 comments on commit 5a05bb9

Please sign in to comment.