summaryrefslogtreecommitdiff
path: root/scripts/map
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/map')
-rw-r--r--scripts/map/DungeonGenerator.cs7
-rw-r--r--scripts/map/MapData.cs36
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;