Пример #1
0
    /// <summary>
    ///
    /// </summary>返回移动范围(range里面可能加了攻击范围)
    /// <param name="startCell">起点</param>
    /// <param name="range">移动范围+攻击范围</param>
    /// <param name="func">判断是否加入移动列表,海陆空移动范围不一样,非机械单位可以移动到载具里面</param>
    /// <returns></returns>
    public List <NavCell> GetMoveRangePath(HexCell startCell, int range, Func <HexCell, bool> func)
    {
        List <NavCell> path  = new List <NavCell>();             //返回的范围
        NavCell        start = new NavCell(startCell);           //起点

        start.consume = 0;                                       //起点的移动消耗
        path.Add(start);                                         //把起点添加到查找列表
        Queue <NavCell> openNav = new Queue <global::NavCell>(); //已经找到的范围

        openNav.Enqueue(start);
        NavCell navCell = null;

        while (openNav.Count > 0)
        {
            navCell = openNav.Dequeue();
            for (int i = 0; i < 6; i++)
            {
                HexCell cell = navCell.cell.GetNeighbor(i);//返回一个附近的格子
                if (cell == null)
                {
                    continue;
                }
                if (!func(cell))
                {
                    continue;                                 //判断是否满足移动条件
                }
                NavCell nav = path.Find(a => a.cell == cell); //已经找到的范围判断当前格子走过去是否消耗更小
                if (nav != null)
                {
                    if (navCell.consume + cell.Consume() >= nav.consume)
                    {
                        continue;
                    }
                    nav.ReviseParent(navCell);
                }
                else
                {
                    nav = new NavCell(cell, navCell); //新找到的范围,构造函数里面有移动消耗叠加的操作
                }
                if (nav.consume <= range)             //如果距离没有超过移动距离
                {
                    path.Add(nav);
                    if (nav.consume < range)
                    {
                        openNav.Enqueue(nav);
                    }
                }
            }
        }
        return(path);
    }
Пример #2
0
    /// <summary>
    ///
    /// </summary>找到一条到目标的路径
    /// <param name="start">起点</param>
    /// <param name="end">目标点</param>
    /// <param name="move">移动力,搜索深度达到一定值就不在搜索</param>
    /// <returns></returns>
    public List <NavCell> GetPath(HexCell start, HexCell end, int move, Func <HexCell, bool> func)
    {
        int             depth    = (int)(move * Config.pathDepth);
        Queue <NavCell> open     = new Queue <NavCell>();
        List <HexCell>  close    = new List <HexCell>();
        List <NavCell>  path     = new List <NavCell>();
        NavCell         startNav = new NavCell(start);

        startNav.consume = 0;
        open.Enqueue(startNav);
        NavCell nav    = null;
        HexCell cell   = null;
        NavCell EndNav = null;

        while (open.Count > 0)
        {
            if (EndNav != null)
            {
                break;
            }
            nav = open.Dequeue();
            if (close.Contains(nav.cell))
            {
                continue;
            }
            if (nav.consume > depth)
            {
                continue;
            }
            for (int i = 0; i < 6; i++)
            {
                cell = nav.cell.GetNeighbor(i);
                if (cell == null)
                {
                    continue;
                }
                if (cell == end)
                {
                    EndNav = new NavCell(cell, nav);
                    break;
                }
                if (!func(cell))
                {
                    continue;
                }
                NavCell _nav = null;
                if (open.Count > 0)
                {
                    _nav = open.FirstOrDefault(a => a.cell == cell);
                }
                if (_nav != null)
                {
                    if (_nav.consume > cell.Consume() + nav.consume)
                    {
                        _nav.ReviseParent(nav);
                    }
                }
                else
                {
                    _nav = new NavCell(cell, nav);
                    open.Enqueue(_nav);
                }
            }
            close.Add(nav.cell);
        }
        if (EndNav != null)
        {
            while (EndNav.consume > move)
            {
                EndNav = EndNav.parent;
            }
            while (EndNav != null)
            {
                path.Add(EndNav);
                EndNav = EndNav.parent;
            }
            return(path);
        }
        return(path);
    }
Пример #3
0
 public NavCell(HexCell cell)
 {
     this.cell    = cell;
     this.parent  = null;
     this.consume = cell.Consume();
 }
Пример #4
0
 public NavCell(HexCell cell, NavCell parent)
 {
     this.cell    = cell;
     this.parent  = parent;
     this.consume = cell.Consume() + parent.consume;
 }