From 358943907bc9e37d26618ee03adf1ffd97635335 Mon Sep 17 00:00:00 2001 From: Matheus Date: Thu, 28 Aug 2025 17:01:53 -0300 Subject: Combate corpo a corpo. --- scripts/InputHandler.cs | 34 ---------------- scripts/InputHandler.cs.uid | 1 - scripts/actors/Actor.cs | 60 +++++++++++++++++++++++++++-- scripts/actors/ActorDefinition.cs | 4 +- scripts/actors/Enemy.cs | 6 +++ scripts/actors/actions/BumpAction.cs | 2 +- scripts/actors/actions/DirectionalAction.cs | 14 ++++--- scripts/actors/actions/MeleeAction.cs | 18 +++++++-- scripts/actors/actions/MovementAction.cs | 6 +-- scripts/input/BaseInputHandler.cs.uid | 1 + scripts/input/InputHandler.cs | 36 +++++++++++++++++ scripts/input/InputHandler.cs.uid | 1 + 12 files changed, 128 insertions(+), 55 deletions(-) delete mode 100644 scripts/InputHandler.cs delete mode 100644 scripts/InputHandler.cs.uid create mode 100644 scripts/input/BaseInputHandler.cs.uid create mode 100644 scripts/input/InputHandler.cs create mode 100644 scripts/input/InputHandler.cs.uid (limited to 'scripts') diff --git a/scripts/InputHandler.cs b/scripts/InputHandler.cs deleted file mode 100644 index 39016ed..0000000 --- a/scripts/InputHandler.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Numerics; -using Godot; - -/// -/// Obtém input do usuário. -/// -public partial class InputHandler : Node { - 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}, - }; - public 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("skip-turn")) { - action = new WaitAction(player); - } - - return action; - } -} diff --git a/scripts/InputHandler.cs.uid b/scripts/InputHandler.cs.uid deleted file mode 100644 index 302a3b5..0000000 --- a/scripts/InputHandler.cs.uid +++ /dev/null @@ -1 +0,0 @@ -uid://ejqmdbc0524i diff --git a/scripts/actors/Actor.cs b/scripts/actors/Actor.cs index b0e050f..642e9fd 100644 --- a/scripts/actors/Actor.cs +++ b/scripts/actors/Actor.cs @@ -37,18 +37,20 @@ public abstract partial class Actor : Sprite2D get => gridPosition; } + private bool blocksMovement; /// - /// Se o ator bloqueia movimento (não pode oculpar a mesma célula de outro ator.) - /// + /// Se o ator bloqueia movimento (não pode oculpar a mesma célula de outro ator.) + /// public bool BlocksMovement { - get => definition.blocksMovement; + get => blocksMovement; } + private string actorName; /// /// Nome do ator. /// public string ActorName { - get => definition.name; + get => actorName; } private int hp; @@ -64,6 +66,9 @@ public abstract partial class Actor : Sprite2D set { // Esta propriedade impede que o HP seja maior que o máximo. hp = int.Clamp(value, 0, MaxHp); + if (hp <= 0) { + Die(); + } } } @@ -136,6 +141,9 @@ public abstract partial class Actor : Sprite2D /// A definição do ator. public virtual void SetDefinition(ActorDefinition definition) { this.definition = definition; + blocksMovement = definition.blocksMovement; + actorName = definition.name; + ZIndex = 1; Texture = definition.texture; MaxHp = definition.Hp; @@ -147,4 +155,48 @@ public abstract partial class Actor : Sprite2D Def = definition.Def; Men = definition.Men; } + + public virtual void Die() { + //⠀⠀⠀⠀⢠⣤⣤⣤⢠⣤⣤⣤⣤⣄⢀⣠⣤⣤⣄⠀⠀⠀⢀⣠⣤⣤⣄⠀⣤⣤⠀⠀⣠⣤⣤⣤⣤⣤⡄⢠⣤⣤⣤⣄⠀⠀ + //⠀⠀⠀⠀⠈⢹⣿⠉⠈⠉⣿⣿⠉⠉⢾⣿⣉⣉⠙⠀⠀⢀⣾⡟⠉⠉⣿⣧⢸⣿⡄⢠⣿⠏⣿⣿⣉⣉⡁⢸⣿⡏⢉⣿⡷⠀ + //⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⣿⣿⠀⠀⠈⠿⠿⣿⣿⡀⠀⠸⣿⡇⠀⠀⣾⣿⠀⢿⣿⣸⡿⠀⣿⣿⠿⠿⠇⢸⣿⣿⣿⣿⠀⠀ + //⠀⠀⠀⠀⢠⣼⣿⣤⠀⠀⣿⣿⠀⠀⢷⣦⣤⣼⡿⠁⠀⠀⠹⣿⣤⣴⡿⠋⠀⠘⣿⣿⠃⠀⣿⣿⣤⣤⡄⢸⣿⡇⠙⢿⣦⡀ + //⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⠀⠀⠀⠀⠀⠀⢀⣰⣶⣶⣶⣿⣿⣿⣿⣷⣶⣤⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⠿⠛⠛⠻⢿⣿⣿⣿⣿⣿⣿⣿⣶⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⠀⠀⠀⢀⢾⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠀⠈⠉⠉⠉⠻⢿⢿⣿⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⠀⠀⢠⠏⢸⣿⣿⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠿⢻⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⠀⢀⠇⠀⠈⠿⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⢀⡞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⡸⠀⠀⠀⠀⠀⠀⠀⠀⡼⠛⠳⣄⡀⠀⠐⢿⣦⡀⠀⠀⠀⢠⠃⠀⣸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⢠⠇⠀⠀⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⠉⣳⠟⠒⠻⣿⣦⡀⠀⡘⠀⢰⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⢀⠞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠃⢠⣄⡀⠈⠙⢿⡌⠁⠀⡞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠳⣄⣈⢻⡿⠃⢰⠟⠲⣼⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⠀⡰⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⡶⢴⠋⠀⠀⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⡴⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠞⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⡴⢟⠒⠀⠀⠀⠀⢰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠏⠀⠀⠈⠉⣿⠇⠀⢀⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⣷⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⠠⣤⣤⣀⢰⠏⠉⠙⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣀⣠⠴⠢⠦⠽⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⣿⣿⣿⣷⡄⣀⡀⠈⠉⠋⢹⠋⠁⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + //⠿⠿⠿⠿⠿⠦⠈⠀⠀⠀⠸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + + string deathMessage; + + if (Map_Data.Player == this) { + deathMessage = "Você morreu!"; + } else { + deathMessage = $"{ActorName} morreu!"; + } + + GD.Print(deathMessage); + + Texture = definition.deathTexture; + blocksMovement = false; + ZIndex = 0; + actorName = $"Restos mortais de ${actorName}"; + Map_Data.UnregisterBlockingActor(this); + } } \ No newline at end of file diff --git a/scripts/actors/ActorDefinition.cs b/scripts/actors/ActorDefinition.cs index 72eb745..7f67787 100644 --- a/scripts/actors/ActorDefinition.cs +++ b/scripts/actors/ActorDefinition.cs @@ -1,6 +1,5 @@ using Godot; - /// /// Define de forma genérica as características de um ator. /// @@ -14,6 +13,9 @@ public partial class ActorDefinition : Resource // Seu sprite. [Export] public Texture2D texture; + // Sprite de morto + [Export] + public Texture2D deathTexture; [ExportCategory("Mechanics")] // Se o ator bloqueia movimento. diff --git a/scripts/actors/Enemy.cs b/scripts/actors/Enemy.cs index 8cd3a74..19bcd2c 100644 --- a/scripts/actors/Enemy.cs +++ b/scripts/actors/Enemy.cs @@ -46,4 +46,10 @@ public partial class Enemy : Actor break; } } + + public override void Die() { + Soul.QueueFree(); + Soul = null; + base.Die(); + } } diff --git a/scripts/actors/actions/BumpAction.cs b/scripts/actors/actions/BumpAction.cs index def721b..0ce7fbe 100644 --- a/scripts/actors/actions/BumpAction.cs +++ b/scripts/actors/actions/BumpAction.cs @@ -19,7 +19,7 @@ public partial class BumpAction : DirectionalAction Action action; // Se houver um ator no destino, crie uma ação de ataque. - if (GetBlockingActorAtPosition(destination) != null) { + if (GetTargetActor() != null) { action = new MeleeAction(actor, Offset); } else { // Mas se não houver, crie uma ação de movimento. diff --git a/scripts/actors/actions/DirectionalAction.cs b/scripts/actors/actions/DirectionalAction.cs index 8c3c68f..ca2ca95 100644 --- a/scripts/actors/actions/DirectionalAction.cs +++ b/scripts/actors/actions/DirectionalAction.cs @@ -11,18 +11,20 @@ public abstract partial class DirectionalAction : Action /// Seu significado depende da ação que implementará esta classe. /// public Vector2I Offset { get; private set; } + /// + /// Coordenada do alvo da ação. + /// + public Vector2I Destination { get => actor.GridPosition + Offset; } public DirectionalAction(Actor actor, Vector2I offset) : base(actor) { Offset = offset; } /// - /// É conveniente ter acesso à função para obter atores em uma determinada posição. - /// Este método expõe o método de mesmo nome do mapa. + /// Função que obtém o alvo da ação, se houver. /// - /// Posição para verificar - /// O ator naquela posição, nulo se não houver. - protected Actor GetBlockingActorAtPosition(Vector2I pos) { - return Map_Data.GetBlockingActorAtPosition(pos); + /// O ator alvo da ação, nulo se não houver. + protected Actor GetTargetActor() { + return Map_Data.GetBlockingActorAtPosition(Destination); } } diff --git a/scripts/actors/actions/MeleeAction.cs b/scripts/actors/actions/MeleeAction.cs index cf40f1d..c61a803 100644 --- a/scripts/actors/actions/MeleeAction.cs +++ b/scripts/actors/actions/MeleeAction.cs @@ -14,14 +14,24 @@ public partial class MeleeAction : DirectionalAction /// public override void Perform() { - Vector2I destination = actor.GridPosition + Offset; // Eu te disse que este método seria útil. - Actor target = GetBlockingActorAtPosition(destination); + Actor target = GetTargetActor(); // Se não houver um ator na direção, não podemos continuar. if (target == null) return; - // TODO: Implementar ataque. - GD.Print($"Você tenta socar {target.ActorName}, mas como não sobra nada para o beta, você ainda não tem um método de ataque."); + // não podemos ter dano negativo. + int damage = actor.Atk - target.Def; + + string attackDesc = $"{actor.ActorName} ataca {target.ActorName}"; + + if (damage > 0) { + attackDesc += $" e remove {damage} de HP."; + target.Hp -= damage; + } else { + attackDesc += $" mas {target.ActorName} tem músculos de aço."; + } + + GD.Print(attackDesc); } } diff --git a/scripts/actors/actions/MovementAction.cs b/scripts/actors/actions/MovementAction.cs index f86d542..b37f9ae 100644 --- a/scripts/actors/actions/MovementAction.cs +++ b/scripts/actors/actions/MovementAction.cs @@ -11,14 +11,12 @@ public partial class MovementAction : DirectionalAction public override void Perform() { - Vector2I finalDestination = actor.GridPosition + Offset; - // Não anda se o destino for um tile sólido. - if (!Map_Data.IsTileWalkable(finalDestination)) return; + if (!Map_Data.IsTileWalkable(Destination)) return; // Não anda se o destino for oculpado por um ator. // Na maioria dos casos, essa condição nunca é verdadeira. - if (GetBlockingActorAtPosition(finalDestination) != null) return; + if (GetTargetActor() != null) return; actor.Walk(Offset); } diff --git a/scripts/input/BaseInputHandler.cs.uid b/scripts/input/BaseInputHandler.cs.uid new file mode 100644 index 0000000..deae303 --- /dev/null +++ b/scripts/input/BaseInputHandler.cs.uid @@ -0,0 +1 @@ +uid://cl1of0602byx0 diff --git a/scripts/input/InputHandler.cs b/scripts/input/InputHandler.cs new file mode 100644 index 0000000..838790e --- /dev/null +++ b/scripts/input/InputHandler.cs @@ -0,0 +1,36 @@ +using System.Numerics; +using Godot; + +/// +/// Obtém input do usuário. +/// +public partial class InputHandler : Node { + 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}, + }; + public Action GetAction(Player player) { + Action action = null; + + if (player.IsAlive) { + foreach (var direction in directions) { + if (Input.IsActionJustPressed(direction.Key)) { + action = new BumpAction(player, direction.Value); + } + } + + if (Input.IsActionJustPressed("skip-turn")) { + action = new WaitAction(player); + } + } + + return action; + } +} diff --git a/scripts/input/InputHandler.cs.uid b/scripts/input/InputHandler.cs.uid new file mode 100644 index 0000000..302a3b5 --- /dev/null +++ b/scripts/input/InputHandler.cs.uid @@ -0,0 +1 @@ +uid://ejqmdbc0524i -- cgit v1.2.3