Navigation now grid based again.

WorldChunkRefactoring
Martin Felis 2023-06-29 23:07:29 +02:00
parent adaf5292fb
commit c8921bfb7b
3 changed files with 32 additions and 21 deletions

View File

@ -81,7 +81,7 @@ public class NavigationComponent : Node
private NavigationPoint _currentGoal;
private Vector3 _currentGoalPositionWorld = Vector3.Zero;
private Quat _currentGoalOrientationWorld = Quat.Identity;
private List<NavigationPoint> _pathWorldNavigationPoints;
private HexCell[] _path;
@ -115,10 +115,10 @@ public class NavigationComponent : Node
foreach (int index in Enumerable.Range(1, _path.Length - 1))
{
_pathWorldNavigationPoints.Add(
new NavigationPoint(TileWorld.GetTileWorldCenterFromOffset(_path[index].OffsetCoords)));
new NavigationPoint(TileWorld.GetHexCenterFromOffset(_path[index].OffsetCoords)));
}
if ((toPositionWorld - TileWorld.GetTileWorldCenterFromOffset(toCell.OffsetCoords)).LengthSquared() >
if ((fromPositionWorld - TileWorld.GetHexCenterFromOffset(toCell.OffsetCoords)).LengthSquared() >
Globals.EpsPositionSquared)
{
// Remove the last one, because it is only the position rounded to HexGrid coordinates.
@ -129,7 +129,7 @@ public class NavigationComponent : Node
}
_pathWorldNavigationPoints.Add(new NavigationPoint(toPositionWorld));
UpdateCurrentGoal();
}
@ -140,33 +140,35 @@ public class NavigationComponent : Node
_pathWorldNavigationPoints.Add(new NavigationPoint(toWorldOrientation));
}
public void PlanGridPath(Transform fromTransformWorld, NavigationPoint navigationPoint)
{
if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position)
&& navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Orientation))
{
PlanGridPath(fromTransformWorld.origin, navigationPoint.WorldPosition, navigationPoint.WorldOrientation);
} else if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position))
}
else if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position))
{
PlanGridPath(fromTransformWorld.origin, navigationPoint.WorldPosition);
} else
}
else
{
throw new NotImplementedException();
}
}
public void PlanDirectPath(Vector3 fromPositionWorld, Vector3 toPositionWorld)
{
_pathWorldNavigationPoints.Clear();
_pathWorldNavigationPoints.Add(new NavigationPoint(toPositionWorld));
UpdateCurrentGoal();
}
public void PlanDirectPath(Vector3 fromPositionWorld, Vector3 toPositionWorld, Quat toWorldOrientation)
{
PlanDirectPath(fromPositionWorld, toPositionWorld);
@ -181,10 +183,12 @@ public class NavigationComponent : Node
&& navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Orientation))
{
PlanDirectPath(fromTransformWorld.origin, navigationPoint.WorldPosition, navigationPoint.WorldOrientation);
} else if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position))
}
else if (navigationPoint.Flags.HasFlag(NavigationPoint.NavigationFlags.Position))
{
PlanDirectPath(fromTransformWorld.origin, navigationPoint.WorldPosition);
} else
}
else
{
throw new NotImplementedException();
}
@ -199,7 +203,7 @@ public class NavigationComponent : Node
}
_currentGoal = _pathWorldNavigationPoints[0];
_currentGoalPositionWorld = _pathWorldNavigationPoints[0].WorldPosition;
_currentGoalPositionWorld = _pathWorldNavigationPoints[0].WorldPosition;
_currentGoalOrientationWorld = _pathWorldNavigationPoints[0].WorldOrientation;
// GD.Print("Navigation Goal: pos " + _currentGoal.WorldPosition + " " + " rot: " + _currentGoal.WorldOrientation +
@ -212,13 +216,14 @@ public class NavigationComponent : Node
if (_currentGoal.Flags == NavigationPoint.NavigationFlags.Orientation)
{
_currentGoalPositionWorld = worldTransform.origin;
} else if (_currentGoal.Flags == NavigationPoint.NavigationFlags.Position)
}
else if (_currentGoal.Flags == NavigationPoint.NavigationFlags.Position)
{
_currentGoalOrientationWorld = worldTransform.basis.Quat();
}
}
public void UpdateCurrentGoal(Transform currentTransformWorld)
{
if (_pathWorldNavigationPoints.Count == 0)
@ -230,7 +235,7 @@ public class NavigationComponent : Node
if (_currentGoal.Flags.HasFlag(NavigationPoint.NavigationFlags.Position))
{
_currentGoalPositionWorld = _pathWorldNavigationPoints[0].WorldPosition;
_currentGoalPositionWorld = _pathWorldNavigationPoints[0].WorldPosition;
}
else
{
@ -249,10 +254,9 @@ public class NavigationComponent : Node
if (_currentGoal.IsReached(currentTransformWorld))
{
_pathWorldNavigationPoints.RemoveAt(0);
UpdateCurrentGoal();
ApplyExistingTransform(currentTransformWorld);
}
if (_pathWorldNavigationPoints.Count == 0)

View File

@ -103,7 +103,7 @@ public class Player : Entity, IInteractionInterface
if (currentTask is TaskQueueComponent.NavigationTask)
{
TaskQueueComponent.NavigationTask navigationTask = (TaskQueueComponent.NavigationTask)currentTask;
_navigationComponent.PlanDirectPath(GlobalTransform, navigationTask.NavigationPoint);
_navigationComponent.PlanGridPath(GlobalTransform, navigationTask.NavigationPoint);
}
}
}

View File

@ -345,4 +345,11 @@ public class TileWorld : Spatial
GetHeightAtOffset(offsetCoord),
tileCenter.y + ((Mathf.Sqrt(3) / 2) * Mathf.Round(Size * 0.5f)));
}
public Vector3 GetHexCenterFromOffset(Vector2 offsetCoord)
{
Vector2 tileCenter = _hexGrid.GetHexCenterFromOffset(offsetCoord);
return new Vector3(tileCenter.x, GetHeightAtOffset(offsetCoord), tileCenter.y);
}
}