Files
GameJamDungeon/src/game/Game.cs
2024-09-04 12:52:28 -07:00

87 lines
2.6 KiB
C#

namespace GameJamDungeon;
using Chickensoft.AutoInject;
using Chickensoft.GodotNodeInterfaces;
using Chickensoft.Introspection;
using Godot;
public interface IGame : IProvide<IGameRepo>, INode3D;
[Meta(typeof(IAutoNode))]
public partial class Game : Node3D, IGame
{
public override void _Notification(int what) => this.Notify(what);
IGameRepo IProvide<IGameRepo>.Value() => GameRepo;
public IGameLogic GameLogic { get; set; } = default!;
public IGameRepo GameRepo { get; set; } = default!;
public GameLogic.IBinding GameBinding { get; set; } = default!;
[Dependency] public IAppRepo AppRepo => this.DependOn<IAppRepo>();
[Node] public IInventoryMenu InventoryMenu { get; set; } = default!;
[Node] public Control MiniMap { get; set; } = default!;
[Node] public NavigationRegion3D NavigationRegion { get; set; } = default!;
public void Setup()
{
GameRepo = new GameRepo();
GameLogic = new GameLogic();
GameLogic.Set(GameRepo);
GameLogic.Set(AppRepo);
}
public void OnResolved()
{
GameBinding = GameLogic.Bind();
GameBinding
.Handle((in GameLogic.Output.StartGame _) => { })
.Handle((in GameLogic.Output.SetPauseMode output) => { CallDeferred(nameof(SetPauseMode), output.IsPaused); })
.Handle((in GameLogic.Output.SetInventoryMode _) => { InventoryMenu.PopulateItems(_.Inventory); InventoryMenu.Show(); })
.Handle((in GameLogic.Output.HideInventory _) => { InventoryMenu.Hide(); InventoryMenu.ClearItems(); })
.Handle((in GameLogic.Output.ShowMiniMap _) => { MiniMap.Show(); })
.Handle((in GameLogic.Output.HideMiniMap _) => { MiniMap.Hide(); })
.Handle((in GameLogic.Output.GameOver _) => { AppRepo.OnGameOver(); });
GameLogic.Start();
GameLogic.Input(new GameLogic.Input.Initialize());
this.Provide();
}
public override void _Input(InputEvent @event)
{
if (Input.IsActionJustPressed(GameInputs.Inventory))
{
GD.Print("Inventory button pressed");
GameLogic.Input(new GameLogic.Input.InventoryMenuButtonPressed());
}
if (Input.IsActionJustPressed(GameInputs.MiniMap))
{
GD.Print("MiniMap button pressed");
GameLogic.Input(new GameLogic.Input.MiniMapButtonPressed());
}
if (Input.IsActionJustReleased(GameInputs.MiniMap))
{
GD.Print("MiniMap button released");
GameLogic.Input(new GameLogic.Input.MiniMapButtonReleased());
}
}
private void SetPauseMode(bool isPaused)
{
if (GetTree() != null)
GetTree().Paused = isPaused;
}
public void OnStart() => GameLogic.Input(new GameLogic.Input.Start());
}