From c6bbb834f7758027c0df338f1520f34fad3befea Mon Sep 17 00:00:00 2001 From: Matheus Date: Tue, 9 Sep 2025 19:09:34 -0300 Subject: Organização MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/InputHandling/BaseInputHandler.cs | 31 +++++++++ scripts/InputHandling/BaseInputHandler.cs.uid | 1 + scripts/InputHandling/GameOverInputHandler.cs | 16 +++++ scripts/InputHandling/GameOverInputHandler.cs.uid | 1 + scripts/InputHandling/InputHandler.cs | 58 +++++++++++++++++ scripts/InputHandling/InputHandler.cs.uid | 1 + scripts/InputHandling/InspectInputHandler.cs | 70 ++++++++++++++++++++ scripts/InputHandling/InspectInputHandler.cs.uid | 1 + scripts/InputHandling/InventoryInputHandler.cs | 75 ++++++++++++++++++++++ scripts/InputHandling/InventoryInputHandler.cs.uid | 1 + scripts/InputHandling/MainGameInputHandler.cs | 58 +++++++++++++++++ scripts/InputHandling/MainGameInputHandler.cs.uid | 1 + scripts/InputHandling/PickupInputHandler.cs | 50 +++++++++++++++ scripts/InputHandling/PickupInputHandler.cs.uid | 1 + 14 files changed, 365 insertions(+) create mode 100644 scripts/InputHandling/BaseInputHandler.cs create mode 100644 scripts/InputHandling/BaseInputHandler.cs.uid create mode 100644 scripts/InputHandling/GameOverInputHandler.cs create mode 100644 scripts/InputHandling/GameOverInputHandler.cs.uid create mode 100644 scripts/InputHandling/InputHandler.cs create mode 100644 scripts/InputHandling/InputHandler.cs.uid create mode 100644 scripts/InputHandling/InspectInputHandler.cs create mode 100644 scripts/InputHandling/InspectInputHandler.cs.uid create mode 100644 scripts/InputHandling/InventoryInputHandler.cs create mode 100644 scripts/InputHandling/InventoryInputHandler.cs.uid create mode 100644 scripts/InputHandling/MainGameInputHandler.cs create mode 100644 scripts/InputHandling/MainGameInputHandler.cs.uid create mode 100644 scripts/InputHandling/PickupInputHandler.cs create mode 100644 scripts/InputHandling/PickupInputHandler.cs.uid (limited to 'scripts/InputHandling') diff --git a/scripts/InputHandling/BaseInputHandler.cs b/scripts/InputHandling/BaseInputHandler.cs new file mode 100644 index 0000000..f3527ca --- /dev/null +++ b/scripts/InputHandling/BaseInputHandler.cs @@ -0,0 +1,31 @@ +using Godot; +using TheLegendOfGustav.Entities.Actors; +using TheLegendOfGustav.Entities.Actions; + +namespace TheLegendOfGustav.InputHandling; + +/// +/// Classe base para obter ações do usuário. +/// É interessante ter mais de um objeto para obter ações de +/// usuário porque permite limitar certas ações para +/// certos estados do jogo. Atualmente, o jogo +/// possui somente dois estados: Com jogador vivo e com jogador morto. +/// Mas isto pode aumentar. +/// +public abstract partial class BaseInputHandler : Node +{ + /// + /// Método executado quando o input handler entra em cena; + /// + public virtual void Enter() { } + /// + /// Método executado quando o input handler sai de cena; + /// + public virtual void Exit() { } + /// + /// Obtém uma ação do usuári conforme input. + /// + /// Jogador + /// Ação que o jogador escolheu, nulo se nenhuma. + public abstract Action GetAction(Player player); +} diff --git a/scripts/InputHandling/BaseInputHandler.cs.uid b/scripts/InputHandling/BaseInputHandler.cs.uid new file mode 100644 index 0000000..deae303 --- /dev/null +++ b/scripts/InputHandling/BaseInputHandler.cs.uid @@ -0,0 +1 @@ +uid://cl1of0602byx0 diff --git a/scripts/InputHandling/GameOverInputHandler.cs b/scripts/InputHandling/GameOverInputHandler.cs new file mode 100644 index 0000000..e11e98a --- /dev/null +++ b/scripts/InputHandling/GameOverInputHandler.cs @@ -0,0 +1,16 @@ +using TheLegendOfGustav.Entities.Actions; +using TheLegendOfGustav.Entities.Actors; + +namespace TheLegendOfGustav.InputHandling; + +/// +/// Esquema de controles para quando o jogador está morto. +/// +public partial class GameOverInputHandler : BaseInputHandler +{ + // Por enquanto não tem nada. + public override Action GetAction(Player player) + { + return null; + } +} \ No newline at end of file diff --git a/scripts/InputHandling/GameOverInputHandler.cs.uid b/scripts/InputHandling/GameOverInputHandler.cs.uid new file mode 100644 index 0000000..14ddfd8 --- /dev/null +++ b/scripts/InputHandling/GameOverInputHandler.cs.uid @@ -0,0 +1 @@ +uid://ogqlb3purl6n diff --git a/scripts/InputHandling/InputHandler.cs b/scripts/InputHandling/InputHandler.cs new file mode 100644 index 0000000..55a17b4 --- /dev/null +++ b/scripts/InputHandling/InputHandler.cs @@ -0,0 +1,58 @@ +using Godot; +using TheLegendOfGustav.Entities.Actions; +using TheLegendOfGustav.Entities.Actors; + +namespace TheLegendOfGustav.InputHandling; + +public enum InputHandlers +{ + MainGame, + GameOver, + Inspect, + Pickup, + Inventory +} + +/// +/// Máquina de estado que obtém ações do usuário conforme o estado atual do jogo. +/// +public partial class InputHandler : Node +{ + [Export] + private InputHandlers StartingInputHandler { get; set; } + + private Godot.Collections.Dictionary InputHandlerDict { get; set; } = []; + + private BaseInputHandler SelectedInputHandler { get; set; } + + public override void _Ready() + { + base._Ready(); + // Controles para quando o jogador está vivo e jogando normalmente. + InputHandlerDict.Add(InputHandlers.MainGame, GetNode("MainGameInputHandler")); + // Controles para quando o jogador está morto. + InputHandlerDict.Add(InputHandlers.GameOver, GetNode("GameOverInputHandler")); + InputHandlerDict.Add(InputHandlers.Inspect, GetNode("InspectInputHandler")); + InputHandlerDict.Add(InputHandlers.Pickup, GetNode("PickupInputHandler")); + InputHandlerDict.Add(InputHandlers.Inventory, GetNode("InventoryInputHandler")); + + SetInputHandler(StartingInputHandler); + } + + public Action GetAction(Player player) + { + return SelectedInputHandler.GetAction(player); + } + + /// + /// Define o esquema de controle atual do jogo + /// para o estado informado. + /// + /// Estado do jogo. + public void SetInputHandler(InputHandlers inputhandler) + { + SelectedInputHandler?.Exit(); + SelectedInputHandler = InputHandlerDict[inputhandler]; + SelectedInputHandler.Enter(); + } +} diff --git a/scripts/InputHandling/InputHandler.cs.uid b/scripts/InputHandling/InputHandler.cs.uid new file mode 100644 index 0000000..f61cdba --- /dev/null +++ b/scripts/InputHandling/InputHandler.cs.uid @@ -0,0 +1 @@ +uid://bxt6g7t1uvvia diff --git a/scripts/InputHandling/InspectInputHandler.cs b/scripts/InputHandling/InspectInputHandler.cs new file mode 100644 index 0000000..90734f9 --- /dev/null +++ b/scripts/InputHandling/InspectInputHandler.cs @@ -0,0 +1,70 @@ +using Godot; +using TheLegendOfGustav.Entities.Actions; +using TheLegendOfGustav.Entities.Actors; +using TheLegendOfGustav.Utils; + +namespace TheLegendOfGustav.InputHandling; + +/// +/// TODO: Esta solução é nojenta e precisa ser retrabalhada. +/// +public partial class InspectInputHandler : BaseInputHandler +{ + private static readonly PackedScene InspectorScene = GD.Load("res://scenes/Inspector.tscn"); + + private static readonly Godot.Collections.Dictionary directions = new() + { + {"walk-up", Vector2I.Up}, + {"walk-down", Vector2I.Down}, + {"walk-left", Vector2I.Left}, + {"walk-right", Vector2I.Right}, + {"walk-up-right", Vector2I.Up + Vector2I.Right}, + {"walk-up-left", Vector2I.Up + Vector2I.Left}, + {"walk-down-right", Vector2I.Down + Vector2I.Right}, + {"walk-down-left", Vector2I.Down + Vector2I.Left}, + }; + + /// + /// Preciso disso + /// + [Export] + private Map.Map Map { get; set; } + + private Inspector Inspector { get; set; } + + public override void Enter() + { + SignalBus.Instance.EmitSignal(SignalBus.SignalName.EnterInspectionMode); + Inspector = InspectorScene.Instantiate(); + + Inspector.GridPosition = Map.MapData.Player.GridPosition; + + Map.AddChild(Inspector); + } + + public override void Exit() + { + Inspector.QueueFree(); + + SignalBus.Instance.EmitSignal(SignalBus.SignalName.ExitInspectionMode); + } + + public override Action GetAction(Player player) + { + Action action = null; + foreach (var direction in directions) + { + if (Input.IsActionJustPressed(direction.Key)) + { + Inspector.Walk(direction.Value); + } + } + + if (Input.IsActionJustPressed("quit")) + { + GetParent().SetInputHandler(InputHandlers.MainGame); + } + + return action; + } +} \ No newline at end of file diff --git a/scripts/InputHandling/InspectInputHandler.cs.uid b/scripts/InputHandling/InspectInputHandler.cs.uid new file mode 100644 index 0000000..20141fa --- /dev/null +++ b/scripts/InputHandling/InspectInputHandler.cs.uid @@ -0,0 +1 @@ +uid://bamlnrj5bm1rd diff --git a/scripts/InputHandling/InventoryInputHandler.cs b/scripts/InputHandling/InventoryInputHandler.cs new file mode 100644 index 0000000..390e545 --- /dev/null +++ b/scripts/InputHandling/InventoryInputHandler.cs @@ -0,0 +1,75 @@ +using Godot; +using TheLegendOfGustav.GUI; +using TheLegendOfGustav.Entities.Actors; +using TheLegendOfGustav.Entities.Items; +using TheLegendOfGustav.Entities.Actions; + +namespace TheLegendOfGustav.InputHandling; + +public partial class InventoryInputHandler : BaseInputHandler +{ + private static readonly PackedScene inventoryScene = GD.Load("res://scenes/GUI/invetory_menu.tscn"); + + + [Export] + private Map.Map Map { get; set; } + + private InventoryMenu InventoryMenu { get; set; } + private ConsumableItem ActivationItem { get; set; } = null; + private ConsumableItem DropItem { get; set; } = null; + + public override void Enter() + { + InventoryMenu = inventoryScene.Instantiate(); + Map.MapData.Player.AddChild(InventoryMenu); + InventoryMenu.Initialize(Map.MapData.Player.Inventory); + InventoryMenu.ItemSelected += OnItemActivate; + InventoryMenu.ItemDrop += OnItemDrop; + } + + public override void Exit() + { + ActivationItem = null; + DropItem = null; + InventoryMenu.QueueFree(); + } + + public override Action GetAction(Player player) + { + Action action = null; + + if (ActivationItem != null) + { + action = new ItemAction(player, ActivationItem); + Close(); + } + + if (DropItem != null) + { + action = new DropAction(player, DropItem); + Close(); + } + + if (Input.IsActionJustPressed("quit")) + { + Close(); + } + + return action; + } + + private void Close() + { + GetParent().SetInputHandler(InputHandlers.MainGame); + } + + private void OnItemActivate(ConsumableItem item) + { + ActivationItem = item; + } + + private void OnItemDrop(ConsumableItem item) + { + DropItem = item; + } +} \ No newline at end of file diff --git a/scripts/InputHandling/InventoryInputHandler.cs.uid b/scripts/InputHandling/InventoryInputHandler.cs.uid new file mode 100644 index 0000000..b5d0ed9 --- /dev/null +++ b/scripts/InputHandling/InventoryInputHandler.cs.uid @@ -0,0 +1 @@ +uid://bjcjktvyrdh10 diff --git a/scripts/InputHandling/MainGameInputHandler.cs b/scripts/InputHandling/MainGameInputHandler.cs new file mode 100644 index 0000000..fe8e573 --- /dev/null +++ b/scripts/InputHandling/MainGameInputHandler.cs @@ -0,0 +1,58 @@ +using Godot; +using TheLegendOfGustav.Entities.Actions; +using TheLegendOfGustav.Entities.Actors; + +namespace TheLegendOfGustav.InputHandling; + +/// +/// Esquema de controles principal do jogo. +/// +public partial class MainGameInputHandler : BaseInputHandler +{ + private static readonly Godot.Collections.Dictionary directions = new() + { + {"walk-up", Vector2I.Up}, + {"walk-down", Vector2I.Down}, + {"walk-left", Vector2I.Left}, + {"walk-right", Vector2I.Right}, + {"walk-up-right", Vector2I.Up + Vector2I.Right}, + {"walk-up-left", Vector2I.Up + Vector2I.Left}, + {"walk-down-right", Vector2I.Down + Vector2I.Right}, + {"walk-down-left", Vector2I.Down + Vector2I.Left}, + }; + + public override Action GetAction(Player player) + { + Action action = null; + + foreach (var direction in directions) + { + if (Input.IsActionJustPressed(direction.Key)) + { + action = new BumpAction(player, direction.Value); + } + } + + if (Input.IsActionJustPressed("open-inventory")) + { + GetParent().SetInputHandler(InputHandlers.Inventory); + } + + if (Input.IsActionJustPressed("pick-item")) + { + GetParent().SetInputHandler(InputHandlers.Pickup); + } + + if (Input.IsActionJustPressed("inspect")) + { + GetParent().SetInputHandler(InputHandlers.Inspect); + } + + if (Input.IsActionJustPressed("skip-turn")) + { + action = new WaitAction(player); + } + + return action; + } +} diff --git a/scripts/InputHandling/MainGameInputHandler.cs.uid b/scripts/InputHandling/MainGameInputHandler.cs.uid new file mode 100644 index 0000000..302a3b5 --- /dev/null +++ b/scripts/InputHandling/MainGameInputHandler.cs.uid @@ -0,0 +1 @@ +uid://ejqmdbc0524i diff --git a/scripts/InputHandling/PickupInputHandler.cs b/scripts/InputHandling/PickupInputHandler.cs new file mode 100644 index 0000000..1396b2b --- /dev/null +++ b/scripts/InputHandling/PickupInputHandler.cs @@ -0,0 +1,50 @@ +using Godot; +using TheLegendOfGustav.Entities.Actions; +using TheLegendOfGustav.Entities.Actors; + +namespace TheLegendOfGustav.InputHandling; + +/// +/// Esquema de controles para pegar um item. +/// +public partial class PickupInputHandler : BaseInputHandler +{ + private readonly Godot.Collections.Dictionary directions = new() + { + {"walk-up", Vector2I.Up}, + {"walk-down", Vector2I.Down}, + {"walk-left", Vector2I.Left}, + {"walk-right", Vector2I.Right}, + {"walk-up-right", Vector2I.Up + Vector2I.Right}, + {"walk-up-left", Vector2I.Up + Vector2I.Left}, + {"walk-down-right", Vector2I.Down + Vector2I.Right}, + {"walk-down-left", Vector2I.Down + Vector2I.Left}, + {"skip-turn", Vector2I.Zero} + }; + + public override Action GetAction(Player player) + { + Action action = null; + + foreach (var direction in directions) + { + if (Input.IsActionJustPressed(direction.Key)) + { + action = new PickupAction(player, direction.Value); + Quit(); + } + } + + if (Input.IsActionJustPressed("quit")) + { + Quit(); + } + + return action; + } + + private void Quit() + { + GetParent().SetInputHandler(InputHandlers.MainGame); + } +} diff --git a/scripts/InputHandling/PickupInputHandler.cs.uid b/scripts/InputHandling/PickupInputHandler.cs.uid new file mode 100644 index 0000000..139dd25 --- /dev/null +++ b/scripts/InputHandling/PickupInputHandler.cs.uid @@ -0,0 +1 @@ +uid://dspqgdxg5jji0 -- cgit v1.2.3