From 78d4591c3c79f7db630c86803506337a9dcf3f9f Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 8 Oct 2023 14:32:13 +0900 Subject: [PATCH] refactor: DtPolyTypes --- .../Unity/Astar/OffMeshLinkCreator.cs | 2 +- src/DotRecast.Detour/DtNavMesh.cs | 10 ++++---- src/DotRecast.Detour/DtNavMeshBuilder.cs | 4 ++-- src/DotRecast.Detour/DtNavMeshQuery.cs | 24 +++++++++---------- src/DotRecast.Detour/DtNavMeshRaycast.cs | 2 +- src/DotRecast.Detour/DtPoly.cs | 6 ----- src/DotRecast.Detour/DtPolyTypes.cs | 9 +++++++ .../Draw/RecastDebugDraw.cs | 8 +++---- .../NavMeshBuilderTest.cs | 2 +- 9 files changed, 35 insertions(+), 32 deletions(-) create mode 100644 src/DotRecast.Detour/DtPolyTypes.cs diff --git a/src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs b/src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs index 98bbc43..f6e5052 100644 --- a/src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs +++ b/src/DotRecast.Detour.Extras/Unity/Astar/OffMeshLinkCreator.cs @@ -40,7 +40,7 @@ namespace DotRecast.Detour.Extras.Unity.Astar startTile.polys[poly] = new DtPoly(poly, 2); startTile.polys[poly].verts[0] = startTile.header.vertCount; startTile.polys[poly].verts[1] = startTile.header.vertCount + 1; - startTile.polys[poly].SetPolyType(DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION); + startTile.polys[poly].SetPolyType(DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION); startTile.verts = RcArrayUtils.CopyOf(startTile.verts, startTile.verts.Length + 6); startTile.header.polyCount++; startTile.header.vertCount += 2; diff --git a/src/DotRecast.Detour/DtNavMesh.cs b/src/DotRecast.Detour/DtNavMesh.cs index da3991a..74c6120 100644 --- a/src/DotRecast.Detour/DtNavMesh.cs +++ b/src/DotRecast.Detour/DtNavMesh.cs @@ -396,7 +396,7 @@ namespace DotRecast.Detour { DtPoly p = tile.data.polys[i]; // Do not return off-mesh connection polygons. - if (p.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (p.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } @@ -650,7 +650,7 @@ namespace DotRecast.Detour DtPoly poly = tile.data.polys[i]; tile.polyLinks[poly.index] = DT_NULL_LINK; - if (poly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (poly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } @@ -1227,7 +1227,7 @@ namespace DotRecast.Detour // Off-mesh connections do not have detail polys and getting height // over them does not make sense. - if (poly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (poly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { return false; } @@ -1332,7 +1332,7 @@ namespace DotRecast.Detour posOverPoly = false; // Off-mesh connections don't have detail polygons. - if (poly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (poly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { int i = poly.verts[0] * 3; var v0 = new RcVec3f { x = tile.data.verts[i], y = tile.data.verts[i + 1], z = tile.data.verts[i + 2] }; @@ -1547,7 +1547,7 @@ namespace DotRecast.Detour DtPoly poly = tile.data.polys[ip]; // Make sure that the current poly is indeed off-mesh link. - if (poly.GetPolyType() != DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (poly.GetPolyType() != DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { return DtStatus.DT_FAILURE; } diff --git a/src/DotRecast.Detour/DtNavMeshBuilder.cs b/src/DotRecast.Detour/DtNavMeshBuilder.cs index 1cedec7..cc7fb55 100644 --- a/src/DotRecast.Detour/DtNavMeshBuilder.cs +++ b/src/DotRecast.Detour/DtNavMeshBuilder.cs @@ -487,7 +487,7 @@ namespace DotRecast.Detour p.vertCount = 0; p.flags = option.polyFlags[i]; p.SetArea(option.polyAreas[i]); - p.SetPolyType(DtPoly.DT_POLYTYPE_GROUND); + p.SetPolyType(DtPolyTypes.DT_POLYTYPE_GROUND); for (int j = 0; j < nvp; ++j) { if (option.polys[src + j] == MESH_NULL_IDX) @@ -534,7 +534,7 @@ namespace DotRecast.Detour p.verts[1] = offMeshVertsBase + n * 2 + 1; p.flags = option.offMeshConFlags[i]; p.SetArea(option.offMeshConAreas[i]); - p.SetPolyType(DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION); + p.SetPolyType(DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION); n++; } } diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index 25cbd78..e680aed 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -96,7 +96,7 @@ namespace DotRecast.Detour { DtPoly p = tile.data.polys[i]; // Do not return off-mesh connection polygons. - if (p.GetPolyType() != DtPoly.DT_POLYTYPE_GROUND) + if (p.GetPolyType() != DtPolyTypes.DT_POLYTYPE_GROUND) { continue; } @@ -249,7 +249,7 @@ namespace DotRecast.Detour m_nav.GetTileAndPolyByRefUnsafe(bestRef, out var bestTile, out var bestPoly); // Place random locations on on ground. - if (bestPoly.GetPolyType() == DtPoly.DT_POLYTYPE_GROUND) + if (bestPoly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_GROUND) { // Calc area of the polygon. float polyArea = 0.0f; @@ -510,7 +510,7 @@ namespace DotRecast.Detour // We used to return success for offmesh connections, but the // getPolyHeight in DetourNavMesh does not do this, so special // case it here. - if (poly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (poly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { int i = poly.verts[0] * 3; var v0 = new RcVec3f { x = tile.data.verts[i], y = tile.data.verts[i + 1], z = tile.data.verts[i + 2] }; @@ -628,7 +628,7 @@ namespace DotRecast.Detour { DtPoly p = tile.data.polys[i]; // Do not return off-mesh connection polygons. - if (p.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (p.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } @@ -1607,7 +1607,7 @@ namespace DotRecast.Detour // End of the path. left = closestEndPos; right = closestEndPos; - toType = DtPoly.DT_POLYTYPE_GROUND; + toType = DtPolyTypes.DT_POLYTYPE_GROUND; } // Right vertex. @@ -1640,7 +1640,7 @@ namespace DotRecast.Detour { flags = DtStraightPathFlags.DT_STRAIGHTPATH_END; } - else if (leftPolyType == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + else if (leftPolyType == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { flags = DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION; } @@ -1696,7 +1696,7 @@ namespace DotRecast.Detour { flags = DtStraightPathFlags.DT_STRAIGHTPATH_END; } - else if (rightPolyType == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + else if (rightPolyType == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { flags = DtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION; } @@ -1996,7 +1996,7 @@ namespace DotRecast.Detour } // Handle off-mesh connections. - if (fromPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (fromPoly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { // Find link that points to first vertex. for (int i = fromTile.polyLinks[fromPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = fromTile.links[i].next) @@ -2019,7 +2019,7 @@ namespace DotRecast.Detour return DtStatus.DT_FAILURE | DtStatus.DT_INVALID_PARAM; } - if (toPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (toPoly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { for (int i = toTile.polyLinks[toPoly.index]; i != DtNavMesh.DT_NULL_LINK; i = toTile.links[i].next) { @@ -2256,7 +2256,7 @@ namespace DotRecast.Detour m_nav.GetTileAndPolyByRefUnsafe(link.refs, out nextTile, out nextPoly); // Skip off-mesh connections. - if (nextPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (nextPoly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } @@ -2839,7 +2839,7 @@ namespace DotRecast.Detour m_nav.GetTileAndPolyByRefUnsafe(neighbourRef, out var neighbourTile, out var neighbourPoly); // Skip off-mesh connections. - if (neighbourPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (neighbourPoly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } @@ -3233,7 +3233,7 @@ namespace DotRecast.Detour m_nav.GetTileAndPolyByRefUnsafe(neighbourRef, out var neighbourTile, out var neighbourPoly); // Skip off-mesh connections. - if (neighbourPoly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (neighbourPoly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } diff --git a/src/DotRecast.Detour/DtNavMeshRaycast.cs b/src/DotRecast.Detour/DtNavMeshRaycast.cs index 869efdb..af79efd 100644 --- a/src/DotRecast.Detour/DtNavMeshRaycast.cs +++ b/src/DotRecast.Detour/DtNavMeshRaycast.cs @@ -50,7 +50,7 @@ namespace DotRecast.Detour for (int i = 0; i < tile.data.header.polyCount; ++i) { DtPoly p = tile.data.polys[i]; - if (p.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (p.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } diff --git a/src/DotRecast.Detour/DtPoly.cs b/src/DotRecast.Detour/DtPoly.cs index d923665..0ef2a77 100644 --- a/src/DotRecast.Detour/DtPoly.cs +++ b/src/DotRecast.Detour/DtPoly.cs @@ -23,12 +23,6 @@ namespace DotRecast.Detour /** Defines a polygon within a MeshTile object. */ public class DtPoly { - /** The polygon is a standard convex polygon that is part of the surface of the mesh. */ - public const int DT_POLYTYPE_GROUND = 0; - - /** The polygon is an off-mesh connection consisting of two vertices. */ - public const int DT_POLYTYPE_OFFMESH_CONNECTION = 1; - public readonly int index; /** The indices of the polygon's vertices. The actual vertices are located in MeshTile::verts. */ diff --git a/src/DotRecast.Detour/DtPolyTypes.cs b/src/DotRecast.Detour/DtPolyTypes.cs new file mode 100644 index 0000000..2d26de8 --- /dev/null +++ b/src/DotRecast.Detour/DtPolyTypes.cs @@ -0,0 +1,9 @@ +namespace DotRecast.Detour +{ + /// Flags representing the type of a navigation mesh polygon. + public static class DtPolyTypes + { + public const int DT_POLYTYPE_GROUND = 0; // The polygon is a standard convex polygon that is part of the surface of the mesh. + public const int DT_POLYTYPE_OFFMESH_CONNECTION = 1; // The polygon is an off-mesh connection consisting of two vertices. + } +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs b/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs index be652fb..c8e1f31 100644 --- a/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs @@ -124,7 +124,7 @@ public class RecastDebugDraw : DebugDraw for (int i = 0; i < tile.data.header.polyCount; ++i) { DtPoly p = tile.data.polys[i]; - if (p.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (p.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } @@ -171,7 +171,7 @@ public class RecastDebugDraw : DebugDraw { DtPoly p = tile.data.polys[i]; - if (p.GetPolyType() != DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (p.GetPolyType() != DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } @@ -305,7 +305,7 @@ public class RecastDebugDraw : DebugDraw { DtPoly p = tile.data.polys[i]; - if (p.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (p.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { continue; } @@ -1290,7 +1290,7 @@ public class RecastDebugDraw : DebugDraw int c = DuTransCol(col, 64); int ip = poly.index; - if (poly.GetPolyType() == DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION) + if (poly.GetPolyType() == DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION) { DtOffMeshConnection con = tile.data.offMeshCons[ip - tile.data.header.offMeshBase]; diff --git a/test/DotRecast.Detour.Test/NavMeshBuilderTest.cs b/test/DotRecast.Detour.Test/NavMeshBuilderTest.cs index 14097b5..0a9393c 100644 --- a/test/DotRecast.Detour.Test/NavMeshBuilderTest.cs +++ b/test/DotRecast.Detour.Test/NavMeshBuilderTest.cs @@ -64,6 +64,6 @@ public class NavMeshBuilderTest Assert.That(nmd.polys[118].verts[1], Is.EqualTo(224)); Assert.That(nmd.polys[118].flags, Is.EqualTo(12)); Assert.That(nmd.polys[118].GetArea(), Is.EqualTo(2)); - Assert.That(nmd.polys[118].GetPolyType(), Is.EqualTo(DtPoly.DT_POLYTYPE_OFFMESH_CONNECTION)); + Assert.That(nmd.polys[118].GetPolyType(), Is.EqualTo(DtPolyTypes.DT_POLYTYPE_OFFMESH_CONNECTION)); } }