using Godot;
using TheLegendOfGustav.Entities.Actors;
using TheLegendOfGustav.Entities.Actions;
using TheLegendOfGustav.Utils;
using TheLegendOfGustav.InputHandling;
using TheLegendOfGustav.Map;
using TheLegendOfGustav.Magic;
namespace TheLengendOfGustav.InputHandling;
///
/// Esquema de controles para quando o jogador precisar escolher um alvo
/// de feitiço.
///
/// É bem similar ao InspectorInputHandler.
///
public partial class CastSpellInputHandler : 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},
};
private Inspector inspector = null;
private SpellResource selectedSpell = null;
[Export]
private Map map;
public override void _Ready()
{
base._Ready();
// O jogador informa qual feitiço será usado.
SignalBus.Instance.PlayerSpellChooseLocation += (SpellResource spell) => selectedSpell = spell;
}
public override void Enter()
{
inspector = inspectorScene.Instantiate();
inspector.GridPosition = map.MapData.Player.GridPosition;
map.AddChild(inspector);
}
public override void Exit()
{
selectedSpell = null;
inspector.QueueFree();
}
public override Action GetAction(Player player)
{
Action action = null;
foreach (var direction in directions)
{
if (Input.IsActionJustPressed(direction.Key))
{
inspector.Walk(direction.Value);
}
}
if (selectedSpell != null && Input.IsActionJustPressed("ui_accept"))
{
action = new SpellAction(player, inspector.GridPosition - player.GridPosition, selectedSpell);
GetParent().SetInputHandler(InputHandlers.MainGame);
}
// Se o jogador cancelar a seleção,
// Mandamos um sinal avisando que o feitiço não foi executado com sucesso.
// Pergaminhos usam esta informação.
if (Input.IsActionJustPressed("quit"))
{
SignalBus.Instance.EmitSignal(SignalBus.SignalName.PlayerSpellCast, false);
GetParent().SetInputHandler(InputHandlers.MainGame);
}
return action;
}
}