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

View File

@@ -4,7 +4,6 @@ namespace Zennysoft.Game.Ma;
using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Chickensoft.SaveFileBuilder;
using Zennysoft.Game.Ma.src.items;
using Godot;
using System;
using System.IO.Abstractions;
@@ -193,8 +192,6 @@ public partial class Game : Node3D, IGame
.Handle((in GameLogic.Output.LoadMap _) => { Map.LoadMap(); })
.Handle((in GameLogic.Output.ShowFloorClearMenu _) => { FloorClearMenu.Show(); FloorClearMenu.FadeIn(); })
.Handle((in GameLogic.Output.ExitFloorClearMenu _) => { FloorClearMenu.FadeOut(); })
.Handle((in GameLogic.Output.OpenInventory _) => { InGameUI.ShowInventoryScreen(); InGameUI.InventoryMenu.SetProcessInput(true); })
.Handle((in GameLogic.Output.CloseInventory _) => { InGameUI.HideInventoryScreen(); InGameUI.InventoryMenu.SetProcessInput(false); })
.Handle((in GameLogic.Output.ShowAskForTeleport _) => { GameRepo.Pause(); InGameUI.UseTeleportPrompt.FadeIn(); InGameUI.SetProcessInput(true); })
.Handle((in GameLogic.Output.HideAskForTeleport _) => { GameRepo.Resume(); InGameUI.UseTeleportPrompt.FadeOut(); InGameUI.SetProcessInput(false); })
.Handle((in GameLogic.Output.ShowLostScreen _) => { DeathMenu.Show(); DeathMenu.FadeIn(); })
@@ -215,7 +212,6 @@ public partial class Game : Node3D, IGame
PauseMenu.TransitionCompleted += OnPauseMenuTransitioned;
PauseMenu.UnpauseButtonPressed += PauseMenu_UnpauseButtonPressed;
InGameUI.InventoryMenu.ClosedMenu += InventoryMenu_CloseInventory;
InGameUI.UseTeleportPrompt.TeleportToNextFloor += UseTeleportPrompt_TeleportToNextFloor;
InGameUI.UseTeleportPrompt.CloseTeleportPrompt += UseTeleportPrompt_CloseTeleportPrompt;
@@ -245,14 +241,6 @@ public partial class Game : Node3D, IGame
GameLogic.Input(new GameLogic.Input.PauseGame());
}
public void ToggleInventory()
{
if (GameLogic.Value is InventoryOpened)
GameLogic.Input(new GameLogic.Input.CloseInventory());
else
GameLogic.Input(new GameLogic.Input.OpenInventory());
}
public void FloorExitReached()
{
GameLogic.Input(new GameLogic.Input.AskForTeleport());
@@ -314,22 +302,28 @@ public partial class Game : Node3D, IGame
}
if (item is ThrowableItem throwableItem)
{
if (throwableItem.ThrowableItemTag == ThrowableItemTag.DoubleExp)
GameRepo.StartDoubleEXP(TimeSpan.FromSeconds(30));
switch (throwableItem.ThrowableItemTag)
{
case ThrowableItemTag.DoubleExp:
GameRepo.StartDoubleEXP(TimeSpan.FromSeconds(30));
break;
case ThrowableItemTag.TeleportToRandomLocation:
_effectService.TeleportToRandomRoom(Player);
InGameUI.CloseInventory();
break;
case ThrowableItemTag.CanChangeAffinity:
_effectService.ChangeAffinity(throwableItem);
break;
case ThrowableItemTag.WarpToExitIfFound:
_effectService.WarpToExit(Player);
InGameUI.CloseInventory();
break;
}
if (throwableItem.HealHPAmount > 0)
Player.HealHP(throwableItem.HealHPAmount);
if (throwableItem.HealVTAmount > 0)
Player.HealVT(throwableItem.HealVTAmount);
if (throwableItem.ThrowableItemTag == ThrowableItemTag.TeleportToRandomLocation)
_effectService.TeleportToRandomRoom(Player);
if (throwableItem.ThrowableItemTag == ThrowableItemTag.CanChangeAffinity)
_effectService.ChangeAffinity(throwableItem);
if (throwableItem.ThrowableItemTag == ThrowableItemTag.WarpToExitIfFound)
_effectService.WarpToExit(Player);
}
await ToSignal(GetTree().CreateTimer(1f), "timeout");
@@ -427,8 +421,6 @@ public partial class Game : Node3D, IGame
GameRepo.EndDoubleExp();
}
private void InventoryMenu_CloseInventory() => GameLogic.Input(new GameLogic.Input.CloseInventory());
public void NextFloorLoaded()
{
GameLogic.Input(new GameLogic.Input.HideFloorClearMenu());

View File

@@ -27,8 +27,6 @@ public interface IGame : IProvide<IGameRepo>, IProvide<IGameEventDepot>, IProvid
public void ThrowItem(IInventoryItem item);
public void ToggleInventory();
public void FloorExitReached();
public void NextFloorLoaded();

View File

@@ -15,8 +15,6 @@ public interface IInventoryMenu : IControl
public Task DisplayMessage(string message);
event InventoryMenu.ClosedMenuEventHandler ClosedMenu;
public void RemoveItem(IInventoryItem item);
}
@@ -33,9 +31,6 @@ public partial class InventoryMenu : Control, IInventoryMenu
[Dependency] public IGameEventDepot GameEventDepot => this.DependOn<IGameEventDepot>();
[Signal]
public delegate void ClosedMenuEventHandler();
private InventoryPageNumber _currentPageNumber = InventoryPageNumber.FirstPage;
private string ITEM_SLOT_SCENE = "res://src/inventory_menu/ItemSlot.tscn";
@@ -178,7 +173,7 @@ public partial class InventoryMenu : Control, IInventoryMenu
}
else
{
EmitSignal(SignalName.ClosedMenu);
_gameRepo.CloseInventory();
}
}
@@ -429,7 +424,7 @@ public partial class InventoryMenu : Control, IInventoryMenu
if (_currentIndex <= 0)
_currentIndex = 0;
EmitSignal(SignalName.ClosedMenu);
_gameRepo.CloseInventory();
}
private async void DropButtonPressed()
@@ -443,7 +438,7 @@ public partial class InventoryMenu : Control, IInventoryMenu
if (_currentIndex <= 0)
_currentIndex = 0;
EmitSignal(SignalName.ClosedMenu);
_gameRepo.CloseInventory();
}
private async void RefreshUIAfterUserSelection()

