Skip to content

Commit ff6ae7d

Browse files
committed
feat(pathfinding2d): add route cost planning
1 parent e0f0fa0 commit ff6ae7d

14 files changed

Lines changed: 627 additions & 67 deletions

com.zerogamestudio.zeroengine.pathfinding2d/Runtime/Data/Platform2DPath.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public class Platform2DPath
5454
/// <summary>预计总耗时</summary>
5555
public float TotalDuration { get; private set; }
5656

57+
public PlatformRouteCost RouteCost { get; private set; } = PlatformRouteCost.Unreachable;
58+
public bool CommandsValidated { get; private set; }
59+
5760
/// <summary>是否还有剩余指令</summary>
5861
public bool HasNextCommand => CurrentIndex < Commands.Count;
5962

@@ -136,6 +139,12 @@ public void SetStatus(PathStatus status)
136139
Status = status;
137140
}
138141

142+
public void SetRouteDiagnostics(PlatformRouteCost routeCost, bool commandsValidated)
143+
{
144+
RouteCost = routeCost;
145+
CommandsValidated = commandsValidated;
146+
}
147+
139148
/// <summary>
140149
/// 检查路径是否过期
141150
/// </summary>

com.zerogamestudio.zeroengine.pathfinding2d/Runtime/Data/PlatformSurfaceSegment.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class PlatformSurfaceSegment
2121
public int RightNodeId = -1;
2222
public readonly List<int> NodeIds = new List<int>();
2323

24+
public int Id => GroupId;
25+
public float MinX => Left;
26+
public float MaxX => Right;
2427
public float Width => Right - Left;
2528
public Vector2 Center => new Vector2((Left + Right) * 0.5f, Y);
2629

com.zerogamestudio.zeroengine.pathfinding2d/Runtime/Platform2DPathfinder.cs

Lines changed: 292 additions & 64 deletions
Large diffs are not rendered by default.

com.zerogamestudio.zeroengine.pathfinding2d/Runtime/PlatformGraphGenerator.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,37 @@ public bool TryGetSurfaceSegment(int surfaceGroupId, out PlatformSurfaceSegment
877877
return surfaceSegmentsById.TryGetValue(surfaceGroupId, out segment);
878878
}
879879

880+
public bool TryFindSurfaceSegmentAt(Vector3 position, float verticalTolerance, out PlatformSurfaceSegment segment)
881+
{
882+
foreach (var candidate in SurfaceSegments)
883+
{
884+
bool xInside = position.x >= candidate.MinX && position.x <= candidate.MaxX;
885+
bool yClose = Mathf.Abs(position.y - candidate.Y) <= verticalTolerance;
886+
if (!xInside || !yClose)
887+
continue;
888+
889+
segment = candidate;
890+
return true;
891+
}
892+
893+
segment = default;
894+
return false;
895+
}
896+
897+
public string BuildSurfaceSegmentDebug()
898+
{
899+
if (SurfaceSegments == null || SurfaceSegments.Count == 0)
900+
return "none";
901+
902+
var parts = new List<string>(SurfaceSegments.Count);
903+
foreach (var segment in SurfaceSegments)
904+
{
905+
parts.Add($"{segment.Id}:x=[{segment.MinX:F2},{segment.MaxX:F2}],y={segment.Y:F2},oneWay={segment.IsOneWay}");
906+
}
907+
908+
return string.Join(" | ", parts);
909+
}
910+
880911
private void AttachExistingNodesToSegment(PlatformSurfaceSegment segment)
881912
{
882913
foreach (var node in Nodes)

com.zerogamestudio.zeroengine.pathfinding2d/Runtime/PlatformNavigationSnapshot.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public readonly struct PlatformNavigationSnapshot
1414
public readonly PlatformPathCompletionKind CompletionKind;
1515
public readonly string CommandDebug;
1616
public readonly string SurfaceSegmentDebug;
17+
public readonly float RouteCost;
18+
public readonly int CurrentSurfaceSegmentId;
19+
public readonly int TargetSurfaceSegmentId;
20+
public readonly bool CommandsValidated;
1721

1822
public PlatformNavigationSnapshot(
1923
bool hasGraphGenerator,
@@ -27,7 +31,11 @@ public PlatformNavigationSnapshot(
2731
PlatformPathCompletionKind completionKind,
2832
string commandDebug,
2933
int surfaceSegmentCount = 0,
30-
string surfaceSegmentDebug = null)
34+
string surfaceSegmentDebug = null,
35+
float routeCost = 0f,
36+
int currentSurfaceSegmentId = -1,
37+
int targetSurfaceSegmentId = -1,
38+
bool commandsValidated = false)
3139
{
3240
HasGraphGenerator = hasGraphGenerator;
3341
IsGenerated = isGenerated;
@@ -41,6 +49,10 @@ public PlatformNavigationSnapshot(
4149
CompletionKind = completionKind;
4250
CommandDebug = commandDebug;
4351
SurfaceSegmentDebug = surfaceSegmentDebug ?? "none";
52+
RouteCost = routeCost;
53+
CurrentSurfaceSegmentId = currentSurfaceSegmentId;
54+
TargetSurfaceSegmentId = targetSurfaceSegmentId;
55+
CommandsValidated = commandsValidated;
4456
}
4557
}
4658
}

