Пример #1
0
    /// <summary>
    /// (八向)洪水填充算法
    /// </summary>
    private void FloodFill(NavGraphNode startPoint)
    {
        Queue <NavGraphNode> queue = new Queue <NavGraphNode>();

        queue.Enqueue(startPoint);

        FillState state;
        int       id = 0;

        while (queue.Count != 0)
        {
            var node = queue.Dequeue();

            #region 进行八个方向的填充


            //上方
            if ((state = isFill(node.Pos, Vector2.up, distanceBetweenNode, ref id)) == FillState.Success)
            {
                var n = AddPointAndNode(node, node.Pos + up * distanceBetweenNode, 0, lowCost);
                queue.Enqueue(n);
            }
            else if (state == FillState.FailByOtherPoint)
            {
                if (id >= 0 && !navGraph.HasEdge(node.ID, id))
                {
                    NavGraph.AddEdge(node.ID, id, lowCost);
                    //NavGraph.AddEdge(id, node.ID, lowCost);
                    DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos);
                }
            }
            //下方
            if ((state = isFill(node.Pos, Vector2.down, distanceBetweenNode, ref id)) == FillState.Success)
            {
                var n = AddPointAndNode(node, node.Pos + down * distanceBetweenNode, 0, lowCost);
                queue.Enqueue(n);
            }
            else if (state == FillState.FailByOtherPoint)
            {
                if (id >= 0 && !navGraph.HasEdge(node.ID, id))
                {
                    NavGraph.AddEdge(node.ID, id, lowCost);
                    //NavGraph.AddEdge(id, node.ID, lowCost);
                    DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos);
                }
            }

            //左边
            if ((state = isFill(node.Pos, Vector2.left, distanceBetweenNode, ref id)) == FillState.Success)
            {
                var n = AddPointAndNode(node, node.Pos + left * distanceBetweenNode, 0, lowCost);
                queue.Enqueue(n);
            }
            else if (state == FillState.FailByOtherPoint)
            {
                if (id >= 0 && !navGraph.HasEdge(node.ID, id))
                {
                    NavGraph.AddEdge(node.ID, id, lowCost);
                    //NavGraph.AddEdge(id, node.ID, lowCost);
                    DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos);
                }
            }

            //右边
            if ((state = isFill(node.Pos, Vector2.right, distanceBetweenNode, ref id)) == FillState.Success)
            {
                var n = AddPointAndNode(node, node.Pos + right * distanceBetweenNode, 0, lowCost);

                queue.Enqueue(n);
            }
            else if (state == FillState.FailByOtherPoint)
            {
                if (id >= 0 && !navGraph.HasEdge(node.ID, id))
                {
                    NavGraph.AddEdge(node.ID, id, lowCost);
                    //NavGraph.AddEdge(id, node.ID, lowCost);
                    DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos);
                }
            }
            //左上角
            if ((state = isFill(node.Pos, Vector2.up + Vector2.left, distanceBetweenDiagonal, ref id)) == FillState.Success)
            {
                var n = AddPointAndNode(node, node.Pos + leftUp * distanceBetweenDiagonal, 0, heightCost);
                queue.Enqueue(n);
            }
            else if (state == FillState.FailByOtherPoint)
            {
                if (id >= 0 && !navGraph.HasEdge(node.ID, id))
                {
                    NavGraph.AddEdge(node.ID, id, heightCost);
                    //NavGraph.AddEdge(id, node.ID, heightCost);
                    DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos);
                }
            }
            //右上角
            if ((state = isFill(node.Pos, Vector2.up + Vector2.right, distanceBetweenDiagonal, ref id)) == FillState.Success)
            {
                var n = AddPointAndNode(node, node.Pos + rightUp * distanceBetweenDiagonal, 0, heightCost);
                queue.Enqueue(n);
            }
            else if (state == FillState.FailByOtherPoint)
            {
                if (id >= 0 && !navGraph.HasEdge(node.ID, id))
                {
                    NavGraph.AddEdge(node.ID, id, heightCost);
                    //NavGraph.AddEdge(id, node.ID, heightCost);
                    DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos);
                }
            }
            //左下角
            if ((state = isFill(node.Pos, Vector2.down + Vector2.left, distanceBetweenDiagonal, ref id)) == FillState.Success)
            {
                var n = AddPointAndNode(node, node.Pos + leftDown * distanceBetweenDiagonal, 0, heightCost);
                queue.Enqueue(n);
            }
            else if (state == FillState.FailByOtherPoint)
            {
                if (id >= 0 && !navGraph.HasEdge(node.ID, id))
                {
                    NavGraph.AddEdge(node.ID, id, heightCost);
                    //NavGraph.AddEdge(id, node.ID, heightCost);
                    DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos);
                }
            }
            //右下角
            if ((state = isFill(node.Pos, Vector2.down + Vector2.right, distanceBetweenDiagonal, ref id)) == FillState.Success)
            {
                var n = AddPointAndNode(node, node.Pos + rightDown * distanceBetweenDiagonal, 0, heightCost);
                queue.Enqueue(n);
            }
            else if (state == FillState.FailByOtherPoint)
            {
                if (id >= 0 && !navGraph.HasEdge(node.ID, id))
                {
                    NavGraph.AddEdge(node.ID, id, heightCost);
                    //NavGraph.AddEdge(id, node.ID, heightCost);
                    DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos);
                }
            }
            #endregion
        }
    }