View File

@@ -3,7 +3,7 @@ using System.Linq;
using System;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma.src.items;
namespace Zennysoft.Game.Ma;
public class EffectService
{
@@ -189,7 +189,6 @@ public class EffectService
var roomsGodotCollection = new Godot.Collections.Array<MonsterRoom>(validRooms);
var randomRoom = roomsGodotCollection.PickRandom();
var spawnPoint = randomRoom.PlayerSpawn;
_game.ToggleInventory();
player.TeleportPlayer(spawnPoint.Transform);
}

View File

@@ -1,6 +1,5 @@
using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Zennysoft.Game.Ma.src.items;
using Godot;
using Zennysoft.Game.Ma.Implementation;
using Zennysoft.Game.Abstractions;

View File

@@ -10,8 +10,6 @@ public interface IPlayer : IKillable, IProvide<ISaveChunk<PlayerData>>
{
public void Attack();
public void ToggleInventory();
public void PlayerPause();
public void TakeDamage(double damage, ElementType elementType = ElementType.None, bool isCriticalHit = false);

View File

@@ -266,11 +266,6 @@ public partial class Player : CharacterBody3D, IPlayer
PlayerLogic.Input(new PlayerLogic.Input.Attack());
}
public void ToggleInventory()
{
Game.ToggleInventory();
}
public void PlayerPause()
{
Game.TogglePause();
@@ -396,9 +391,6 @@ public partial class Player : CharacterBody3D, IPlayer
public override void _UnhandledInput(InputEvent @event)
{
if (@event.IsActionPressed(GameInputs.Inventory))
ToggleInventory();
if (@event.IsActionPressed(GameInputs.Pause))
PlayerPause();

View File

@@ -2,22 +2,13 @@ using Chickensoft.AutoInject;
using Chickensoft.GodotNodeInterfaces;
using Chickensoft.Introspection;
using Godot;
using Renci.SshNet.Messages;
using Zennysoft.Game.Ma.Implementation;
namespace Zennysoft.Game.Ma;
public interface IInGameUI : IControl
{
public void ShowInventoryScreen();
public void HideInventoryScreen();
public void ShowMiniMap();
public void HideMiniMap();
event InGameUI.MinimapButtonReleasedEventHandler MinimapButtonReleased;
void CloseInventory();
}
[Meta(typeof(IAutoNode))]
@@ -39,9 +30,6 @@ public partial class InGameUI : Control, IInGameUI
public InGameUILogic.IBinding InGameUILogicBinding { get; set; } = default!;
[Signal]
public delegate void MinimapButtonReleasedEventHandler();
public void Setup()
{
InGameUILogic = new InGameUILogic();
@@ -53,31 +41,23 @@ public partial class InGameUI : Control, IInGameUI
InGameUILogicBinding = InGameUILogic.Bind();
InGameUILogicBinding
.Handle((in InGameUILogic.Output.AnnounceMessageOnMainScreen output) =>
{
PlayerInfoUI.DisplayMessage(output.Message);
})
.Handle((in InGameUILogic.Output.AnnounceMessageInInventory output) =>
{
InventoryMenu.DisplayMessage(output.Message);
})
.Handle((in InGameUILogic.Output.RemoveItemFromInventory output) =>
{
InventoryMenu.RemoveItem(output.Item);
})
.Handle((in InGameUILogic.Output.DisplayMinimap _) =>
{
ShowMiniMap();
})
.Handle((in InGameUILogic.Output.HideMinimap _) =>
{
HideMiniMap();
});
;
.Handle((in InGameUILogic.Output.AnnounceMessageOnMainScreen output) => { PlayerInfoUI.DisplayMessage(output.Message); })
.Handle((in InGameUILogic.Output.AnnounceMessageInInventory output) => { InventoryMenu.DisplayMessage(output.Message); })
.Handle((in InGameUILogic.Output.RemoveItemFromInventory output) => { InventoryMenu.RemoveItem(output.Item); })
.Handle((in InGameUILogic.Output.DisplayMinimap _) => { MiniMap.SetProcessUnhandledInput(true); MiniMap.Show(); })
.Handle((in InGameUILogic.Output.HideMinimap _) => { MiniMap.SetProcessUnhandledInput(false); MiniMap.Hide(); })
.Handle((in InGameUILogic.Output.ShowInventory _) => { InventoryMenu.RefreshInventoryScreen(); InventoryMenu.Show(); InventoryMenu.SetProcessInput(true); })
.Handle((in InGameUILogic.Output.HideInventory _) => { CloseInventory(); });
InGameUILogic.Start();
}
public void CloseInventory()
{
InventoryMenu.Hide();
InventoryMenu.SetProcessInput(false);
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event.IsActionPressed(GameInputs.MiniMap))
@@ -90,28 +70,11 @@ public partial class InGameUI : Control, IInGameUI
GD.Print("MiniMap button released");
InGameUILogic.Input(new InGameUILogic.Input.HideMinimap());
}
}
public void HideInventoryScreen()
{
InventoryMenu.Hide();
}
public void HideMiniMap()
{
MiniMap.SetProcessUnhandledInput(false);
MiniMap.Hide();
}
public void ShowInventoryScreen()
{
InventoryMenu.RefreshInventoryScreen();
InventoryMenu.Show();
}
public void ShowMiniMap()
{
MiniMap.SetProcessUnhandledInput(true);
MiniMap.Show();
if (@event.IsActionPressed(GameInputs.Inventory))
{
GD.Print("Inventory button pressed");
InGameUILogic.Input(new InGameUILogic.Input.ShowInventory());
}
}
}