xx 1 year ago
parent
commit
95472c813a
2 changed files with 179 additions and 0 deletions
  1. 68 0
      src/Program.cs
  2. 111 0
      src/RecastPathProcessor.cs

+ 68 - 0
src/Program.cs

@@ -0,0 +1,68 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Recast_Server;
+namespace testServer
+{
+
+    class Program
+    {
+        public const string Moba5V5MapNavDataPath = "../../../RecastNavData/nav.bin";//使用項目路徑無法成功運行
+
+        static public Dictionary<int, RecastPathProcessor> m_RecastPathProcessorDic = new Dictionary<int, RecastPathProcessor>();
+        static void Main(string[] args)
+        {
+            var isInitRes = RecastInterface.Init();
+            Console.WriteLine("isInitRes" + isInitRes);
+            LoadMapNavData(10001, Moba5V5MapNavDataPath.ToCharArray());
+            //int num = 0;
+            //int[] polyCount = RecastInterface.GetPathPoly(10001,out num);
+            //Console.WriteLine("polyCount:" + polyCount.Length+":"+ num);
+            while (true)
+            {
+                Console.ReadLine();
+                var recast = new RecastPath();
+                recast.StartPos = new UnityEngine.Vector3(-1.138f, 0.0f, 1.0f);
+                recast.EndPos = new UnityEngine.Vector3(2.22f, 0.0f, 2.22f);
+                SearchPath(10001,recast);
+            }
+ 
+        }
+
+        static public void LoadMapNavData(int mapId, char[] navDataPath)
+        {
+            if (m_RecastPathProcessorDic.ContainsKey(mapId))
+            {
+                Console.WriteLine($"已存在Id为{mapId}的地图Nav数据,请勿重复加载!");
+                return;
+            }
+            if (RecastInterface.LoadMap(mapId, navDataPath))
+            {
+                RecastPathProcessor recastPathProcessor = new RecastPathProcessor();
+                recastPathProcessor.MapId = mapId;
+                m_RecastPathProcessorDic[mapId] = recastPathProcessor;
+                Console.WriteLine($"加载Id为{mapId}的{new string(navDataPath)}地图Nav数据成功!");
+            }
+        }
+        static public void SearchPath(int mapId,RecastPath recastPath)
+        {
+            GetRecastPathProcessor(mapId).CalculatePath(recastPath);
+        }
+        static public RecastPathProcessor GetRecastPathProcessor(int mapId)
+        {
+            if (m_RecastPathProcessorDic.TryGetValue(mapId, out var recastPathProcessor))
+            {
+                return recastPathProcessor;
+            }
+            else
+            {
+                Console.WriteLine($"未找到地图id为{mapId}的recastPathProcessor");
+                return null;
+            }
+        }
+    }
+
+
+}

+ 111 - 0
src/RecastPathProcessor.cs

@@ -0,0 +1,111 @@
+using Recast_Server;
+using System.Collections.Generic;
+using UnityEngine;
+using System;
+namespace testServer
+{
+    /// <summary>
+    /// 引用接口。
+    /// </summary>
+    public interface IReference
+    {
+        /// <summary>
+        /// 清理引用。
+        /// </summary>
+        void Clear();
+    }
+    public class RecastPath : IReference
+    {
+        public Vector3 StartPos = Vector3.zero;
+        public Vector3 EndPos = Vector3.zero;
+
+        public List<Vector3> Results = new List<Vector3>();
+
+        public void Clear()
+        {
+            StartPos = Vector3.zero;
+            EndPos = Vector3.zero;
+            Results.Clear();
+        }
+    }
+
+    public static class RecastPathExtension
+    {
+        /// <summary>
+        /// 修改坐标以适配Recast
+        /// </summary>
+        /// <param name="self"></param>
+        public static void AujustSelfContentForRecast(this RecastPath self)
+        {
+            self.StartPos.x = -self.StartPos.x;
+            self.EndPos.x = -self.EndPos.x;
+        }
+    }
+
+    /// <summary>
+    /// 寻路处理者
+    /// </summary>
+    public class RecastPathProcessor : IReference
+    {
+        /// <summary>
+        /// 归属的地图Id
+        /// </summary>
+        public int MapId;
+
+        public void CalculatePath(RecastPath recastPath)
+        {
+            if (RecastInterface.FindPath(this.MapId, recastPath.StartPos, recastPath.EndPos))
+            {
+                 var isSmooth =  RecastInterface.Smooth(this.MapId, 2f, 0.5f);
+                if(isSmooth)
+                {
+                    int smoothCount = 0;
+                    float[] smooths = RecastInterface.GetPathSmooth(this.MapId, out smoothCount);
+                    for (int i = 0; i < smoothCount; ++i)
+                    {
+                        Vector3 node = new Vector3(smooths[i * 3], smooths[i * 3 + 1], smooths[i * 3 + 2]);
+                        recastPath.Results.Add(node);
+                        //Con.WriteLine($"路径点:{node}");
+                        Console.WriteLine($"路径点:{node}");
+                    }
+                }
+            }
+        }
+
+        public void Clear()
+        {
+            MapId = 0;
+        }
+
+        // #region Benchmark
+        //
+        // public static void BenchmarkSample()
+        // {
+        //     BenchmarkHelper.Profile("寻路100000次", BenchmarkRecast, 100);
+        // }
+        //
+        // private static void BenchmarkRecast()
+        // {
+        //     if (RecastInterface.FindPath(100,
+        //         new System.Numerics.Vector3(-RandomHelper.RandomNumber(2, 50) - RandomHelper.RandFloat(),
+        //             RandomHelper.RandomNumber(-1, 5) + RandomHelper.RandFloat(), RandomHelper.RandomNumber(3, 20) + RandomHelper.RandFloat()),
+        //         new System.Numerics.Vector3(-RandomHelper.RandomNumber(2, 50) - RandomHelper.RandFloat(),
+        //             RandomHelper.RandomNumber(-1, 5) + RandomHelper.RandFloat(), RandomHelper.RandomNumber(3, 20) + RandomHelper.RandFloat())))
+        //     {
+        //         RecastInterface.Smooth(100, 2f, 0.5f);
+        //         {
+        //             int smoothCount = 0;
+        //             float[] smooths = RecastInterface.GetPathSmooth(100, out smoothCount);
+        //             List<Vector3> results = new List<Vector3>();
+        //             for (int i = 0; i < smoothCount; ++i)
+        //             {
+        //                 Vector3 node = new Vector3(smooths[i * 3], smooths[i * 3 + 1], smooths[i * 3 + 2]);
+        //                 results.Add(node);
+        //             }
+        //         }
+        //     }
+        // }
+        //
+        // #endregion
+    }
+}