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>();

View File

@@ -24,6 +24,7 @@ public partial class InGameUILogic
var gameRepo = Get<IGameRepo>();
gameRepo.AnnounceMessageOnMainScreenEvent -= OnAnnounceMessageOnMainScreen;
gameRepo.AnnounceMessageInInventoryEvent -= OnAnnounceMessageInInventory;
gameRepo.RemoveItemFromInventoryEvent -= OnRemoveItemFromInventory;
});
}

View File

@@ -5,5 +5,7 @@ public partial class InGameUILogic
{
public readonly record struct ShowMinimap;
public readonly record struct HideMinimap;
public readonly record struct ShowInventory;
public readonly record struct HideInventory;
}
}

View File

@@ -8,7 +8,9 @@ public partial class InGameUILogic
public readonly record struct AnnounceMessageOnMainScreen(string Message);
public readonly record struct AnnounceMessageInInventory(string Message);
public readonly record struct RemoveItemFromInventory(IInventoryItem Item);
public readonly record struct DisplayMinimap();
public readonly record struct HideMinimap();
public readonly record struct DisplayMinimap;
public readonly record struct HideMinimap;
public readonly record struct ShowInventory;
public readonly record struct HideInventory;
}
}

View File

@@ -7,9 +7,10 @@ public partial class InGameUILogic
public partial record State
{
[Meta]
public partial record Active : State, IGet<Input.ShowMinimap>
public partial record Active : State, IGet<Input.ShowMinimap>, IGet<Input.ShowInventory>
{
public Transition On(in Input.ShowMinimap input) => To<MinimapOpen>();
public Transition On(in Input.ShowInventory input) => To<InventoryOpen>();
}
}
}

View File

@@ -0,0 +1,35 @@
using Chickensoft.Introspection;
namespace Zennysoft.Game.Ma.Implementation;
public partial class InGameUILogic
{
public partial record State
{
[Meta]
public partial record InventoryOpen : Active, IGet<Input.HideInventory>
{
public InventoryOpen()
{
OnAttach(() =>
{
var gameRepo = Get<IGameRepo>();
gameRepo.CloseInventoryEvent += OnCloseInventoryEvent;
gameRepo.Pause();
Output(new Output.ShowInventory());
});
OnDetach(() =>
{
var gameRepo = Get<IGameRepo>();
gameRepo.Resume();
gameRepo.CloseInventoryEvent -= OnCloseInventoryEvent;
Output(new Output.HideInventory());
});
}
private void OnCloseInventoryEvent() => Input(new Input.HideInventory());
public Transition On(in Input.HideInventory input) => To<Active>();
}
}
}

View File

@@ -1,5 +1,4 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma.Implementation;
@@ -8,7 +7,7 @@ public partial class InGameUILogic
public partial record State
{
[Meta]
public partial record MinimapOpen : State, IGet<Input.HideMinimap>
public partial record MinimapOpen : Active, IGet<Input.HideMinimap>
{
public MinimapOpen()
{