From b17531e504ac1e2d79323ae91ae827d14aa0c103 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Mon, 17 Jul 2023 22:58:15 +0200 Subject: [PATCH] Better heuristics to avoid full grid searches. --- GodotComponentTest.csproj | 4 ++++ HexGrid.cs | 11 +++++++++-- assets/Environment/HexTileMesh.tres | 2 +- project.godot | 8 ++++---- scenes/HexTile3D.cs | 6 ------ scenes/HexTile3D.tscn | 5 ++--- scenes/TileWorld.cs | 5 +++-- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/GodotComponentTest.csproj b/GodotComponentTest.csproj index a7bd7b6..992788b 100644 --- a/GodotComponentTest.csproj +++ b/GodotComponentTest.csproj @@ -35,4 +35,8 @@ + + + + diff --git a/HexGrid.cs b/HexGrid.cs index 439001e..a11d3da 100644 --- a/HexGrid.cs +++ b/HexGrid.cs @@ -28,6 +28,7 @@ public class HexGrid : Resource public float PathCostDefault = 1; + public int FindPathCheckedCells = 0; public Vector2 HexSize { @@ -90,7 +91,7 @@ public class HexGrid : Resource { _minCoords = minCell; _maxCoords = maxCell; - _bounds = new Rect2(_minCoords.AxialCoords, (_maxCoords.AxialCoords - _minCoords.AxialCoords) + Vector2.One); + _bounds = new Rect2(_minCoords.OffsetCoords, (_maxCoords.OffsetCoords - _minCoords.OffsetCoords) + Vector2.One); } public void AddObstacle(Vector2 axialCoords, float cost = 0) @@ -143,7 +144,8 @@ public class HexGrid : Resource public float GetHexCost(Vector2 axialCoords) { - if (!_bounds.HasPoint(axialCoords)) + HexCell cell = new HexCell(axialCoords); + if (!_bounds.HasPoint(cell.OffsetCoords)) { return 0; } @@ -210,8 +212,11 @@ public class HexGrid : Resource cameFrom.Add(startHex.AxialCoords, startHex.AxialCoords); costSoFar.Add(startHex.AxialCoords, 0); + FindPathCheckedCells = 0; + while (frontier.Any()) { + FindPathCheckedCells++; HexCell currentHex = new HexCell(frontier.Dequeue()); Vector2 currentAxial = currentHex.AxialCoords; if (currentHex == goalHex) @@ -249,6 +254,8 @@ public class HexGrid : Resource } } + // GD.Print("Checked Cell Count: " + FindPathCheckedCells); + if (!cameFrom.ContainsKey(goalHex.AxialCoords)) { return new List(); diff --git a/assets/Environment/HexTileMesh.tres b/assets/Environment/HexTileMesh.tres index 5e73caa..9e40a9b 100644 --- a/assets/Environment/HexTileMesh.tres +++ b/assets/Environment/HexTileMesh.tres @@ -6,6 +6,6 @@ material = ExtResource( 1 ) top_radius = 0.5 bottom_radius = 0.5 -height = 10.0 +height = 5.0 radial_segments = 6 rings = 1 diff --git a/project.godot b/project.godot index cd4dd44..f9b6e9d 100644 --- a/project.godot +++ b/project.godot @@ -9,17 +9,17 @@ config_version=4 _global_script_classes=[ { -"base": "Node", +"base": "Reference", "class": "ClickableComponent", "language": "GDScript", "path": "res://components/ClickableComponent.gd" }, { -"base": "KinematicBody2D", +"base": "Reference", "class": "CollisionLine", "language": "GDScript", "path": "res://utils/CollisionLine.gd" }, { -"base": "Node", +"base": "Reference", "class": "ColorComponent", "language": "GDScript", "path": "res://components/ColorComponent.gd" @@ -54,7 +54,7 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://utils/SpringDamper.gd" }, { -"base": "Sprite", +"base": "Reference", "class": "TintedSpriteComponent", "language": "GDScript", "path": "res://components/TintedSpriteComponent.gd" diff --git a/scenes/HexTile3D.cs b/scenes/HexTile3D.cs index 7b97fe5..0c4462c 100644 --- a/scenes/HexTile3D.cs +++ b/scenes/HexTile3D.cs @@ -139,10 +139,4 @@ public class HexTile3D : Spatial IsMouseOver = false; _mesh.MaterialOverride = _previousMaterial; } - -// // Called every frame. 'delta' is the elapsed time since the previous frame. -// public override void _Process(float delta) -// { -// -// } } \ No newline at end of file diff --git a/scenes/HexTile3D.tscn b/scenes/HexTile3D.tscn index 00331d6..1be3f2d 100644 --- a/scenes/HexTile3D.tscn +++ b/scenes/HexTile3D.tscn @@ -19,18 +19,17 @@ flags_transparent = true albedo_color = Color( 0.968627, 0.882353, 0.529412, 0.501961 ) [node name="HexTile3D" type="Spatial"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0096302, 0, 0 ) script = ExtResource( 1 ) [node name="Mesh" type="MeshInstance" parent="." groups=["GameGeometry"]] -transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, -5, 0 ) +transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, -2.5, 0 ) mesh = ExtResource( 3 ) material/0 = ExtResource( 2 ) [node name="StaticBody" type="StaticBody" parent="."] [node name="CollisionShape" type="CollisionShape" parent="StaticBody"] -transform = Transform( -4.37114e-08, 0, 1, 0, 10, 0, -1, 0, -4.37114e-08, 0, -5, 0 ) +transform = Transform( -4.37114e-08, 0, 1, 0, 5, 0, -1, 0, -4.37114e-08, 0, -2.5, 0 ) shape = SubResource( 5 ) [node name="MeshInstance" type="MeshInstance" parent="StaticBody/CollisionShape" groups=["PhysicsGeometry"]] diff --git a/scenes/TileWorld.cs b/scenes/TileWorld.cs index 4dbb2d0..6788999 100644 --- a/scenes/TileWorld.cs +++ b/scenes/TileWorld.cs @@ -114,8 +114,9 @@ public class TileWorld : Spatial _halfSize = Mathf.RoundToInt((float)size) / 2; HexGrid.SetBounds( - HexGrid.GetHexAtOffset(new Vector2(-_halfSize, -_halfSize)), - HexGrid.GetHexAtOffset(new Vector2(_halfSize, _halfSize))); + TextureCoordToCell(new Vector2(0, 0)), + TextureCoordToCell(new Vector2(size, size)) + ); HexGrid.Obstacles.Clear(); HexGrid.Barriers.Clear();