com.zerogamestudio.zeroengine.pathfinding2d/Runtime/PlatformPathFailureReason.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public enum PlatformPathFailureReason
1111
EndNodeNotFound,
1212
PathNotFound,
1313
PartialPath,
14-
PartialPathUnavailable
14+
PartialPathUnavailable,
15+
InvalidCommand
1516
}
1617
}

com.zerogamestudio.zeroengine.pathfinding2d/Runtime/PlatformPathRequest.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,27 @@ public readonly struct PlatformPathRequest
99
public readonly bool ForceRequest;
1010
public readonly bool ProjectTargetToGround;
1111
public readonly float ProjectionDistance;
12+
public readonly bool? AllowPartialPathOverride;
1213

1314
public PlatformPathRequest(
1415
Vector3 start,
1516
Vector3 target,
1617
bool forceRequest = false,
1718
bool projectTargetToGround = false,
18-
float projectionDistance = 1.5f)
19+
float projectionDistance = 1.5f,
20+
bool? allowPartialPathOverride = null)
1921
{
2022
Start = start;
2123
Target = target;
2224
ForceRequest = forceRequest;
2325
ProjectTargetToGround = projectTargetToGround;
2426
ProjectionDistance = projectionDistance;
27+
AllowPartialPathOverride = allowPartialPathOverride;
28+
}
29+
30+
public bool ShouldAllowPartialPath(bool configDefault)
31+
{
32+
return AllowPartialPathOverride ?? configDefault;
2533
}
2634
}
2735
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace ZeroEngine.Pathfinding2D
2+
{
3+
public readonly struct PlatformRouteCost
4+
{
5+
public readonly float Total;
6+
public readonly float HorizontalDistance;
7+
public readonly float VerticalDistance;
8+
public readonly int JumpCount;
9+
public readonly int FallCount;
10+
public readonly int DropDownCount;
11+
public readonly int CommandCount;
12+
13+
public PlatformRouteCost(
14+
float total,
15+
float horizontalDistance,
16+
float verticalDistance,
17+
int jumpCount,
18+
int fallCount,
19+
int dropDownCount,
20+
int commandCount)
21+
{
22+
Total = total;
23+
HorizontalDistance = horizontalDistance;
24+
VerticalDistance = verticalDistance;
25+
JumpCount = jumpCount;
26+
FallCount = fallCount;
27+
DropDownCount = dropDownCount;
28+
CommandCount = commandCount;
29+
}
30+
31+
public static PlatformRouteCost Unreachable => new PlatformRouteCost(
32+
float.PositiveInfinity,
33+
0f,
34+
0f,
35+
0,
36+
0,
37+
0,
38+
0);
39+
40+
public bool IsReachable => !float.IsPositiveInfinity(Total);
41+
}
42+
}

com.zerogamestudio.zeroengine.pathfinding2d/Runtime/PlatformRouteCost.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using UnityEngine;
2+
3+
namespace ZeroEngine.Pathfinding2D
4+
{
5+
public readonly struct PlatformRouteQuery
6+
{
7+
public readonly Vector3 StartPosition;
8+
public readonly Vector3 TargetPosition;
9+
public readonly bool ProjectTargetToGround;
10+
public readonly bool AllowPartialPath;
11+
12+
public PlatformRouteQuery(
13+
Vector3 startPosition,
14+
Vector3 targetPosition,
15+
bool projectTargetToGround = true,
16+
bool allowPartialPath = false)
17+
{
18+
StartPosition = startPosition;
19+
TargetPosition = targetPosition;
20+
ProjectTargetToGround = projectTargetToGround;
21+
AllowPartialPath = allowPartialPath;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)