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

View File

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

View File

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

View File

@ -373,7 +373,6 @@ input_ray_pickable = false
[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 )
EntityMask = 3
[node name="World" parent="." instance=ExtResource( 7 )]

View File

@ -489,7 +489,10 @@ public class World : Spatial {
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;