Refactoring game logic

This commit is contained in:
2025-03-07 12:05:20 -08:00
parent e7ef669c29
commit c3bfab5f53
13 changed files with 127 additions and 42 deletions

View File

@@ -12,9 +12,9 @@ public partial class GameLogic
public readonly record struct ExitPauseMenu;
public readonly record struct OpenInventory();
public readonly record struct OpenInventory;
public readonly record struct HideInventory;
public readonly record struct CloseInventory;
public readonly record struct SetPauseMode(bool IsPaused);
@@ -41,5 +41,9 @@ public partial class GameLogic
public readonly record struct GoToOverworld;
public readonly record struct SaveGame;
public readonly record struct AnnounceMessage(string Message);
public readonly record struct DoubleExpTimeStart(int lengthOfTimeInSeconds);
}
}

View File

@@ -14,14 +14,30 @@ public partial class GameLogic
{
var gameRepo = Get<IGameRepo>();
gameRepo.IsPaused.Sync += OnIsPaused;
gameRepo.OpenInventory += OnOpenInventory;
gameRepo.CloseInventory += OnCloseInventory;
gameRepo.AnnounceMessage += OnAnnounceMessage;
gameRepo.DoubleExpTimeStart += OnDoubleExpTimeStart;
});
OnDetach(() =>
{
var gameRepo = Get<IGameRepo>();
gameRepo.IsPaused.Sync -= OnIsPaused;
gameRepo.OpenInventory -= OnOpenInventory;
gameRepo.CloseInventory -= OnCloseInventory;
gameRepo.AnnounceMessage -= OnAnnounceMessage;
gameRepo.DoubleExpTimeStart -= OnDoubleExpTimeStart;
});
}
private void OnOpenInventory() => Output(new Output.OpenInventory());
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

@@ -5,27 +5,51 @@ namespace Zennysoft.Game.Ma.Implementation;
public interface IGameRepo : IDisposable
{
event Action? Ended;
event Action? OpenInventory;
event Action? CloseInventory;
event Action<string>? AnnounceMessage;
event Action<int>? DoubleExpTimeStart;
event Action? DoubleExpTimeEnd;
void Pause();
void Resume();
IAutoProp<bool> IsPaused { get; }
public void StartDoubleEXP(TimeSpan lengthOfEffect);
public void EndDoubleExp();
public double ExpRate { get; }
}
public class GameRepo : IGameRepo
{
public event Action? Ended;
public event Action? OpenInventory;
public event Action? CloseInventory;
public event Action<string>? AnnounceMessage;
public event Action<int>? DoubleExpTimeStart;
public event Action? DoubleExpTimeEnd;
public IAutoProp<bool> IsPaused => _isPaused;
private readonly AutoProp<bool> _isPaused;
public int MaxItemSize => 20;
public double ExpRate { get; private set; }
private bool _disposedValue;
public GameRepo()
{
_isPaused = new AutoProp<bool>(true);
ExpRate = 1;
}
public void Pause()
@@ -40,6 +64,20 @@ public class GameRepo : IGameRepo
GD.Print("Resume");
}
public void StartDoubleEXP(TimeSpan lengthOfEffect)
{
CloseInventory?.Invoke();
AnnounceMessage?.Invoke("Experience points temporarily doubled.");
DoubleExpTimeStart?.Invoke(lengthOfEffect.Seconds);
ExpRate = 2;
}
public void EndDoubleExp()
{
AnnounceMessage?.Invoke("Experience points effect wore off.");
ExpRate = 1;
}
public void OnGameEnded()
{
Pause();

View File

@@ -13,7 +13,7 @@ public partial class GameLogic
public InventoryOpened()
{
this.OnEnter(() => { Get<IGameRepo>().Pause(); Output(new Output.OpenInventory()); });
this.OnExit(() => { Get<IGameRepo>().Resume(); Output(new Output.HideInventory()); });
this.OnExit(() => { Get<IGameRepo>().Resume(); Output(new Output.CloseInventory()); });
}
public Transition On(in Input.CloseInventory input)