2023-01-03 17:46:55 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2023-01-04 21:29:42 +01:00
|
|
|
using System.Diagnostics;
|
2023-01-03 17:46:55 +01:00
|
|
|
using System.Linq;
|
|
|
|
using Godot;
|
2023-07-07 17:27:05 +02:00
|
|
|
using GodotComponentTest.utils;
|
2023-01-03 17:46:55 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// </summary>
|
2023-11-18 22:11:34 +01:00
|
|
|
public class NavigationComponent : Spatial {
|
2023-12-29 17:43:04 +01:00
|
|
|
[Export] public bool PathSmoothing = false;
|
|
|
|
|
2023-11-10 11:11:08 +01:00
|
|
|
public World World { set; get; }
|
|
|
|
public Vector3 CurrentGoalPositionWorld { get; private set; } = Vector3.Zero;
|
2023-11-10 12:22:17 +01:00
|
|
|
public float CurrentGoalAngleWorld { get; private set; }
|
2023-02-12 16:30:56 +01:00
|
|
|
|
|
|
|
private NavigationPoint _currentGoal;
|
2023-01-04 21:29:42 +01:00
|
|
|
private HexCell[] _path;
|
2023-11-10 11:11:08 +01:00
|
|
|
private List<NavigationPoint> _pathWorldNavigationPoints = new();
|
2023-12-29 17:43:04 +01:00
|
|
|
private List<NavigationPoint> _activePathWorldNavigationPoints = new();
|
2023-11-10 11:11:08 +01:00
|
|
|
|
|
|
|
private List<NavigationPoint> _planningPathWorldNavigationPoints = new();
|
|
|
|
private List<NavigationPoint> _smoothedPathWorldNavigationPoints = new();
|
2023-01-04 21:29:42 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public override void _Ready() {
|
2023-01-04 21:29:42 +01:00
|
|
|
base._Ready();
|
2023-02-12 16:30:56 +01:00
|
|
|
_pathWorldNavigationPoints = new List<NavigationPoint>();
|
2023-01-04 21:29:42 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public override void _Process(float delta) {
|
2023-11-10 11:11:08 +01:00
|
|
|
Debug.Assert(World != null);
|
2023-01-04 21:29:42 +01:00
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void PlanSmoothedPath(Entity body, Transform fromTransformWorld, NavigationPoint navigationPoint) {
|
2023-08-28 18:22:28 +02:00
|
|
|
if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position)
|
2023-11-18 22:11:34 +01:00
|
|
|
&& navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Orientation)) {
|
2023-08-28 18:22:28 +02:00
|
|
|
FindPath(body, fromTransformWorld.origin, navigationPoint);
|
2023-11-18 22:11:34 +01:00
|
|
|
} else if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position)) {
|
2023-08-28 18:22:28 +02:00
|
|
|
FindPath(body, fromTransformWorld.origin, navigationPoint.WorldPosition);
|
2023-11-18 22:11:34 +01:00
|
|
|
} else {
|
2023-08-28 18:22:28 +02:00
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 21:29:42 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void FindPath(Entity entity, Vector3 fromPositionWorld, Vector3 toPositionWorld) {
|
|
|
|
HexCell fromCell = World.HexGrid.GetHexAt(new Vector2(fromPositionWorld.x, fromPositionWorld.z));
|
|
|
|
if (World.HexGrid.GetHexCost(fromCell) == 0) {
|
2023-07-28 22:14:28 +02:00
|
|
|
GD.Print("Invalid starting point for FindPath(): returning empty path.");
|
|
|
|
_planningPathWorldNavigationPoints = new List<NavigationPoint>();
|
|
|
|
_planningPathWorldNavigationPoints.Add(new NavigationPoint(fromPositionWorld));
|
2023-12-29 17:43:04 +01:00
|
|
|
_activePathWorldNavigationPoints = _planningPathWorldNavigationPoints;
|
2023-07-28 22:14:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
HexCell toCell = World.HexGrid.GetHexAt(new Vector2(toPositionWorld.x, toPositionWorld.z));
|
2023-11-10 11:11:08 +01:00
|
|
|
toCell = World.HexGrid.GetClosestWalkableCell(fromCell, toCell);
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (World.HexGrid.GetHexCost(toCell) == 0) {
|
2023-07-28 22:14:28 +02:00
|
|
|
GD.Print("Invalid target point for FindPath(): returning empty path.");
|
|
|
|
_planningPathWorldNavigationPoints = new List<NavigationPoint>();
|
|
|
|
_planningPathWorldNavigationPoints.Add(new NavigationPoint(fromPositionWorld));
|
2023-12-29 17:43:04 +01:00
|
|
|
_activePathWorldNavigationPoints = _planningPathWorldNavigationPoints;
|
2023-07-28 22:14:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
List<HexCell> path = World.FindPath(entity, fromCell, toCell);
|
2023-08-28 18:22:28 +02:00
|
|
|
|
|
|
|
// Generate grid navigation points
|
2023-07-16 20:38:39 +02:00
|
|
|
_planningPathWorldNavigationPoints = new List<NavigationPoint>();
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (int index in Enumerable.Range(0, path.Count)) {
|
2023-07-16 20:38:39 +02:00
|
|
|
_planningPathWorldNavigationPoints.Add(
|
2023-11-10 11:11:08 +01:00
|
|
|
new NavigationPoint(World.HexGrid.GetHexCenterVec3FromOffset(path[index].OffsetCoords)));
|
2023-07-16 20:38:39 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 18:22:28 +02:00
|
|
|
// Ensure the last point coincides with the target position
|
2023-11-10 11:11:08 +01:00
|
|
|
if (_planningPathWorldNavigationPoints.Count > 0 &&
|
|
|
|
(_planningPathWorldNavigationPoints.Last().WorldPosition - toPositionWorld).LengthSquared() <
|
2023-11-18 22:11:34 +01:00
|
|
|
0.5f * 0.5f) {
|
2023-11-10 11:11:08 +01:00
|
|
|
_planningPathWorldNavigationPoints[_planningPathWorldNavigationPoints.Count - 1].WorldPosition =
|
|
|
|
toPositionWorld;
|
2023-08-28 18:22:28 +02:00
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-08-28 18:22:28 +02:00
|
|
|
// Perform smoothing
|
2023-12-29 17:43:04 +01:00
|
|
|
if (PathSmoothing)
|
|
|
|
{
|
|
|
|
_activePathWorldNavigationPoints = World.SmoothPath(entity, _planningPathWorldNavigationPoints);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_activePathWorldNavigationPoints = _planningPathWorldNavigationPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-01 12:34:28 +01:00
|
|
|
// Ensure starting point is the current position
|
2023-12-29 17:43:04 +01:00
|
|
|
if (_activePathWorldNavigationPoints.Count > 0) {
|
|
|
|
_activePathWorldNavigationPoints[0] = new NavigationPoint(fromPositionWorld);
|
2023-11-01 12:34:28 +01:00
|
|
|
}
|
2023-07-16 20:38:39 +02:00
|
|
|
}
|
2023-01-04 21:29:42 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void FindPath(Entity entity, Vector3 fromPositionWorld, NavigationPoint navigationPoint) {
|
|
|
|
FindPath(entity, fromPositionWorld, navigationPoint.WorldPosition);
|
2023-08-28 18:22:28 +02:00
|
|
|
|
2023-12-14 17:27:07 +01:00
|
|
|
_planningPathWorldNavigationPoints[_planningPathWorldNavigationPoints.Count - 1] = navigationPoint;
|
2023-12-29 17:43:04 +01:00
|
|
|
_activePathWorldNavigationPoints[_activePathWorldNavigationPoints.Count - 1] =
|
2023-12-14 17:27:07 +01:00
|
|
|
navigationPoint;
|
2023-08-28 18:22:28 +02:00
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void PlanGridPath(Entity entity, Vector3 fromPositionWorld, Vector3 toPositionWorld) {
|
|
|
|
Vector2 fromPositionOffset = World.WorldToOffsetCoords(fromPositionWorld);
|
|
|
|
Vector2 toPositionOffset = World.WorldToOffsetCoords(toPositionWorld);
|
2023-01-04 21:29:42 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
HexCell fromCell = new();
|
2023-01-04 21:29:42 +01:00
|
|
|
fromCell.OffsetCoords = fromPositionOffset;
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
HexCell toCell = new();
|
2023-01-04 21:29:42 +01:00
|
|
|
toCell.OffsetCoords = toPositionOffset;
|
|
|
|
|
|
|
|
_path = fromCell.LineTo(toCell);
|
|
|
|
Debug.Assert(_path.Length > 0);
|
|
|
|
|
2023-02-12 16:30:56 +01:00
|
|
|
_pathWorldNavigationPoints = new List<NavigationPoint>();
|
2023-07-09 22:17:55 +02:00
|
|
|
_pathWorldNavigationPoints.Add(
|
2023-11-10 11:11:08 +01:00
|
|
|
new NavigationPoint(World.HexGrid.GetHexCenterVec3FromOffset(fromPositionOffset)));
|
2023-07-15 00:39:38 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (int index in Enumerable.Range(1, _path.Length - 1)) {
|
2023-02-12 16:30:56 +01:00
|
|
|
_pathWorldNavigationPoints.Add(
|
2023-11-10 11:11:08 +01:00
|
|
|
new NavigationPoint(World.GetHexCenterFromOffset(_path[index].OffsetCoords)));
|
2023-02-12 16:30:56 +01:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:11:08 +01:00
|
|
|
if ((fromPositionWorld - World.GetHexCenterFromOffset(toCell.OffsetCoords)).LengthSquared() >
|
2023-02-12 16:30:56 +01:00
|
|
|
Globals.EpsPositionSquared)
|
2023-02-12 16:44:48 +01:00
|
|
|
// Remove the last one, because it is only the position rounded to HexGrid coordinates.
|
2023-11-18 22:11:34 +01:00
|
|
|
{
|
|
|
|
if (_pathWorldNavigationPoints.Count > 0) {
|
2023-02-12 16:44:48 +01:00
|
|
|
_pathWorldNavigationPoints.RemoveAt(_pathWorldNavigationPoints.Count - 1);
|
|
|
|
}
|
2023-01-04 21:29:42 +01:00
|
|
|
}
|
|
|
|
|
2023-05-03 12:01:02 +02:00
|
|
|
_pathWorldNavigationPoints.Add(new NavigationPoint(toPositionWorld));
|
2023-11-18 22:11:34 +01:00
|
|
|
if (_pathWorldNavigationPoints.Count > 2) {
|
|
|
|
_smoothedPathWorldNavigationPoints = SmoothPath(entity, _pathWorldNavigationPoints);
|
2023-07-09 22:17:55 +02:00
|
|
|
_pathWorldNavigationPoints = _smoothedPathWorldNavigationPoints;
|
|
|
|
}
|
2023-06-29 23:07:29 +02:00
|
|
|
|
2023-01-04 21:29:42 +01:00
|
|
|
UpdateCurrentGoal();
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:30:56 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void PlanGridPath(Entity entity, Vector3 fromPositionWorld, Vector3 toPositionWorld,
|
|
|
|
Quat toWorldOrientation) {
|
|
|
|
PlanGridPath(entity, fromPositionWorld, toPositionWorld);
|
2023-05-03 12:10:01 +02:00
|
|
|
|
|
|
|
_pathWorldNavigationPoints.Add(new NavigationPoint(toWorldOrientation));
|
|
|
|
}
|
2023-06-29 23:07:29 +02:00
|
|
|
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void PlanGridPath(Entity entity, Transform fromTransformWorld, NavigationPoint navigationPoint) {
|
2023-05-03 12:10:01 +02:00
|
|
|
if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position)
|
2023-11-18 22:11:34 +01:00
|
|
|
&& navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Orientation)) {
|
|
|
|
PlanGridPath(entity, fromTransformWorld.origin, navigationPoint.WorldPosition,
|
2023-07-15 00:39:38 +02:00
|
|
|
navigationPoint.WorldOrientation);
|
2023-11-18 22:11:34 +01:00
|
|
|
} else if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position)) {
|
|
|
|
PlanGridPath(entity, fromTransformWorld.origin, navigationPoint.WorldPosition);
|
|
|
|
} else {
|
2023-05-03 12:10:01 +02:00
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 23:07:29 +02:00
|
|
|
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void PlanDirectPath(KinematicBody body, Vector3 fromPositionWorld, Vector3 toPositionWorld) {
|
2023-05-03 12:10:01 +02:00
|
|
|
_pathWorldNavigationPoints.Clear();
|
|
|
|
_pathWorldNavigationPoints.Add(new NavigationPoint(toPositionWorld));
|
2023-06-29 23:07:29 +02:00
|
|
|
|
2023-05-03 12:10:01 +02:00
|
|
|
UpdateCurrentGoal();
|
|
|
|
}
|
|
|
|
|
2023-06-29 23:07:29 +02:00
|
|
|
|
2023-07-15 00:39:38 +02:00
|
|
|
public void PlanDirectPath(KinematicBody body, Vector3 fromPositionWorld, Vector3 toPositionWorld,
|
2023-11-18 22:11:34 +01:00
|
|
|
Quat toWorldOrientation) {
|
2023-07-09 22:17:55 +02:00
|
|
|
PlanDirectPath(body, fromPositionWorld, toPositionWorld);
|
2023-02-12 16:30:56 +01:00
|
|
|
|
|
|
|
_pathWorldNavigationPoints.Add(new NavigationPoint(toWorldOrientation));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public bool HasPathCollision(KinematicBody body, Vector3 fromPositionWorld, Vector3 toPositionWorld) {
|
2023-07-09 00:16:37 +02:00
|
|
|
Vector3 fromPositionLocal = GlobalTransform.XformInv(fromPositionWorld);
|
|
|
|
Vector3 toPositionLocal = GlobalTransform.XformInv(toPositionWorld);
|
2023-07-15 00:39:38 +02:00
|
|
|
Vector3 relativeVelocity = GlobalTransform.basis.Xform(toPositionLocal - fromPositionLocal);
|
2023-07-09 00:16:37 +02:00
|
|
|
|
2023-07-15 00:39:38 +02:00
|
|
|
KinematicCollision moveCollision = body.MoveAndCollide(relativeVelocity, true, true, true);
|
2023-11-18 22:11:34 +01:00
|
|
|
if (moveCollision != null) {
|
2023-07-15 00:39:38 +02:00
|
|
|
Spatial colliderSpatial = moveCollision.Collider as Spatial;
|
2023-07-16 20:38:39 +02:00
|
|
|
// GD.Print("Found collision: " + moveCollision.Collider + " (" + colliderSpatial.Name + ")");
|
2023-07-09 00:16:37 +02:00
|
|
|
return true;
|
2023-07-01 09:50:41 +02:00
|
|
|
}
|
2023-07-09 22:17:55 +02:00
|
|
|
|
|
|
|
return false;
|
2023-07-01 09:50:41 +02:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public bool CheckSweptTriangleCellCollision(Vector3 startWorld, Vector3 endWorld, float radius) {
|
|
|
|
Vector2 startPlane = new(startWorld.x, startWorld.z);
|
|
|
|
Vector2 endPlane = new(endWorld.x, endWorld.z);
|
2023-08-13 22:03:10 +02:00
|
|
|
Vector2 directionPlane = (endPlane - startPlane).Normalized();
|
|
|
|
Vector2 sidePlane = directionPlane.Rotated(Mathf.Pi * 0.5f);
|
|
|
|
|
2023-11-10 11:11:08 +01:00
|
|
|
List<HexCell> cells =
|
|
|
|
World.HexGrid.GetCellsForLine(startPlane + directionPlane * radius, endPlane + directionPlane * radius);
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (HexCell cell in cells) {
|
|
|
|
if (World.HexGrid.GetHexCost(cell) == 0) {
|
2023-08-13 22:03:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
|
|
|
cells = World.HexGrid.GetCellsForLine(startPlane + sidePlane * radius, endPlane + sidePlane * radius);
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (HexCell cell in cells) {
|
|
|
|
if (World.HexGrid.GetHexCost(cell) == 0) {
|
2023-08-13 22:03:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
|
|
|
cells = World.HexGrid.GetCellsForLine(startPlane - sidePlane * radius, endPlane - sidePlane * radius);
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (HexCell cell in cells) {
|
|
|
|
if (World.HexGrid.GetHexCost(cell) == 0) {
|
2023-08-13 22:03:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-08-13 22:03:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public List<NavigationPoint> SmoothPath(KinematicBody body, List<NavigationPoint> navigationPoints) {
|
|
|
|
if (navigationPoints.Count <= 2) {
|
2023-07-16 20:38:39 +02:00
|
|
|
return navigationPoints;
|
|
|
|
}
|
2023-07-14 17:56:01 +02:00
|
|
|
|
|
|
|
Vector3 bodyGlobalTranslation = body.GlobalTranslation;
|
2023-11-18 22:11:34 +01:00
|
|
|
List<NavigationPoint> smoothedPath = new();
|
2023-07-15 00:39:38 +02:00
|
|
|
|
2023-07-01 09:50:41 +02:00
|
|
|
int startIndex = 0;
|
2023-07-09 22:17:55 +02:00
|
|
|
int endIndex = navigationPoints.Count > 1 ? 1 : 0;
|
2023-07-01 09:50:41 +02:00
|
|
|
smoothedPath.Add(navigationPoints[startIndex]);
|
2023-07-14 17:56:01 +02:00
|
|
|
Vector3 startPoint = navigationPoints[startIndex].WorldPosition;
|
2023-07-15 00:39:38 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
while (endIndex != navigationPoints.Count) {
|
2023-07-01 09:50:41 +02:00
|
|
|
Vector3 endPoint = navigationPoints[endIndex].WorldPosition;
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (CheckSweptTriangleCellCollision(startPoint, endPoint, 0.27f)) {
|
|
|
|
if (endIndex - startIndex == 1) {
|
2023-07-14 17:56:01 +02:00
|
|
|
GD.Print("Aborting SmoothPath: input path passes through collision geometry.");
|
|
|
|
body.GlobalTranslation = bodyGlobalTranslation;
|
|
|
|
return smoothedPath;
|
2023-07-09 22:17:55 +02:00
|
|
|
}
|
2023-07-14 17:56:01 +02:00
|
|
|
|
2023-07-15 00:39:38 +02:00
|
|
|
smoothedPath.Add(navigationPoints[endIndex - 1]);
|
|
|
|
startIndex = endIndex - 1;
|
|
|
|
startPoint = navigationPoints[startIndex].WorldPosition;
|
|
|
|
body.GlobalTranslation = startPoint;
|
|
|
|
|
|
|
|
continue;
|
2023-07-01 09:50:41 +02:00
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (endIndex == navigationPoints.Count - 1) {
|
2023-07-15 00:39:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:17:55 +02:00
|
|
|
endIndex += 1;
|
2023-07-01 09:50:41 +02:00
|
|
|
}
|
2023-07-15 00:39:38 +02:00
|
|
|
|
2023-07-01 09:50:41 +02:00
|
|
|
smoothedPath.Add(navigationPoints[endIndex]);
|
2023-07-14 17:56:01 +02:00
|
|
|
body.GlobalTranslation = bodyGlobalTranslation;
|
2023-07-15 00:39:38 +02:00
|
|
|
|
2023-07-01 09:50:41 +02:00
|
|
|
return smoothedPath;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void PlanDirectPath(KinematicBody body, Transform fromTransformWorld, NavigationPoint navigationPoint) {
|
2023-02-12 21:10:28 +01:00
|
|
|
if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position)
|
2023-11-18 22:11:34 +01:00
|
|
|
&& navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Orientation)) {
|
2023-07-15 00:39:38 +02:00
|
|
|
PlanDirectPath(body, fromTransformWorld.origin, navigationPoint.WorldPosition,
|
|
|
|
navigationPoint.WorldOrientation);
|
2023-11-18 22:11:34 +01:00
|
|
|
} else if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position)) {
|
2023-07-09 22:17:55 +02:00
|
|
|
PlanDirectPath(body, fromTransformWorld.origin, navigationPoint.WorldPosition);
|
2023-11-18 22:11:34 +01:00
|
|
|
} else {
|
2023-02-12 21:10:28 +01:00
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void ActivatePlannedPath() {
|
2023-12-29 17:43:04 +01:00
|
|
|
_pathWorldNavigationPoints = _activePathWorldNavigationPoints;
|
2023-07-16 20:38:39 +02:00
|
|
|
UpdateCurrentGoal();
|
|
|
|
}
|
2023-02-12 21:10:28 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void UpdateCurrentGoal() {
|
|
|
|
if (_pathWorldNavigationPoints.Count == 0) {
|
2023-01-03 17:46:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:30:56 +01:00
|
|
|
_currentGoal = _pathWorldNavigationPoints[0];
|
2023-11-10 11:11:08 +01:00
|
|
|
CurrentGoalPositionWorld = _pathWorldNavigationPoints[0].WorldPosition;
|
2023-11-10 12:22:17 +01:00
|
|
|
CurrentGoalAngleWorld = _pathWorldNavigationPoints[0].WorldAngle;
|
2023-01-04 21:29:42 +01:00
|
|
|
}
|
2023-01-03 17:46:55 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void ApplyExistingTransform(Transform worldTransform) {
|
|
|
|
if (_currentGoal.Flags == NavigationPoint.NavigationFlags.Orientation) {
|
2023-11-10 11:11:08 +01:00
|
|
|
CurrentGoalPositionWorld = worldTransform.origin;
|
2023-11-18 22:11:34 +01:00
|
|
|
} else if (_currentGoal.Flags == NavigationPoint.NavigationFlags.Position) {
|
2023-11-10 12:22:17 +01:00
|
|
|
CurrentGoalAngleWorld = Globals.CalcPlaneAngle(worldTransform);
|
2023-05-01 18:37:35 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-03 17:46:55 +01:00
|
|
|
|
2023-06-29 23:07:29 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void UpdateCurrentGoal(Transform currentTransformWorld) {
|
|
|
|
if (_currentGoal == null) {
|
2023-07-16 20:38:39 +02:00
|
|
|
_currentGoal = new NavigationPoint(currentTransformWorld);
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (_pathWorldNavigationPoints.Count == 0) {
|
2023-11-10 12:22:17 +01:00
|
|
|
CurrentGoalAngleWorld = Globals.CalcPlaneAngle(currentTransformWorld);
|
2023-11-10 11:11:08 +01:00
|
|
|
CurrentGoalPositionWorld = currentTransformWorld.origin;
|
2023-02-12 16:30:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (_currentGoal.Flags.HasFlag(NavigationPoint.NavigationFlags.Position)) {
|
2023-11-10 11:11:08 +01:00
|
|
|
CurrentGoalPositionWorld = _pathWorldNavigationPoints[0].WorldPosition;
|
2023-11-18 22:11:34 +01:00
|
|
|
} else {
|
2023-11-10 12:22:17 +01:00
|
|
|
CurrentGoalAngleWorld = Globals.CalcPlaneAngle(currentTransformWorld);
|
2023-02-12 16:30:56 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (_currentGoal.Flags.HasFlag(NavigationPoint.NavigationFlags.Orientation)) {
|
2023-11-10 12:22:17 +01:00
|
|
|
CurrentGoalAngleWorld = _currentGoal.WorldAngle;
|
2023-11-18 22:11:34 +01:00
|
|
|
} else {
|
2023-11-10 12:22:17 +01:00
|
|
|
CurrentGoalAngleWorld = Globals.CalcPlaneAngle(currentTransformWorld);
|
2023-02-12 16:30:56 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (_currentGoal.IsReached(currentTransformWorld)) {
|
2023-02-12 16:30:56 +01:00
|
|
|
_pathWorldNavigationPoints.RemoveAt(0);
|
2023-06-29 23:07:29 +02:00
|
|
|
|
2023-01-04 21:29:42 +01:00
|
|
|
UpdateCurrentGoal();
|
2023-05-01 18:37:35 +02:00
|
|
|
ApplyExistingTransform(currentTransformWorld);
|
2023-01-03 17:46:55 +01:00
|
|
|
}
|
2023-02-15 20:59:22 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (_pathWorldNavigationPoints.Count == 0) {
|
2023-11-10 11:11:08 +01:00
|
|
|
CurrentGoalPositionWorld = currentTransformWorld.origin;
|
2023-11-10 12:22:17 +01:00
|
|
|
CurrentGoalAngleWorld = Globals.CalcPlaneAngle(currentTransformWorld);
|
2023-02-15 20:59:22 +01:00
|
|
|
}
|
2023-01-03 17:46:55 +01:00
|
|
|
}
|
2023-07-07 17:27:05 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public bool IsGoalReached() {
|
2023-11-10 11:11:08 +01:00
|
|
|
return _pathWorldNavigationPoints.Count == 0;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void DebugDraw(Spatial parentNode, DebugGeometry debugGeometry) {
|
2023-07-07 17:27:05 +02:00
|
|
|
Vector3 yOffset = Vector3.Up * 0.1f;
|
|
|
|
|
|
|
|
debugGeometry.GlobalTransform = Transform.Identity;
|
|
|
|
debugGeometry.Begin(Mesh.PrimitiveType.Lines);
|
2023-07-14 17:56:01 +02:00
|
|
|
Color pinkColor = Colors.Pink;
|
|
|
|
pinkColor.a = 1;
|
|
|
|
debugGeometry.SetColor(pinkColor);
|
2023-07-07 17:27:05 +02:00
|
|
|
debugGeometry.AddVertex(parentNode.GlobalTranslation + yOffset);
|
2023-07-14 17:56:01 +02:00
|
|
|
debugGeometry.SetColor(pinkColor);
|
2023-07-07 17:27:05 +02:00
|
|
|
debugGeometry.AddVertex(CurrentGoalPositionWorld + yOffset);
|
2023-07-14 17:56:01 +02:00
|
|
|
debugGeometry.SetColor(pinkColor);
|
2023-07-07 17:27:05 +02:00
|
|
|
|
|
|
|
debugGeometry.PushTranslated(CurrentGoalPositionWorld);
|
|
|
|
debugGeometry.AddBox(Vector3.One * 1);
|
|
|
|
debugGeometry.PopTransform();
|
|
|
|
|
|
|
|
Vector3 previousPoint = parentNode.GlobalTranslation;
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (NavigationPoint point in _pathWorldNavigationPoints) {
|
2023-07-07 17:27:05 +02:00
|
|
|
debugGeometry.AddVertex(previousPoint + yOffset);
|
|
|
|
debugGeometry.AddVertex(point.WorldPosition + yOffset);
|
|
|
|
|
|
|
|
previousPoint = point.WorldPosition;
|
|
|
|
}
|
2023-07-15 00:39:38 +02:00
|
|
|
|
2023-07-09 22:17:55 +02:00
|
|
|
previousPoint = parentNode.GlobalTranslation;
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (NavigationPoint point in _smoothedPathWorldNavigationPoints) {
|
2023-08-13 21:20:45 +02:00
|
|
|
debugGeometry.SetColor(new Color(0, 0, 1));
|
2023-07-09 22:17:55 +02:00
|
|
|
debugGeometry.AddVertex(previousPoint + yOffset);
|
|
|
|
debugGeometry.AddVertex(point.WorldPosition + yOffset);
|
|
|
|
|
|
|
|
previousPoint = point.WorldPosition;
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-07-16 20:38:39 +02:00
|
|
|
previousPoint = parentNode.GlobalTranslation;
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (NavigationPoint point in _planningPathWorldNavigationPoints) {
|
2023-08-13 21:20:45 +02:00
|
|
|
debugGeometry.SetColor(new Color(1, 0, 1));
|
2023-07-16 20:38:39 +02:00
|
|
|
debugGeometry.AddVertex(previousPoint + yOffset);
|
|
|
|
debugGeometry.AddVertex(point.WorldPosition + yOffset);
|
|
|
|
|
|
|
|
previousPoint = point.WorldPosition;
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-07-16 20:38:39 +02:00
|
|
|
previousPoint = parentNode.GlobalTranslation;
|
2023-12-29 17:43:04 +01:00
|
|
|
foreach (NavigationPoint point in _activePathWorldNavigationPoints) {
|
2023-08-13 21:20:45 +02:00
|
|
|
debugGeometry.SetColor(new Color(1, 1, 0));
|
2023-07-16 20:38:39 +02:00
|
|
|
debugGeometry.AddVertex(previousPoint + yOffset);
|
|
|
|
debugGeometry.AddVertex(point.WorldPosition + yOffset);
|
|
|
|
|
|
|
|
previousPoint = point.WorldPosition;
|
|
|
|
}
|
2023-07-15 00:39:38 +02:00
|
|
|
|
2023-07-07 17:27:05 +02:00
|
|
|
debugGeometry.End();
|
|
|
|
}
|
2023-01-03 17:46:55 +01:00
|
|
|
}
|