Skip to content

Commit b7715a7

Browse files
author
benpollarduk
committed
Added narrative end callback
1 parent 562f621 commit b7715a7

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NetAF.Logic.Callbacks
2+
{
3+
/// <summary>
4+
/// Represents the callback used for when a narrative ends.
5+
/// </summary>
6+
/// <param name="game">The executing game.</param>
7+
public delegate void NarrativeEndCallback(Game game);
8+
}

NetAF/Logic/Game.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,14 @@ private static UpdateResult UpdateWhenActive(Game game, string input)
587587
// handle the reaction
588588
game.HandleReaction(reaction);
589589

590-
// check if the game has ended, and if so end
591-
if (game.CheckForGameEnd(game.EndConditions, out game.endMode))
592-
game.State = GameState.EndConditionMet;
593-
594-
// providing the game hasn't finished render
590+
// providing the game hasn't finished render it
595591
if (game.State != GameState.Finished)
596592
game.Mode.Render(game);
597593

594+
// also if the game is still active, check if the game has ended, and if so end
595+
if (game.State == GameState.Active && game.CheckForGameEnd(game.EndConditions, out game.endMode))
596+
game.State = GameState.EndConditionMet;
597+
598598
return new(true);
599599
}
600600

NetAF/Logic/Modes/NarrativeMode.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using NetAF.Interpretation;
2+
using NetAF.Logic.Callbacks;
23
using NetAF.Narratives;
34
using NetAF.Rendering.FrameBuilders;
45

@@ -8,7 +9,8 @@ namespace NetAF.Logic.Modes
89
/// Provides a display mode for narrative.
910
/// </summary>
1011
/// <param name="narrative">The narrative.</param>
11-
public sealed class NarrativeMode(Narrative narrative) : IGameMode
12+
/// <param name="endCallback">An optional callback to invoke when the narrative ends.</param>
13+
public sealed class NarrativeMode(Narrative narrative, NarrativeEndCallback endCallback = null) : IGameMode
1214
{
1315
#region Fields
1416

@@ -44,6 +46,9 @@ public void Render(Game game)
4446
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<INarrativeFrameBuilder>().Build(narrative, game.Configuration.DisplaySize);
4547
game.Configuration.Adapter.RenderFrame(frame);
4648
isComplete = narrative.IsComplete;
49+
50+
if (isComplete)
51+
endCallback?.Invoke(game);
4752
}
4853
}
4954

NetAF/Narratives/Narrative.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ public string Next()
5353
if ((sections?[index].IsComplete ?? false) && index < sections.Length - 1)
5454
index++;
5555

56-
var entry = sections?[index].Next() ?? string.Empty;
57-
58-
return entry;
56+
return sections?[index].Next() ?? string.Empty;
5957
}
6058

6159
/// <summary>

0 commit comments

Comments
 (0)