From c8921bfb7b9a3fef20b4f8f6fdf1e4450802a61d Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 29 Jun 2023 23:07:29 +0200 Subject: [PATCH] Navigation now grid based again. --- components/NavigationComponent.cs | 44 +++++++++++++++++-------------- entities/Player.cs | 2 +- scenes/TileWorld.cs | 7 +++++ 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/components/NavigationComponent.cs b/components/NavigationComponent.cs index f06f17f..4b79d46 100644 --- a/components/NavigationComponent.cs +++ b/components/NavigationComponent.cs @@ -81,7 +81,7 @@ public class NavigationComponent : Node private NavigationPoint _currentGoal; private Vector3 _currentGoalPositionWorld = Vector3.Zero; private Quat _currentGoalOrientationWorld = Quat.Identity; - + private List _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) diff --git a/entities/Player.cs b/entities/Player.cs index c3c2219..1e7e1c3 100644 --- a/entities/Player.cs +++ b/entities/Player.cs @@ -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); } } } diff --git a/scenes/TileWorld.cs b/scenes/TileWorld.cs index 28c84b6..d0044ba 100644 --- a/scenes/TileWorld.cs +++ b/scenes/TileWorld.cs @@ -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); + } } \ No newline at end of file