Move GameLogic to new project

This commit is contained in:
2025-03-07 01:39:18 -08:00
parent b7bf4f3d10
commit 65e4af8595
28 changed files with 33 additions and 25 deletions

View File

@@ -0,0 +1,40 @@
namespace Zennysoft.Game.Ma.Implementation;
public partial class GameLogic
{
public static class Input
{
public readonly record struct StartGame;
public readonly record struct Initialize;
public readonly record struct GoToOverworld;
public readonly record struct SaveGame;
public readonly record struct OpenInventory;
public readonly record struct CloseInventory;
public readonly record struct MiniMapButtonPressed;
public readonly record struct MiniMapButtonReleased;
public readonly record struct FloorExitReached;
public readonly record struct HideFloorClearMenu;
public readonly record struct GameOver;
public readonly record struct GoToNextFloor;
public readonly record struct PauseGame;
public readonly record struct UnpauseGame;
public readonly record struct PauseMenuTransitioned;
public readonly record struct AskForTeleport;
public readonly record struct HideAskForTeleport;
}
}

View File

@@ -0,0 +1,45 @@
namespace Zennysoft.Game.Ma.Implementation;
public partial class GameLogic
{
public static class Output
{
public readonly record struct StartGame;
public readonly record struct ShowPauseMenu;
public readonly record struct HidePauseMenu;
public readonly record struct ExitPauseMenu;
public readonly record struct OpenInventory();
public readonly record struct HideInventory;
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;
public readonly record struct LoadNextFloor;
public readonly record struct LoadMap;
public readonly record struct ShowFloorClearMenu;
public readonly record struct ExitFloorClearMenu;
public readonly record struct ShowAskForTeleport;
public readonly record struct HideAskForTeleport;
public readonly record struct GoToOverworld;
public readonly record struct SaveGame;
}
}

View File

@@ -0,0 +1,27 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma.Implementation;
public partial class GameLogic
{
[Meta]
public abstract partial record State : StateLogic<State>
{
protected State()
{
OnAttach(() =>
{
var gameRepo = Get<IGameRepo>();
gameRepo.IsPaused.Sync += OnIsPaused;
});
OnDetach(() =>
{
var gameRepo = Get<IGameRepo>();
gameRepo.IsPaused.Sync -= OnIsPaused;
});
}
public void OnIsPaused(bool isPaused) => Output(new Output.SetPauseMode(isPaused));
}
}

View File

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

View File

@@ -0,0 +1,87 @@
using Chickensoft.Collections;
using Godot;
using System;
namespace Zennysoft.Game.Ma.Implementation;
public interface IGameRepo : IDisposable
{
void Pause();
void Resume();
IAutoProp<bool> IsPaused { get; }
void SetPlayerGlobalTransform(Transform3D playerGlobalTransform);
public int MaxItemSize { get; }
public int EXPRate { get; set; }
public int CurrentFloor { get; set; }
}
public class GameRepo : IGameRepo
{
public event Action? Ended;
public IAutoProp<Transform3D> PlayerGlobalTransform => _playerGlobalTransform;
private readonly AutoProp<Transform3D> _playerGlobalTransform;
public IAutoProp<bool> IsPaused => _isPaused;
private readonly AutoProp<bool> _isPaused;
public int MaxItemSize => 20;
public int EXPRate { get; set; } = 1;
private bool _disposedValue;
public int CurrentFloor { get; set; } = 0;
public GameRepo()
{
_isPaused = new AutoProp<bool>(true);
_playerGlobalTransform = new AutoProp<Transform3D>(Transform3D.Identity);
}
public void Pause()
{
_isPaused.OnNext(true);
GD.Print("Paused");
}
public void Resume()
{
_isPaused.OnNext(false);
GD.Print("Resume");
}
public void SetPlayerGlobalTransform(Transform3D playerGlobalTransform) => _playerGlobalTransform.OnNext(playerGlobalTransform);
public void OnGameEnded()
{
Pause();
Ended?.Invoke();
}
protected void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_isPaused.OnCompleted();
_isPaused.Dispose();
}
_disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}

View File

@@ -0,0 +1,26 @@
using Chickensoft.Introspection;
namespace Zennysoft.Game.Ma.Implementation;
public partial class GameLogic
{
public partial record State
{
[Meta]
public partial record AskForTeleport : Playing, IGet<Input.FloorExitReached>, IGet<Input.HideAskForTeleport>
{
public AskForTeleport()
{
this.OnAttach(() => { Get<IGameRepo>().Pause(); Output(new Output.ShowAskForTeleport()); });
this.OnDetach(() => { Output(new Output.HideAskForTeleport()); });
}
public Transition On(in Input.FloorExitReached input) => To<FloorClearedDecisionState>();
public Transition On(in Input.HideAskForTeleport input)
{
return To<Playing>();
}
}
}
}

