Refactor inventory menu logic

This commit is contained in:
2025-03-07 20:42:56 -08:00
parent d30fa35546
commit 8a61104868
19 changed files with 94 additions and 160 deletions

View File

@@ -12,16 +12,8 @@ public partial class GameLogic
public readonly record struct ExitPauseMenu;
public readonly record struct OpenInventory;
public readonly record struct CloseInventory;
public readonly record struct SetPauseMode(bool IsPaused);
public readonly record struct ShowMiniMap;
public readonly record struct HideMiniMap;
public readonly record struct ShowLostScreen;
public readonly record struct ExitLostScreen;

View File

@@ -14,24 +14,16 @@ public partial class GameLogic
{
var gameRepo = Get<IGameRepo>();
gameRepo.IsPaused.Sync += OnIsPaused;
gameRepo.OpenInventory += OnOpenInventory;
gameRepo.CloseInventory += OnCloseInventory;
gameRepo.DoubleExpTimeStart += OnDoubleExpTimeStart;
});
OnDetach(() =>
{
var gameRepo = Get<IGameRepo>();
gameRepo.IsPaused.Sync -= OnIsPaused;
gameRepo.OpenInventory -= OnOpenInventory;
gameRepo.CloseInventory -= OnCloseInventory;
gameRepo.DoubleExpTimeStart -= OnDoubleExpTimeStart;
});
}
private void OnOpenInventory() => Output(new Output.OpenInventory());
private void OnCloseInventory() => Output(new Output.CloseInventory());
private void OnDoubleExpTimeStart(int lengthOfTimeInSeconds) => Output(new Output.DoubleExpTimeStart(lengthOfTimeInSeconds));
public void OnIsPaused(bool isPaused) => Output(new Output.SetPauseMode(isPaused));

View File

@@ -8,9 +8,7 @@ public interface IGameRepo : IDisposable
{
event Action? Ended;
event Action? OpenInventory;
event Action? CloseInventory;
event Action? CloseInventoryEvent;
event Action<string>? AnnounceMessageOnMainScreenEvent;
@@ -38,14 +36,15 @@ public interface IGameRepo : IDisposable
public void RemoveItemFromInventory(IInventoryItem item);
public void CloseInventory();
public double ExpRate { get; }
}
public class GameRepo : IGameRepo
{
public event Action? Ended;
public event Action? OpenInventory;
public event Action? CloseInventory;
public event Action? CloseInventoryEvent;
public event Action<string>? AnnounceMessageOnMainScreenEvent;
public event Action<string>? AnnounceMessageInInventoryEvent;
public event Action<int>? DoubleExpTimeStart;
@@ -105,6 +104,11 @@ public class GameRepo : IGameRepo
RemoveItemFromInventoryEvent?.Invoke(item);
}
public void CloseInventory()
{
CloseInventoryEvent?.Invoke();
}
public void OnGameEnded()
{
Pause();

View File

@@ -1,25 +0,0 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma.Implementation;
public partial class GameLogic
{
public partial record State
{
[Meta]
public partial record InventoryOpened : Playing, IGet<Input.CloseInventory>
{
public InventoryOpened()
{
this.OnEnter(() => { Get<IGameRepo>().Pause(); Output(new Output.OpenInventory()); });
this.OnExit(() => { Get<IGameRepo>().Resume(); Output(new Output.CloseInventory()); });
}
public Transition On(in Input.CloseInventory input)
{
return To<Playing>();
}
}
}
}

View File

@@ -1,5 +1,4 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma.Implementation;
@@ -9,7 +8,6 @@ public partial class GameLogic
{
[Meta]
public partial record Playing : State,
IGet<Input.OpenInventory>,
IGet<Input.GameOver>,
IGet<Input.AskForTeleport>,
IGet<Input.PauseGame>,
@@ -22,9 +20,6 @@ public partial class GameLogic
public void OnEnded() => Input(new Input.GameOver());
public Transition On(in Input.OpenInventory input) => To<InventoryOpened>();
public Transition On(in Input.GameOver input) => To<Quit>();
public Transition On(in Input.AskForTeleport input) => To<AskForTeleport>();