Exemple #1
0
 /// <summary>
 /// 请求获取指定条件的路径
 /// </summary>
 /// <param name="startPoint">起点</param>
 /// <param name="endPoint">终点</param>
 /// <param name="collisionSize">(移动体)碰撞尺寸</param>
 /// <param name="pathFindEndBack">路径搜索结束事件回调</param>
 /// <param name="maxComputeCount">最大的搜索计算次数计数上限</param>
 public void RequestPaths(Vector3 startPoint, Vector3 endPoint, int collisionSize, MapPath.PathFindEndBack pathFindEndBack = null, int maxComputeCount = 8000)
 {
     try
     {
         this.pfStartPoint    = startPoint;
         this.pfEndPoint      = endPoint;
         this.pfCollisionSize = collisionSize;
         this.pathFindEnd     = false;
         this.pathFindResult  = true;
         this.pathFindEndBack = pathFindEndBack;
         this.paths.Clear();
         this.computeTick = 0;
         this.heapX       = new int[2];
         this.heapY       = new int[2];
         //起点网格坐标
         this.heapX[0] = Mathf.FloorToInt(startPoint.x / this.gridSize) + this.halfWidth;
         this.heapY[0] = Mathf.FloorToInt(startPoint.z / this.gridSize) + this.halfHeight;
         //终点网格坐标
         this.heapX[1] = Mathf.FloorToInt(endPoint.x / this.gridSize) + this.halfWidth;
         this.heapY[1] = Mathf.FloorToInt(endPoint.z / this.gridSize) + this.halfHeight;
         if (this.heapX[0] == this.heapX[1] && this.heapY[0] == this.heapY[1])               //原地,直接完成
         {
             this.paths.Add(endPoint);
             this.pathFindEnd = true;
             if (pathFindEndBack != null)
             {
                 pathFindEndBack(this.paths);
             }
         }
         else
         {
             //初始化heap列表
             for (int i = 0; i < 2; i++)
             {
                 if (this.heap[i] == null)
                 {
                     this.heap[i] = new Heap();
                 }
                 this.heap[i].Clear();
             }
             //重置HValeCache
             for (int i = 0; i < this.HValueCache.Count; i++)
             {
                 this.HValueCache[i].Reset();
             }
             //path列表重置
             for (int i = 0; i < this.width; i++)
             {
                 for (int j = 0; j < this.height; j++)
                 {
                     this.path[i, j] = -1;
                 }
             }
             this.cacheIndex = 0;
             for (int k = 0; k < 2; k++)
             {
                 int num  = this.heapX[k];
                 int num2 = this.heapY[k];
                 for (int i = -1; i <= 1; i++)                        //将起点和终点的9宫格信息加入heap表
                 {
                     for (int j = -1; j <= 1; j++)
                     {
                         int     num3    = num + i;
                         int     num4    = num2 + j;
                         Vector3 vector  = new Vector3((float)(this.heapX[0] - this.halfWidth) * this.gridSize, 0f, (float)(this.heapY[0] - this.halfHeight) * this.gridSize);
                         Vector3 vector2 = new Vector3((float)(this.heapX[1] - this.halfWidth) * this.gridSize, 0f, (float)(this.heapY[1] - this.halfHeight) * this.gridSize);
                         Vector3 vector3 = (k != 0) ? vector2 : vector;
                         Vector3 vector4 = new Vector3((float)(num3 - this.halfWidth) * this.gridSize, 0f, (float)(num4 - this.halfHeight) * this.gridSize);
                         vector3.y = (vector4.y = 0f);
                         float num5 = Vector3.Distance(vector3, vector4);
                         float num6 = this.TryWalkDistance(vector3, vector4, collisionSize);
                         if (Math.Abs(num6 - num5) < 0.01f)
                         {
                             HValue hValue = this.getHValue(num3, num4);
                             hValue.Set((short)num3, (short)num4, num5, 0f);
                             hValue.Mark = (short)(1 + k * this.markBase);
                             hValue.PreX = -1;
                             hValue.PreY = -1;
                             this.heap[k].Push(hValue);
                         }
                     }
                 }
             }
             this.nearestTarget = this.heap[0].Peek();
             int num7 = 8;
             this.dx = new int[num7];
             this.dy = new int[num7];
             for (int i = 0; i < num7; i++)
             {
                 float num8 = (float)i * 2f * 3.14159274f / (float)num7;
                 this.dx[i] = this.Sign(Math.Cos((double)num8));
                 this.dy[i] = this.Sign(Math.Sin((double)num8));
             }
             this.pathHValues.Clear();
             this.RequestPathsImmed(this.pfStartPoint, this.pfEndPoint, this.pfCollisionSize, maxComputeCount);
         }
     }
     catch (Exception ex)          //路径搜索失败
     {
         this.paths = new List <Vector3>();
         if (GameScene.isEditor)
         {
             LogSystem.Log(new object[]
             {
                 string.Concat(new object[]
                 {
                     "寻路失败: 起始点 ",
                     startPoint,
                     " 目标点 : ",
                     endPoint,
                     " 错误消息 : ",
                     ex.Message
                 })
             });
         }
     }
 }
Exemple #2
0
 public void Clear()
 {
     this.pathFindEnd     = true;
     this.pathFindEndBack = null;
 }