View File

@@ -0,0 +1 @@
uid://boai3f4vwgree

View File

@@ -0,0 +1,36 @@
using Chickensoft.Introspection;
namespace Zennysoft.Game.Ma.Implementation;
public partial class GameLogic
{
public partial record State
{
[Meta]
public partial record FloorClearedDecisionState : Playing, IGet<Input.GoToNextFloor>, IGet<Input.SaveGame>, IGet<Input.HideFloorClearMenu>
{
public FloorClearedDecisionState()
{
this.OnAttach(() => { Get<IGameRepo>().Pause(); Output(new Output.ShowFloorClearMenu()); });
this.OnDetach(() => { Output(new Output.ExitFloorClearMenu()); });
}
public Transition On(in Input.GoToNextFloor input)
{
Output(new Output.LoadNextFloor());
return ToSelf();
}
public Transition On(in Input.HideFloorClearMenu input)
{
return To<Playing>();
}
public Transition On(in Input.SaveGame input)
{
Output(new Output.SaveGame());
return To<Playing>();
}
}
}
}

View File

@@ -0,0 +1 @@
uid://dw1wcux7lcpqy

View File

@@ -0,0 +1 @@
uid://cpu75lmblmbj4

View File

@@ -0,0 +1,25 @@
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.HideInventory()); });
}
public Transition On(in Input.CloseInventory input)
{
return To<Playing>();
}
}
}
}

View File

@@ -0,0 +1 @@
uid://dmbireek8b68t

View File

@@ -0,0 +1,33 @@
namespace Zennysoft.Game.Ma.Implementation;
using Chickensoft.Introspection;
using Zennysoft.Game.Abstractions;
public partial class GameLogic
{
public partial record State
{
[Meta]
public partial record MenuBackdrop : State, IGet<Input.StartGame>, IGet<Input.Initialize>
{
public MenuBackdrop()
{
OnAttach(() => Get<IAppRepo>().GameEntered += OnGameEntered);
OnDetach(() => Get<IAppRepo>().GameEntered -= OnGameEntered);
}
public void OnGameEntered() => Input(new Input.StartGame());
public Transition On(in Input.StartGame input)
{
Output(new Output.LoadMap());
return To<Playing>();
}
public Transition On(in Input.Initialize input)
{
return ToSelf();
}
}
}
}

View File

@@ -0,0 +1 @@
uid://lwjsht36v6ut

View File

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

View File

@@ -0,0 +1 @@
uid://nlpm8t4dege2

View File

@@ -0,0 +1,26 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma.Implementation;
public partial class GameLogic
{
public partial record State
{
[Meta]
public partial record Paused : Playing, IGet<Input.UnpauseGame>
{
public Paused()
{
this.OnEnter(() =>
{
Get<IGameRepo>().Pause();
Output(new Output.ShowPauseMenu());
});
this.OnExit(() => Output(new Output.ExitPauseMenu()));
}
public virtual Transition On(in Input.UnpauseGame input) => To<Resuming>();
}
}
}

View File

@@ -0,0 +1 @@
uid://c46publoqhqsn

View File

@@ -0,0 +1,43 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma.Implementation;
public partial class GameLogic
{
public partial record State
{
[Meta]
public partial record Playing : State,
IGet<Input.OpenInventory>,
IGet<Input.MiniMapButtonPressed>,
IGet<Input.GameOver>,
IGet<Input.AskForTeleport>,
IGet<Input.PauseGame>,
IGet<Input.GoToOverworld>
{
public Playing()
{
OnAttach(() => Output(new Output.StartGame()));
}
public void OnEnded() => Input(new Input.GameOver());
public Transition On(in Input.OpenInventory input) => To<InventoryOpened>();
public Transition On(in Input.MiniMapButtonPressed input) => To<MinimapOpen>();
public Transition On(in Input.GameOver input) => To<Quit>();
public Transition On(in Input.AskForTeleport input) => To<AskForTeleport>();
public Transition On(in Input.PauseGame input) => To<Paused>();
public Transition On(in Input.GoToOverworld input)
{
Output(new Output.GoToOverworld());
return ToSelf();
}
}
}
}

View File

@@ -0,0 +1 @@
uid://i54nvesmcliy

View File

@@ -0,0 +1,19 @@
using Chickensoft.Introspection;
using Chickensoft.LogicBlocks;
namespace Zennysoft.Game.Ma.Implementation;
public partial class GameLogic
{
public partial record State
{
[Meta]
public partial record Quit : State
{
public Quit()
{
this.OnEnter(() => Output(new Output.ShowLostScreen()));
}
}
}
}

View File

@@ -0,0 +1 @@
uid://cq37bi3y07rxa

View File

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

View File

@@ -0,0 +1 @@
uid://clh8ucurjwuvh