Skip to content

Commit

Permalink
animation event now can be triggered even if skipped
Browse files Browse the repository at this point in the history
  • Loading branch information
saint11 committed Jul 22, 2023
1 parent 85e76b8 commit 71f09f2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/Murder.Editor/Systems/SpriteRenderDebugSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public void Draw(RenderContext render, Context context)

if (!frameInfo.Event.IsEmpty)
{
e.SendMessage(new AnimationEventMessage(frameInfo.Event.ToString()));
foreach (var ev in frameInfo.Event)
{
e.SendMessage(new AnimationEventMessage(ev));
}
}

if (frameInfo.Complete && overload != null)
Expand Down
10 changes: 9 additions & 1 deletion src/Murder/Core/Graphics/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ public FrameInfo Evaluate(float time, float lastFrameTime, bool animationLoop, f
int clampedFrame = Math.Clamp(frame, 0, Frames.Length - 1);
if (previousFrame != frame)
{
return new FrameInfo(Frames[clampedFrame], time + Game.FixedDeltaTime * 2 >= animationDuration, Events.ContainsKey(clampedFrame) ? Events[clampedFrame] : ReadOnlySpan<char>.Empty);
var events = ImmutableArray.CreateBuilder<string>();
for (int i = previousFrame; i <= frame; i++)
{
if (Events.ContainsKey(i))
{
events.Add(Events[i]);
}
}
return new FrameInfo(Frames[clampedFrame], time + Game.FixedDeltaTime * 2 >= animationDuration, events.ToImmutable());
}
else
{
Expand Down
11 changes: 9 additions & 2 deletions src/Murder/Core/Graphics/FrameInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Murder.Core;
/// <summary>
/// A struct representing information about a single animation frame, such as its index in the list and a flag indicating whether the animation is complete
/// </summary>
public ref struct FrameInfo
public readonly struct FrameInfo
{
internal static FrameInfo Fail => new() { Failed = true };

Expand All @@ -28,9 +28,16 @@ public ref struct FrameInfo
/// <summary>
/// A string ID representing the events played since the last played frame (if any). Usually set in Aseprite.
/// </summary>
public readonly ReadOnlySpan<char> Event;
public readonly ImmutableArray<string> Event = ImmutableArray<string>.Empty;

public FrameInfo(int frame, bool animationComplete, ReadOnlySpan<char> @event)
{
Frame = frame;
Complete = animationComplete;
Event = ImmutableArray.Create(@event.ToString());
}

public FrameInfo(int frame, bool animationComplete, ImmutableArray<string> @event)
{
Frame = frame;
Complete = animationComplete;
Expand Down
5 changes: 4 additions & 1 deletion src/Murder/Systems/Agents/AgentSpriteSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ public void Draw(RenderContext render, Context context)

if (!frameInfo.Event.IsEmpty)
{
e.SendMessage(new AnimationEventMessage(frameInfo.Event.ToString()));
foreach (var ev in frameInfo.Event)
{
e.SendMessage(new AnimationEventMessage(ev));
}
}

// The animation overload is now done
Expand Down
5 changes: 4 additions & 1 deletion src/Murder/Systems/Graphics/SpriteRenderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ public void Draw(RenderContext render, Context context)

if (!frameInfo.Event.IsEmpty)
{
e.SendMessage(new AnimationEventMessage(frameInfo.Event.ToString()));
foreach (var ev in frameInfo.Event)
{
e.SendMessage(new AnimationEventMessage(ev));
}
}

if (frameInfo.Complete)
Expand Down

0 comments on commit 71f09f2

Please sign in to comment.