Esempio n. 1
0
    /// <summary>
    ///判断是否在危险区域
    /// </summary>
    public bool IsDangerousArea(Grid nowGrig)
    {
        // 清空危险区域,安全区域
        dangerousList.Clear();
        safeList.Clear();

        Bomb bomb;

        //获取所有炸弹脚本
        Bomb [] _bombnum = GameObject.FindObjectsOfType <Bomb> ();

        // 获取和原地图一致的地图记录危险区域
        _dangerousMap = _map.GetCopyMap();

        bool isCreateUpBomb, isCreateDownBomb, isCreateLeftBomb, isCreateRightBomb;

        //获取所有炸弹的危险格子加入危险列表
        for (int bombIndex = 0; bombIndex < _bombnum.Length; bombIndex++)
        {
            bomb = _bombnum [bombIndex];
            //地图更新
            //_map.GetGrid(bomb.transform.position);

            // 获取炸弹所在格子
            Grid bombGrid = _map.GetGrid(bomb.transform.position);
            // 炸弹格子上下左右可以设置为危险区域
            isCreateUpBomb = isCreateDownBomb = isCreateLeftBomb = isCreateRightBomb = true;
            //遍历威力,找出危险格子,并存入危险列表
            dangerousList.Add(new GridDate(bombGrid.Rows, bombGrid.Columns));
            for (int dangerIndex = 0; dangerIndex < bomb.j; dangerIndex++)
            {
                //炸弹上方的危险区域
                if (isCreateUpBomb)
                {
                    AddGridToDangerousList(new GridDate(bombGrid.Rows + dangerIndex, bombGrid.Columns),
                                           ref dangerousList, ref isCreateUpBomb);
                }

                //炸弹下方的危险区域
                if (isCreateDownBomb)
                {
                    AddGridToDangerousList(new GridDate(bombGrid.Rows - dangerIndex, bombGrid.Columns),
                                           ref dangerousList, ref isCreateDownBomb);
                }

                //炸弹右方的危险区域
                if (isCreateRightBomb)
                {
                    AddGridToDangerousList(new GridDate(bombGrid.Rows, bombGrid.Columns + dangerIndex),
                                           ref dangerousList, ref isCreateRightBomb);
                }

                //炸弹左方的危险区域
                if (isCreateLeftBomb)
                {
                    AddGridToDangerousList(new GridDate(bombGrid.Rows, bombGrid.Columns - dangerIndex),
                                           ref dangerousList, ref isCreateLeftBomb);
                }
            }
        }

        if (_bombnum.Length == 0)
        {
            return(false);
        }

        // 在地图上把危险区域标出来
        for (int i = 0; i < dangerousList.Count; i++)
        {
            _dangerousMap [dangerousList[i].Rows, dangerousList[i].Columns] = 99;
        }

        //  遍历整张地图,把安全区域加入列表
        for (int i = 0; i < _dangerousMap.GetLength(0); i++)
        {
            for (int j = 0; j < _dangerousMap.GetLength(1); j++)
            {
                if (_dangerousMap [i, j] == 1)
                {
                    safeList.Add(new GridDate(i, j));
                }
            }
        }

        /*
         * //将所有路的格子为危险格子的删掉
         * for (int pathListIndex = safeList.Count - 1; pathListIndex >= 0; pathListIndex--) {
         *      for (int dangerousListIndex = 0; dangerousListIndex < dangerousList.Count; dangerousListIndex++) {
         *              if (safeList [pathListIndex] == dangerousList [dangerousListIndex]) {
         *                      safeList.Remove (safeList [pathListIndex]);
         *              }
         *      }
         * }
         */
        //判断敌人是否在安全区域
        return(!GridDate.IsList(safeList, nowGrig.Rows, nowGrig.Columns));

        /*for (int direnSafe = 0; direnSafe < safeList.Count; direnSafe++) {
         *      //如果敌人在安全区域,返回为真否则进行躲避危险行为
         *      if (nowGrig.Rows == safeList [direnSafe].Rows && nowGrig.Columns == safeList [direnSafe].Columns) {
         *              return true;
         *      }
         *
         * }
         *
         * return false;*/
    }
