Start refactoring UI concerns away from game and into UI logic class

This commit is contained in:
2025-03-07 16:48:45 -08:00
parent c3bfab5f53
commit 93c04440d4
11 changed files with 111 additions and 23 deletions

View File

@@ -42,8 +42,6 @@ public partial class GameLogic
public readonly record struct SaveGame;
public readonly record struct AnnounceMessage(string Message);
public readonly record struct DoubleExpTimeStart(int lengthOfTimeInSeconds);
}
}

View File

@@ -16,7 +16,6 @@ public partial class GameLogic
gameRepo.IsPaused.Sync += OnIsPaused;
gameRepo.OpenInventory += OnOpenInventory;
gameRepo.CloseInventory += OnCloseInventory;
gameRepo.AnnounceMessage += OnAnnounceMessage;
gameRepo.DoubleExpTimeStart += OnDoubleExpTimeStart;
});
OnDetach(() =>
@@ -25,7 +24,6 @@ public partial class GameLogic
gameRepo.IsPaused.Sync -= OnIsPaused;
gameRepo.OpenInventory -= OnOpenInventory;
gameRepo.CloseInventory -= OnCloseInventory;
gameRepo.AnnounceMessage -= OnAnnounceMessage;
gameRepo.DoubleExpTimeStart -= OnDoubleExpTimeStart;
});
}
@@ -34,8 +32,6 @@ public partial class GameLogic
private void OnCloseInventory() => Output(new Output.CloseInventory());
private void OnAnnounceMessage(string message) => Output(new Output.AnnounceMessage(message));
private void OnDoubleExpTimeStart(int lengthOfTimeInSeconds) => Output(new Output.DoubleExpTimeStart(lengthOfTimeInSeconds));
public void OnIsPaused(bool isPaused) => Output(new Output.SetPauseMode(isPaused));

View File

@@ -27,6 +27,8 @@ public interface IGameRepo : IDisposable
public void EndDoubleExp();
public void AnnounceMessageOnMainScreen(string message);
public double ExpRate { get; }
}
@@ -67,17 +69,22 @@ public class GameRepo : IGameRepo
public void StartDoubleEXP(TimeSpan lengthOfEffect)
{
CloseInventory?.Invoke();
AnnounceMessage?.Invoke("Experience points temporarily doubled.");
AnnounceMessageOnMainScreen("Experience points temporarily doubled.");
DoubleExpTimeStart?.Invoke(lengthOfEffect.Seconds);
ExpRate = 2;
}
public void EndDoubleExp()
{
AnnounceMessage?.Invoke("Experience points effect wore off.");
AnnounceMessageOnMainScreen("Experience points effect wore off.");
ExpRate = 1;
}
public void AnnounceMessageOnMainScreen(string message)
{
AnnounceMessage?.Invoke(message);
}
public void OnGameEnded()
{
Pause();

View File

@@ -0,0 +1,30 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma.Implementation;
public partial class InGameUILogic
{
[Meta]
public abstract partial record State : StateLogic<State>
{
protected State()
{
OnAttach(() =>
{
var gameRepo = Get<IGameRepo>();
gameRepo.AnnounceMessage += OnAnnounceMessage;
});
OnDetach(() =>
{
var gameRepo = Get<IGameRepo>();
gameRepo.AnnounceMessage -= OnAnnounceMessage;
});
}
private void OnAnnounceMessage(string message)
{
Output(new Output.AnnounceMessage(message));
}
}
}

View File

@@ -0,0 +1,13 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma.Implementation;
public interface IInGameUILogic : ILogicBlock<InGameUILogic.State>;
[Meta]
[LogicBlock(typeof(State), Diagram = true)]
public partial class InGameUILogic : LogicBlock<InGameUILogic.State>, IInGameUILogic
{
public override Transition GetInitialState() => To<State.Active>();
}

View File

@@ -0,0 +1,8 @@
namespace Zennysoft.Game.Ma.Implementation;
public partial class InGameUILogic
{
public static class Output
{
public readonly record struct AnnounceMessage(string Message);
}
}

View File

@@ -0,0 +1,15 @@
using Chickensoft.Introspection;
namespace Zennysoft.Game.Ma.Implementation;
public partial class InGameUILogic
{
public partial record State
{
[Meta]
public partial record Active : State
{
}
}
}