diff options
| author | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-08-26 22:59:57 -0300 |
|---|---|---|
| committer | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-08-26 22:59:57 -0300 |
| commit | 6c7e2ac133986efa57b43df52a5498c6f7efcf75 (patch) | |
| tree | 8404bfd3415a8dfcf7073191cbfdf58cc7c905ed /scripts/map | |
| parent | a3103718796a472da76838bf6fd72ba5d8409d79 (diff) | |
AI
Diffstat (limited to 'scripts/map')
| -rw-r--r-- | scripts/map/DungeonGenerator.cs | 7 | ||||
| -rw-r--r-- | scripts/map/MapData.cs | 36 |
2 files changed, 40 insertions, 3 deletions
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<ActorDefinition> enemies = [ - GD.Load<ActorDefinition>("res://assets/definitions/actor/Skeleton.tres") + private static readonly Godot.Collections.Array<EnemyDefinition> enemies = [ + GD.Load<EnemyDefinition>("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<Actor> 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; |