Esempio n. 2
0
    public List <GridDate> FindingPath(Grid now, Grid target)
    {
        _openList.Clear();
        _closeList.Clear();
        _isFindPath = false;
        path.Clear();

        GridDate nowData = new GridDate(now.Rows, now.Columns);

        /*_map.Rows = 2;
         * _map.Columns = 11;
         */

        //敌人初始位置
        //GridDate _direnGrid=new GridDate(_map.Rows,_map.Columns);

        _openList.Add(nowData);         //将初始位置加入到开启列表
        //获取初始格子
        //GridDate _mingrid=_openList[0];
        //获取玩家坐标
        //Vector2 pos2=GameObject.Find("Player").transform.position;
        bool isFindTarget = false;

        do
        {
            GridDate _mingrid = _openList[0];
            _mingrid._gCost = 0;

            //gridDate._hCost = (pos2.x - _mingrid.Rows) + (pos2.y - _mingrid.Columns);
            //遍历开启列表,获取权值最小的格子,如果列表中有玩家位置,则跳出
            //Debug.Log(_mingrid);
            for (int i = 0; i < _openList.Count; i++)
            {
                if (_openList[i]._fCost < _mingrid._fCost)
                {
                    _mingrid = _openList[i];
                }
                if (_openList[i].Rows == target.Rows && _openList[i].Columns == target.Columns)
                {
                    isFindTarget = true;

                    //List<GridDate> path = new List<GridDate>();

                    GridDate nowGrid = _openList[i];
                    path.Add(nowGrid);
                    do
                    {
                        nowGrid = nowGrid._writeGrid;
                        if (nowGrid == null)
                        {
                            break;
                        }
                        path.Insert(0, nowGrid);
                    }while(true);



                    string strPath = string.Empty;
                    for (int pathIndex = 0; pathIndex < path.Count; pathIndex++)
                    {
                        strPath += string.Format("{0}\n", path[pathIndex]);
                    }
                    //Debug.Log(strPath);


                    break;
                }
            }

            //在开启列表中删除此格子,并将格子加入关闭列表
            _openList.Remove(_mingrid);
            _closeList.Add(_mingrid);

            if (isFindTarget)
            {
                break;
            }
            //如果获取的最小权值的格子上一格可以移动并且没有在关闭列表中,添加到开启列表
            if (IsAIPassGrid(_mingrid.Rows + 1, _mingrid.Columns) &&
                !GridDate.IsList(_closeList, _mingrid.Rows + 1, _mingrid.Columns))
            {
                GridDate newUpGrid = new GridDate(_mingrid.Rows + 1, _mingrid.Columns);
                _openList.Add(newUpGrid);
                //记录前一个格子
                newUpGrid._writeGrid = _mingrid;
                //敌人移动步数+1
                newUpGrid._gCost = _mingrid._gCost + 1;
                //加入到open列表的格子距离玩家步数
                newUpGrid._hCost = newUpGrid.CalcMoveCount(target);
            }

            //Debug.Log(_openList.Count);

            //如果获取的最小权值的格子下一格可以移动并且没有在关闭列表中,添加到开启列表
            if (IsAIPassGrid(_mingrid.Rows - 1, _mingrid.Columns) &&
                !GridDate.IsList(_closeList, _mingrid.Rows - 1, _mingrid.Columns))
            {
                GridDate newDownGrid = new GridDate(_mingrid.Rows - 1, _mingrid.Columns);
                _openList.Add(newDownGrid);
                //记录前一个格子
                newDownGrid._writeGrid = _mingrid;
                //敌人移动步数+1
                newDownGrid._gCost = _mingrid._gCost + 1;
                //加入到open列表的格子距离玩家步数
                newDownGrid._hCost = newDownGrid.CalcMoveCount(target);
            }
            //如果获取的最小权值的格子左一格可以移动并且没有在关闭列表中,添加到开启列表
            if (IsAIPassGrid(_mingrid.Rows, _mingrid.Columns - 1) &&
                !GridDate.IsList(_closeList, _mingrid.Rows, _mingrid.Columns - 1))
            {
                GridDate newLeftGrid = new GridDate(_mingrid.Rows, _mingrid.Columns - 1);

                _openList.Add(newLeftGrid);
                //记录前一个格子
                newLeftGrid._writeGrid = _mingrid;
                //敌人移动步数+1
                newLeftGrid._gCost = _mingrid._gCost + 1;
                //加入到open列表的格子距离玩家步数
                newLeftGrid._hCost = newLeftGrid.CalcMoveCount(target);
            }
            //如果获取的最小权值的格子右一格可以移动并且没有在关闭列表中,添加到开启列表
            if (IsAIPassGrid(_mingrid.Rows, _mingrid.Columns + 1) &&
                !GridDate.IsList(_closeList, _mingrid.Rows, _mingrid.Columns + 1))
            {
                GridDate newRightGrid = new GridDate(_mingrid.Rows, _mingrid.Columns + 1);
                _openList.Add(newRightGrid);
                //记录前一个格子
                newRightGrid._writeGrid = _mingrid;
                //敌人移动步数+1
                newRightGrid._gCost = _mingrid._gCost + 1;
                //加入到open列表的格子距离玩家步数
                newRightGrid._hCost = newRightGrid.CalcMoveCount(target);
            }

            //Debug.LogFormat("_openList.Count:{0} _closeList.Count:{1}",
            //	_openList.Count, _closeList.Count);
        }while(_openList.Count > 0);
        return(path);
    }