From 6c7e2ac133986efa57b43df52a5498c6f7efcf75 Mon Sep 17 00:00:00 2001 From: Matheus Date: Tue, 26 Aug 2025 22:59:57 -0300 Subject: AI --- scripts/Enemy.cs | 9 ----- scripts/Enemy.cs.uid | 1 - scripts/Game.cs | 5 +-- scripts/actors/AI/BaseAI.cs | 20 +++++++++++ scripts/actors/AI/BaseAI.cs.uid | 1 + scripts/actors/AI/HostileEnemyAI.cs | 38 ++++++++++++++++++++ scripts/actors/AI/HostileEnemyAI.cs.uid | 1 + scripts/actors/Actor.cs | 60 ++++++++++++++++++++++++++------ scripts/actors/ActorDefinition.cs | 12 +++++++ scripts/actors/Enemy.cs | 34 ++++++++++++++++++ scripts/actors/Enemy.cs.uid | 1 + scripts/actors/EnemyDefinition.cs | 8 +++++ scripts/actors/EnemyDefinition.cs.uid | 1 + scripts/actors/actions/MovementAction.cs | 2 ++ scripts/map/DungeonGenerator.cs | 7 ++-- scripts/map/MapData.cs | 36 +++++++++++++++++++ 16 files changed, 211 insertions(+), 25 deletions(-) delete mode 100644 scripts/Enemy.cs delete mode 100644 scripts/Enemy.cs.uid create mode 100644 scripts/actors/AI/BaseAI.cs create mode 100644 scripts/actors/AI/BaseAI.cs.uid create mode 100644 scripts/actors/AI/HostileEnemyAI.cs create mode 100644 scripts/actors/AI/HostileEnemyAI.cs.uid create mode 100644 scripts/actors/Enemy.cs create mode 100644 scripts/actors/Enemy.cs.uid create mode 100644 scripts/actors/EnemyDefinition.cs create mode 100644 scripts/actors/EnemyDefinition.cs.uid (limited to 'scripts') diff --git a/scripts/Enemy.cs b/scripts/Enemy.cs deleted file mode 100644 index 6f9f3d9..0000000 --- a/scripts/Enemy.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Godot; -using System; - -public partial class Enemy : Actor -{ - public Enemy(Vector2I initialPosition, MapData map, ActorDefinition definition) : base(initialPosition, map, definition) - { - } -} diff --git a/scripts/Enemy.cs.uid b/scripts/Enemy.cs.uid deleted file mode 100644 index 93255b7..0000000 --- a/scripts/Enemy.cs.uid +++ /dev/null @@ -1 +0,0 @@ -uid://bef1fo3vgvxej diff --git a/scripts/Game.cs b/scripts/Game.cs index 5c18cd7..818b064 100644 --- a/scripts/Game.cs +++ b/scripts/Game.cs @@ -44,8 +44,9 @@ public partial class Game : Node { private void HandleEnemyTurns() { foreach (Actor actor in Map.Map_Data.Actors) { if (actor is Player) continue; - - GD.Print($"O {actor.ActorName} foi cuckado e não tem como agir."); + if (actor is Enemy enemy && enemy.IsAlive) { + enemy.Soul.Perform(); + } } } } diff --git a/scripts/actors/AI/BaseAI.cs b/scripts/actors/AI/BaseAI.cs new file mode 100644 index 0000000..f9b5387 --- /dev/null +++ b/scripts/actors/AI/BaseAI.cs @@ -0,0 +1,20 @@ +using Godot; + +public abstract partial class BaseAI : Node { + protected Actor body; + + public override void _Ready() + { + base._Ready(); + body = GetParent(); + } + + public abstract void Perform(); + + public Godot.Collections.Array GetPathTo(Vector2I destination) { + Godot.Collections.Array list = []; + Vector2[] path = body.Map_Data.Pathfinder.GetPointPath(body.GridPosition, destination); + list.AddRange(path); + return list; + } +} \ No newline at end of file diff --git a/scripts/actors/AI/BaseAI.cs.uid b/scripts/actors/AI/BaseAI.cs.uid new file mode 100644 index 0000000..b23724c --- /dev/null +++ b/scripts/actors/AI/BaseAI.cs.uid @@ -0,0 +1 @@ +uid://jgm5qk02hism diff --git a/scripts/actors/AI/HostileEnemyAI.cs b/scripts/actors/AI/HostileEnemyAI.cs new file mode 100644 index 0000000..061295f --- /dev/null +++ b/scripts/actors/AI/HostileEnemyAI.cs @@ -0,0 +1,38 @@ +using Godot; + +public partial class HostileEnemyAI : BaseAI +{ + private Godot.Collections.Array path = []; + + public override void Perform() + { + Player target = body.Map_Data.Player; + Vector2I offset = target.GridPosition - body.GridPosition; + int distance = int.Max(int.Abs(offset.X), int.Abs(offset.Y)); + + Action action; + + if (body.Map_Data.GetTile(body.GridPosition).IsInView) { + if (distance <= 1) { + action = new MeleeAction(body, offset); + action.Perform(); + return; + } + path = GetPathTo(target.GridPosition); + GD.Print($"Arno Breker: {path}"); + path.RemoveAt(0); + } + + if (path.Count > 0) { + Vector2I destination = (Vector2I) path[0]; + GD.Print(destination); + if (body.Map_Data.GetBlockingActorAtPosition(destination) != null) { + return; + } + + action = new MovementAction(body, destination - body.GridPosition); + action.Perform(); + path.RemoveAt(0); + } + } +} \ No newline at end of file diff --git a/scripts/actors/AI/HostileEnemyAI.cs.uid b/scripts/actors/AI/HostileEnemyAI.cs.uid new file mode 100644 index 0000000..0fa2c32 --- /dev/null +++ b/scripts/actors/AI/HostileEnemyAI.cs.uid @@ -0,0 +1 @@ +uid://db28cxff4pl3t diff --git a/scripts/actors/Actor.cs b/scripts/actors/Actor.cs index e8d728a..199c301 100644 --- a/scripts/actors/Actor.cs +++ b/scripts/actors/Actor.cs @@ -3,18 +3,10 @@ using Godot; [GlobalClass] public abstract partial class Actor : Sprite2D { - private ActorDefinition definition; - private Vector2I gridPosition = Vector2I.Zero; + protected ActorDefinition definition; public MapData Map_Data { get; set; } - public Actor(Vector2I initialPosition, MapData map, ActorDefinition definition) { - GridPosition = initialPosition; - Map_Data = map; - this.definition = definition; - Texture = definition.texture; - Centered = false; - } - + private Vector2I gridPosition = Vector2I.Zero; public Vector2I GridPosition { set { gridPosition = value; @@ -31,6 +23,30 @@ public abstract partial class Actor : Sprite2D get => definition.name; } + private int hp; + public int MaxHp { get; private set; } + public int Hp { + get => hp; + set { + hp = int.Clamp(value, 0, MaxHp); + } + } + + private int mp; + public int MaxMp { get; private set; } + public int Mp { + get => mp; + set { + mp = int.Clamp(value, 0, MaxMp); + } + } + + public int Atk { get; private set; } + + public int Def { get; private set; } + + public int Men { get; private set; } + public override void _Ready() { base._Ready(); @@ -38,6 +54,30 @@ public abstract partial class Actor : Sprite2D } public void Walk(Vector2I offset) { + Map_Data.UnregisterBlockingActor(this); GridPosition += offset; + Map_Data.RegisterBlockingActor(this); + } + + public Actor(Vector2I initialPosition, MapData map, ActorDefinition definition) { + GridPosition = initialPosition; + Map_Data = map; + Centered = false; + + SetDefinition(definition); + } + + public virtual void SetDefinition(ActorDefinition definition) { + this.definition = definition; + Texture = definition.texture; + + MaxHp = definition.Hp; + Hp = definition.Hp; + MaxMp = definition.Mp; + Mp = definition.Mp; + + Atk = definition.Atk; + Def = definition.Def; + Men = definition.Men; } } \ No newline at end of file diff --git a/scripts/actors/ActorDefinition.cs b/scripts/actors/ActorDefinition.cs index 5678921..5c0ece9 100644 --- a/scripts/actors/ActorDefinition.cs +++ b/scripts/actors/ActorDefinition.cs @@ -13,4 +13,16 @@ public partial class ActorDefinition : Resource [ExportCategory("Mechanics")] [Export] public bool blocksMovement = true; + + [ExportCategory("Stats")] + [Export] + public int Hp; + [Export] + public int Mp; + [Export] + public int Atk; + [Export] + public int Def; + [Export] + public int Men; } diff --git a/scripts/actors/Enemy.cs b/scripts/actors/Enemy.cs new file mode 100644 index 0000000..6c111b1 --- /dev/null +++ b/scripts/actors/Enemy.cs @@ -0,0 +1,34 @@ +using Godot; +using System; + +public enum AIType +{ + None, + DefaultHostile +}; + +public partial class Enemy : Actor +{ + public BaseAI Soul { get; private set; } + + public bool IsAlive { get => Soul != null; } + + public Enemy(Vector2I initialPosition, MapData map, EnemyDefinition definition) : base(initialPosition, map, definition) + { + SetDefinition(definition); + } + + public void SetDefinition(EnemyDefinition definition) + { + base.SetDefinition(definition); + + switch(definition.AI) { + case AIType.None: + break; + case AIType.DefaultHostile: + Soul = new HostileEnemyAI(); + AddChild(Soul); + break; + } + } +} diff --git a/scripts/actors/Enemy.cs.uid b/scripts/actors/Enemy.cs.uid new file mode 100644 index 0000000..93255b7 --- /dev/null +++ b/scripts/actors/Enemy.cs.uid @@ -0,0 +1 @@ +uid://bef1fo3vgvxej diff --git a/scripts/actors/EnemyDefinition.cs b/scripts/actors/EnemyDefinition.cs new file mode 100644 index 0000000..21d4ca0 --- /dev/null +++ b/scripts/actors/EnemyDefinition.cs @@ -0,0 +1,8 @@ +using Godot; + +[GlobalClass] +public partial class EnemyDefinition : ActorDefinition { + [ExportCategory("AI")] + [Export] + public AIType AI; +} \ No newline at end of file diff --git a/scripts/actors/EnemyDefinition.cs.uid b/scripts/actors/EnemyDefinition.cs.uid new file mode 100644 index 0000000..1ba03e1 --- /dev/null +++ b/scripts/actors/EnemyDefinition.cs.uid @@ -0,0 +1 @@ +uid://dkfdm2m2scyks diff --git a/scripts/actors/actions/MovementAction.cs b/scripts/actors/actions/MovementAction.cs index 54bb99c..704dd4f 100644 --- a/scripts/actors/actions/MovementAction.cs +++ b/scripts/actors/actions/MovementAction.cs @@ -15,6 +15,8 @@ public partial class MovementAction : DirectionalAction if (GetBlockingActorAtPosition(finalDestination) != null) return; + GD.Print("What?"); + actor.Walk(Offset); } } diff --git a/scripts/map/DungeonGenerator.cs b/scripts/map/DungeonGenerator.cs index 9a2ceae..ebb7a3d 100644 --- a/scripts/map/DungeonGenerator.cs +++ b/scripts/map/DungeonGenerator.cs @@ -2,8 +2,8 @@ using Godot; public partial class DungeonGenerator : Node { - private static readonly Godot.Collections.Array enemies = [ - GD.Load("res://assets/definitions/actor/Skeleton.tres") + private static readonly Godot.Collections.Array enemies = [ + GD.Load("res://assets/definitions/actor/Skeleton.tres") ]; [ExportCategory("Dimension")] @@ -87,6 +87,7 @@ public partial class DungeonGenerator : Node PlaceEntities(data, room); } + data.SetupPathfinding(); return data; } @@ -108,7 +109,7 @@ public partial class DungeonGenerator : Node } if (canPlace) { - ActorDefinition definition = enemies.PickRandom(); + EnemyDefinition definition = enemies.PickRandom(); Enemy enemy = new Enemy(position, data, definition); data.InsertActor(enemy); } diff --git a/scripts/map/MapData.cs b/scripts/map/MapData.cs index ea18b4b..be466ba 100644 --- a/scripts/map/MapData.cs +++ b/scripts/map/MapData.cs @@ -15,6 +15,42 @@ public partial class MapData : RefCounted public Player Player { get; set; } public Godot.Collections.Array Actors { get; private set; } = []; + private AStarGrid2D pathfinder; + public AStarGrid2D Pathfinder { get => pathfinder; } + private static float ActorWeight = 10.0f; + + public void SetupPathfinding() { + pathfinder = new AStarGrid2D + { + Region = new Rect2I(0, 0, Width, Height) + }; + + pathfinder.Update(); + + for (int y = 0; y < Height; y++) { + for (int x = 0; x < Width; x++) { + Vector2I pos = new Vector2I(x, y); + Tile tile = GetTile(pos); + pathfinder.SetPointSolid(pos, !tile.IsWalkable); + } + } + + foreach (Actor actor in Actors) { + if (actor.BlocksMovement) { + RegisterBlockingActor(actor); + } + } + + } + + public void RegisterBlockingActor(Actor actor) { + pathfinder.SetPointWeightScale(actor.GridPosition, ActorWeight); + } + + public void UnregisterBlockingActor(Actor actor) { + pathfinder.SetPointWeightScale(actor.GridPosition, 0); + } + public MapData(int width, int height, Player player) { Width = width; Height = height; -- cgit v1.2.3