Fixed tile masks, made path smoothing optional.

main
Martin Felis 2023-12-29 17:43:04 +01:00
parent a5a94d1b89
commit 06b808c10d
5 changed files with 31 additions and 19 deletions

View File

@ -8,6 +8,8 @@ using GodotComponentTest.utils;
/// <summary> /// <summary>
/// </summary> /// </summary>
public class NavigationComponent : Spatial { public class NavigationComponent : Spatial {
[Export] public bool PathSmoothing = false;
public World World { set; get; } public World World { set; get; }
public Vector3 CurrentGoalPositionWorld { get; private set; } = Vector3.Zero; public Vector3 CurrentGoalPositionWorld { get; private set; } = Vector3.Zero;
public float CurrentGoalAngleWorld { get; private set; } public float CurrentGoalAngleWorld { get; private set; }
@ -15,7 +17,7 @@ public class NavigationComponent : Spatial {
private NavigationPoint _currentGoal; private NavigationPoint _currentGoal;
private HexCell[] _path; private HexCell[] _path;
private List<NavigationPoint> _pathWorldNavigationPoints = new(); private List<NavigationPoint> _pathWorldNavigationPoints = new();
private List<NavigationPoint> _planningPathSmoothedWorldNavigationPoints = new(); private List<NavigationPoint> _activePathWorldNavigationPoints = new();
private List<NavigationPoint> _planningPathWorldNavigationPoints = new(); private List<NavigationPoint> _planningPathWorldNavigationPoints = new();
private List<NavigationPoint> _smoothedPathWorldNavigationPoints = new(); private List<NavigationPoint> _smoothedPathWorldNavigationPoints = new();
@ -46,7 +48,7 @@ public class NavigationComponent : Spatial {
GD.Print("Invalid starting point for FindPath(): returning empty path."); GD.Print("Invalid starting point for FindPath(): returning empty path.");
_planningPathWorldNavigationPoints = new List<NavigationPoint>(); _planningPathWorldNavigationPoints = new List<NavigationPoint>();
_planningPathWorldNavigationPoints.Add(new NavigationPoint(fromPositionWorld)); _planningPathWorldNavigationPoints.Add(new NavigationPoint(fromPositionWorld));
_planningPathSmoothedWorldNavigationPoints = _planningPathWorldNavigationPoints; _activePathWorldNavigationPoints = _planningPathWorldNavigationPoints;
return; return;
} }
@ -57,7 +59,7 @@ public class NavigationComponent : Spatial {
GD.Print("Invalid target point for FindPath(): returning empty path."); GD.Print("Invalid target point for FindPath(): returning empty path.");
_planningPathWorldNavigationPoints = new List<NavigationPoint>(); _planningPathWorldNavigationPoints = new List<NavigationPoint>();
_planningPathWorldNavigationPoints.Add(new NavigationPoint(fromPositionWorld)); _planningPathWorldNavigationPoints.Add(new NavigationPoint(fromPositionWorld));
_planningPathSmoothedWorldNavigationPoints = _planningPathWorldNavigationPoints; _activePathWorldNavigationPoints = _planningPathWorldNavigationPoints;
return; return;
} }
@ -79,11 +81,19 @@ public class NavigationComponent : Spatial {
} }
// Perform smoothing // Perform smoothing
_planningPathSmoothedWorldNavigationPoints = World.SmoothPath(entity, _planningPathWorldNavigationPoints); if (PathSmoothing)
{
_activePathWorldNavigationPoints = World.SmoothPath(entity, _planningPathWorldNavigationPoints);
}
else
{
_activePathWorldNavigationPoints = _planningPathWorldNavigationPoints;
}
// Ensure starting point is the current position // Ensure starting point is the current position
if (_planningPathSmoothedWorldNavigationPoints.Count > 0) { if (_activePathWorldNavigationPoints.Count > 0) {
_planningPathSmoothedWorldNavigationPoints[0] = new NavigationPoint(fromPositionWorld); _activePathWorldNavigationPoints[0] = new NavigationPoint(fromPositionWorld);
} }
} }
@ -91,7 +101,7 @@ public class NavigationComponent : Spatial {
FindPath(entity, fromPositionWorld, navigationPoint.WorldPosition); FindPath(entity, fromPositionWorld, navigationPoint.WorldPosition);
_planningPathWorldNavigationPoints[_planningPathWorldNavigationPoints.Count - 1] = navigationPoint; _planningPathWorldNavigationPoints[_planningPathWorldNavigationPoints.Count - 1] = navigationPoint;
_planningPathSmoothedWorldNavigationPoints[_planningPathSmoothedWorldNavigationPoints.Count - 1] = _activePathWorldNavigationPoints[_activePathWorldNavigationPoints.Count - 1] =
navigationPoint; navigationPoint;
} }
@ -277,7 +287,7 @@ public class NavigationComponent : Spatial {
} }
public void ActivatePlannedPath() { public void ActivatePlannedPath() {
_pathWorldNavigationPoints = _planningPathSmoothedWorldNavigationPoints; _pathWorldNavigationPoints = _activePathWorldNavigationPoints;
UpdateCurrentGoal(); UpdateCurrentGoal();
} }
@ -384,7 +394,7 @@ public class NavigationComponent : Spatial {
} }
previousPoint = parentNode.GlobalTranslation; previousPoint = parentNode.GlobalTranslation;
foreach (NavigationPoint point in _planningPathSmoothedWorldNavigationPoints) { foreach (NavigationPoint point in _activePathWorldNavigationPoints) {
debugGeometry.SetColor(new Color(1, 1, 0)); debugGeometry.SetColor(new Color(1, 1, 0));
debugGeometry.AddVertex(previousPoint + yOffset); debugGeometry.AddVertex(previousPoint + yOffset);
debugGeometry.AddVertex(point.WorldPosition + yOffset); debugGeometry.AddVertex(point.WorldPosition + yOffset);

View File

@ -4,9 +4,9 @@ using Godot;
public class Entity : KinematicBody { public class Entity : KinematicBody {
[Flags] [Flags]
public enum EntityMaskEnum { public enum EntityMaskEnum {
Obstacle = 1 << 1, Obstacle = 1 << 0,
Ground = 1 << 2, Ground = 1 << 1,
Water = 1 << 3 Water = 1 << 2
} }
[Export(PropertyHint.Flags, "Obstacle,Ground,Water")] [Export(PropertyHint.Flags, "Obstacle,Ground,Water")]

View File

@ -9,17 +9,17 @@
config_version=4 config_version=4
_global_script_classes=[ { _global_script_classes=[ {
"base": "Reference", "base": "Node",
"class": "ClickableComponent", "class": "ClickableComponent",
"language": "GDScript", "language": "GDScript",
"path": "res://components/ClickableComponent.gd" "path": "res://components/ClickableComponent.gd"
}, { }, {
"base": "Reference", "base": "KinematicBody2D",
"class": "CollisionLine", "class": "CollisionLine",
"language": "GDScript", "language": "GDScript",
"path": "res://utils/CollisionLine.gd" "path": "res://utils/CollisionLine.gd"
}, { }, {
"base": "Reference", "base": "Node",
"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": "Reference", "base": "Sprite",
"class": "TintedSpriteComponent", "class": "TintedSpriteComponent",
"language": "GDScript", "language": "GDScript",
"path": "res://components/TintedSpriteComponent.gd" "path": "res://components/TintedSpriteComponent.gd"

View File

@ -373,7 +373,6 @@ input_ray_pickable = false
[node name="Chest" parent="Entities" instance=ExtResource( 11 )] [node name="Chest" parent="Entities" instance=ExtResource( 11 )]
transform = Transform( -0.825665, 0, 0.56416, 0, 1, 0, -0.56416, 0, -0.825665, -3.27709, 0, 1.02593 ) transform = Transform( -0.825665, 0, 0.56416, 0, 1, 0, -0.56416, 0, -0.825665, -3.27709, 0, 1.02593 )
EntityMask = 3
[node name="World" parent="." instance=ExtResource( 7 )] [node name="World" parent="." instance=ExtResource( 7 )]

View File

@ -489,7 +489,10 @@ public class World : Spatial {
nextHexCost = 0; nextHexCost = 0;
} }
} }
// GD.Print("Could not find tile type info for color " + tileTypeColor); else
{
GD.Print("Could not find tile type info for color " + tileTypeColor);
}
} }
return nextHexCost; return nextHexCost;