summaryrefslogtreecommitdiff
path: root/scripts/InputHandling
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/InputHandling')
-rw-r--r--scripts/InputHandling/BaseInputHandler.cs31
-rw-r--r--scripts/InputHandling/BaseInputHandler.cs.uid1
-rw-r--r--scripts/InputHandling/GameOverInputHandler.cs16
-rw-r--r--scripts/InputHandling/GameOverInputHandler.cs.uid1
-rw-r--r--scripts/InputHandling/InputHandler.cs58
-rw-r--r--scripts/InputHandling/InputHandler.cs.uid1
-rw-r--r--scripts/InputHandling/InspectInputHandler.cs70
-rw-r--r--scripts/InputHandling/InspectInputHandler.cs.uid1
-rw-r--r--scripts/InputHandling/InventoryInputHandler.cs75
-rw-r--r--scripts/InputHandling/InventoryInputHandler.cs.uid1
-rw-r--r--scripts/InputHandling/MainGameInputHandler.cs58
-rw-r--r--scripts/InputHandling/MainGameInputHandler.cs.uid1
-rw-r--r--scripts/InputHandling/PickupInputHandler.cs50
-rw-r--r--scripts/InputHandling/PickupInputHandler.cs.uid1
14 files changed, 365 insertions, 0 deletions
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;
+
+/// <summary>
+/// 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.
+/// </summary>
+public abstract partial class BaseInputHandler : Node
+{
+ /// <summary>
+ /// Método executado quando o input handler entra em cena;
+ /// </summary>
+ public virtual void Enter() { }
+ /// <summary>
+ /// Método executado quando o input handler sai de cena;
+ /// </summary>
+ public virtual void Exit() { }
+ /// <summary>
+ /// Obtém uma ação do usuári conforme input.
+ /// </summary>
+ /// <param name="player">Jogador</param>
+ /// <returns>Ação que o jogador escolheu, nulo se nenhuma.</returns>
+ 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;
+
+/// <summary>
+/// Esquema de controles para quando o jogador está morto.
+/// </summary>
+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
+}
+
+/// <summary>
+/// Máquina de estado que obtém ações do usuário conforme o estado atual do jogo.
+/// </summary>
+public partial class InputHandler : Node
+{
+ [Export]
+ private InputHandlers StartingInputHandler { get; set; }
+
+ private Godot.Collections.Dictionary<InputHandlers, BaseInputHandler> 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>("MainGameInputHandler"));
+ // Controles para quando o jogador está morto.
+ InputHandlerDict.Add(InputHandlers.GameOver, GetNode<GameOverInputHandler>("GameOverInputHandler"));
+ InputHandlerDict.Add(InputHandlers.Inspect, GetNode<InspectInputHandler>("InspectInputHandler"));
+ InputHandlerDict.Add(InputHandlers.Pickup, GetNode<PickupInputHandler>("PickupInputHandler"));
+ InputHandlerDict.Add(InputHandlers.Inventory, GetNode<InventoryInputHandler>("InventoryInputHandler"));
+
+ SetInputHandler(StartingInputHandler);
+ }
+
+ public Action GetAction(Player player)
+ {
+ return SelectedInputHandler.GetAction(player);
+ }
+
+ /// <summary>
+ /// Define o esquema de controle atual do jogo
+ /// para o estado informado.
+ /// </summary>
+ /// <param name="inputhandler">Estado do jogo.</param>
+ 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;
+
+/// <summary>
+/// TODO: Esta solução é nojenta e precisa ser retrabalhada.
+/// </summary>
+public partial class InspectInputHandler : BaseInputHandler
+{
+ private static readonly PackedScene InspectorScene = GD.Load<PackedScene>("res://scenes/Inspector.tscn");
+
+ private static readonly Godot.Collections.Dictionary<string, Vector2I> 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},
+ };
+
+ /// <summary>
+ /// Preciso disso
+ /// </summary>
+ [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>();
+
+ 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<InputHandler>().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<PackedScene>("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<InventoryMenu>();
+ 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<InputHandler>().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;
+
+/// <summary>
+/// Esquema de controles principal do jogo.
+/// </summary>
+public partial class MainGameInputHandler : BaseInputHandler
+{
+ private static readonly Godot.Collections.Dictionary<string, Vector2I> 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<InputHandler>().SetInputHandler(InputHandlers.Inventory);
+ }
+
+ if (Input.IsActionJustPressed("pick-item"))
+ {
+ GetParent<InputHandler>().SetInputHandler(InputHandlers.Pickup);
+ }
+
+ if (Input.IsActionJustPressed("inspect"))
+ {
+ GetParent<InputHandler>().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;
+
+/// <summary>
+/// Esquema de controles para pegar um item.
+/// </summary>
+public partial class PickupInputHandler : BaseInputHandler
+{
+ private readonly Godot.Collections.Dictionary<string, Vector2I> 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<InputHandler>().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