コード例 #1
0
        /// <summary>
        /// 广度优先寻路
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="ex"></param>
        /// <param name="ey"></param>
        /// <returns></returns>
        public List <MoveSearchHelper> GetWay(int x, int y, int ex, int ey, bool ignoreSpirits = true)
        {
            bool[,] visited = new bool[Field.actualXBlockNo, Field.actualYBlcokNo];
            for (int i = 0; i < Field.actualXBlockNo; ++i)
            {
                for (int j = 0; j < Field.actualYBlcokNo; ++j)
                {
                    visited[i, j] = false;
                }
            }

            Queue <MoveSearchHelper> queue = new Queue <MoveSearchHelper>();
            List <MoveSearchHelper>  rst   = new List <MoveSearchHelper>();

            queue.Enqueue(new MoveSearchHelper()
            {
                X = x, Y = y
            });
            visited[x, y] = true;
            while (queue.Count > 0)
            {
                MoveSearchHelper node = queue.Dequeue();
                if (node.X == ex && node.Y == ey)
                {
                    do
                    {
                        rst.Add(node);
                        node = node.front;
                    } while (node != null);
                    rst.Reverse();
                    return(rst);
                }
                for (int i = 0; i < 4; ++i)
                {
                    int newx = node.X + directionX[i];
                    int newy = node.Y + directionY[i];
                    if ((ignoreSpirits && Field.GetSpirit(newx, newy) != null) || newx < 0 || newx >= Field.actualXBlockNo || newy < 0 ||
                        newy >= Field.actualYBlcokNo || Field.mapCoverLayer[newx, newy] == true || visited[newx, newy])
                    {
                        continue;
                    }
                    queue.Enqueue(new MoveSearchHelper()
                    {
                        X = newx, Y = newy, front = node
                    });
                    visited[newx, newy] = true;
                }
            }
            return(new List <MoveSearchHelper>());
        }
コード例 #2
0
        /// <summary>
        /// 寻找移动范围
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public List <LocationBlock> GetMoveRange(int x, int y)
        {
            List <LocationBlock> rst = new List <LocationBlock>();
            //广度优先遍历,角色移动范围
            Queue <MoveSearchHelper> searchQueue = new Queue <MoveSearchHelper>();

            searchQueue.Enqueue(new MoveSearchHelper()
            {
                X = x, Y = y, Cost = 0
            });

            while (searchQueue.Count > 0)
            {
                MoveSearchHelper currentNode = searchQueue.Dequeue();
                int xx   = currentNode.X;
                int yy   = currentNode.Y;
                int cost = currentNode.Cost;

                bool exist = false;
                foreach (var b in rst)
                {
                    if (b.X == xx && b.Y == yy)
                    {
                        exist = true;
                        break;
                    }
                }
                if (exist)
                {
                    continue;
                }

                rst.Add(new LocationBlock()
                {
                    X = xx, Y = yy
                });
                for (int i = 0; i < 4; ++i)
                {
                    int x2    = xx + directionX[i];
                    int y2    = yy + directionY[i];
                    int dcost = 1;

                    if (!Field.currentSpirit.Role.HasTalent("轻功大师"))
                    {
                        //周围有敌人,则增大代价
                        for (int j = 0; j < 4; ++j)
                        {
                            int    x3 = x2 + directionX[j];
                            int    y3 = y2 + directionY[j];
                            Spirit targetBlockSpirit = Field.GetSpirit(x3, y3);
                            if (targetBlockSpirit != null && targetBlockSpirit.Team != Field.currentSpirit.Team)
                            {
                                dcost = 2;
                                break;
                            }
                        }
                    }
                    if (IsEmptyBlock(x2, y2) &&
                        cost + dcost <= Field.currentSpirit.MoveAbility)
                    {
                        searchQueue.Enqueue(new MoveSearchHelper()
                        {
                            X = x2, Y = y2, Cost = cost + dcost
                        });
                    }
                }
            }
            return(rst);
        }