Better heuristics to avoid full grid searches.

WorldChunkRefactoring
Martin Felis 2023-07-17 22:58:15 +02:00
parent 667403b334
commit b17531e504
7 changed files with 23 additions and 18 deletions

View File

@ -35,4 +35,8 @@
<Content Include="entities\WanderingEntity.tscn" /> <Content Include="entities\WanderingEntity.tscn" />
<Content Include="scenes\Tests\HexTile3DMaterialAssign.tscn" /> <Content Include="scenes\Tests\HexTile3DMaterialAssign.tscn" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Remove="tests\TileWorldTests.cs" />
</ItemGroup>
</Project> </Project>

View File

@ -28,6 +28,7 @@ public class HexGrid : Resource
public float PathCostDefault = 1; public float PathCostDefault = 1;
public int FindPathCheckedCells = 0;
public Vector2 HexSize public Vector2 HexSize
{ {
@ -90,7 +91,7 @@ public class HexGrid : Resource
{ {
_minCoords = minCell; _minCoords = minCell;
_maxCoords = maxCell; _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) public void AddObstacle(Vector2 axialCoords, float cost = 0)
@ -143,7 +144,8 @@ public class HexGrid : Resource
public float GetHexCost(Vector2 axialCoords) public float GetHexCost(Vector2 axialCoords)
{ {
if (!_bounds.HasPoint(axialCoords)) HexCell cell = new HexCell(axialCoords);
if (!_bounds.HasPoint(cell.OffsetCoords))
{ {
return 0; return 0;
} }
@ -210,8 +212,11 @@ public class HexGrid : Resource
cameFrom.Add(startHex.AxialCoords, startHex.AxialCoords); cameFrom.Add(startHex.AxialCoords, startHex.AxialCoords);
costSoFar.Add(startHex.AxialCoords, 0); costSoFar.Add(startHex.AxialCoords, 0);
FindPathCheckedCells = 0;
while (frontier.Any()) while (frontier.Any())
{ {
FindPathCheckedCells++;
HexCell currentHex = new HexCell(frontier.Dequeue()); HexCell currentHex = new HexCell(frontier.Dequeue());
Vector2 currentAxial = currentHex.AxialCoords; Vector2 currentAxial = currentHex.AxialCoords;
if (currentHex == goalHex) if (currentHex == goalHex)
@ -249,6 +254,8 @@ public class HexGrid : Resource
} }
} }
// GD.Print("Checked Cell Count: " + FindPathCheckedCells);
if (!cameFrom.ContainsKey(goalHex.AxialCoords)) if (!cameFrom.ContainsKey(goalHex.AxialCoords))
{ {
return new List<HexCell>(); return new List<HexCell>();

View File

@ -6,6 +6,6 @@
material = ExtResource( 1 ) material = ExtResource( 1 )
top_radius = 0.5 top_radius = 0.5
bottom_radius = 0.5 bottom_radius = 0.5
height = 10.0 height = 5.0
radial_segments = 6 radial_segments = 6
rings = 1 rings = 1

View File

@ -9,17 +9,17 @@
config_version=4 config_version=4
_global_script_classes=[ { _global_script_classes=[ {
"base": "Node", "base": "Reference",
"class": "ClickableComponent", "class": "ClickableComponent",
"language": "GDScript", "language": "GDScript",
"path": "res://components/ClickableComponent.gd" "path": "res://components/ClickableComponent.gd"
}, { }, {
"base": "KinematicBody2D", "base": "Reference",
"class": "CollisionLine", "class": "CollisionLine",
"language": "GDScript", "language": "GDScript",
"path": "res://utils/CollisionLine.gd" "path": "res://utils/CollisionLine.gd"
}, { }, {
"base": "Node", "base": "Reference",
"class": "ColorComponent", "class": "ColorComponent",
"language": "GDScript", "language": "GDScript",
"path": "res://components/ColorComponent.gd" "path": "res://components/ColorComponent.gd"
@ -54,7 +54,7 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://utils/SpringDamper.gd" "path": "res://utils/SpringDamper.gd"
}, { }, {
"base": "Sprite", "base": "Reference",
"class": "TintedSpriteComponent", "class": "TintedSpriteComponent",
"language": "GDScript", "language": "GDScript",
"path": "res://components/TintedSpriteComponent.gd" "path": "res://components/TintedSpriteComponent.gd"

View File

@ -139,10 +139,4 @@ public class HexTile3D : Spatial
IsMouseOver = false; IsMouseOver = false;
_mesh.MaterialOverride = _previousMaterial; _mesh.MaterialOverride = _previousMaterial;
} }
// // Called every frame. 'delta' is the elapsed time since the previous frame.
// public override void _Process(float delta)
// {
//
// }
} }

View File

@ -19,18 +19,17 @@ flags_transparent = true
albedo_color = Color( 0.968627, 0.882353, 0.529412, 0.501961 ) albedo_color = Color( 0.968627, 0.882353, 0.529412, 0.501961 )
[node name="HexTile3D" type="Spatial"] [node name="HexTile3D" type="Spatial"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0096302, 0, 0 )
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Mesh" type="MeshInstance" parent="." groups=["GameGeometry"]] [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 ) mesh = ExtResource( 3 )
material/0 = ExtResource( 2 ) material/0 = ExtResource( 2 )
[node name="StaticBody" type="StaticBody" parent="."] [node name="StaticBody" type="StaticBody" parent="."]
[node name="CollisionShape" type="CollisionShape" parent="StaticBody"] [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 ) shape = SubResource( 5 )
[node name="MeshInstance" type="MeshInstance" parent="StaticBody/CollisionShape" groups=["PhysicsGeometry"]] [node name="MeshInstance" type="MeshInstance" parent="StaticBody/CollisionShape" groups=["PhysicsGeometry"]]

View File

@ -114,8 +114,9 @@ public class TileWorld : Spatial
_halfSize = Mathf.RoundToInt((float)size) / 2; _halfSize = Mathf.RoundToInt((float)size) / 2;
HexGrid.SetBounds( HexGrid.SetBounds(
HexGrid.GetHexAtOffset(new Vector2(-_halfSize, -_halfSize)), TextureCoordToCell(new Vector2(0, 0)),
HexGrid.GetHexAtOffset(new Vector2(_halfSize, _halfSize))); TextureCoordToCell(new Vector2(size, size))
);
HexGrid.Obstacles.Clear(); HexGrid.Obstacles.Clear();
HexGrid.Barriers.Clear(); HexGrid.Barriers.Clear();