Пример #1
0
        //战场中铺设格子
        public void Generate(int width, int height, int obstacleCount, int gap)
        {
            EUtilityHelperL.TimerStart();

            if (width <= 0 || height <= 0)
            {
                return;
            }

            //记录地图宽高
            mapWidth  = width;
            mapHeight = height;
            //生成格子数组
            mapGrids = new GridUnitData[mapWidth, mapHeight];

            //全部生成为普通格子
            for (int r = 0; r < mapHeight; ++r)
            {
                for (int c = 0; c < mapWidth; ++c)
                {
                    GridUnitData gridUnitData = new GridUnitData(mapID, r, c);
                    gridUnitData.localPosition = new Vector3(
                        c * EGameConstL.Map_GridWidth + ((r & 1) == (EGameConstL.Map_FirstRowOffset ? 0 : 1) ? (EGameConstL.Map_GridWidth * 0.5f) : 0f),
                        -r * EGameConstL.Map_GridOffsetY,
                        0
                        );

                    //初始设置为普通格子
                    gridUnitData.GridType = GridType.Normal;
                    //保存
                    mapGrids[c, r] = gridUnitData;
                }
            }
            //随机一些障碍格子
            GenerateObstacle(obstacleCount, gap);
            //整理格子列表
            TidyGridList();

            EUtilityHelperL.Log(string.Format("Generate map {0}, time cost:{1}", mapID, EUtilityHelperL.TimerEnd()));
        }
Пример #2
0
        private void TestNavigation()
        {
            //如果点击了鼠标左键
            if (Input.GetMouseButtonDown(0))
            {
                //计算点击位置
                Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition);
                clickedWorldPos.z = 0;
                //判断是否有格子被点中?
                GridUnit clicked = GetGridClicked(clickedWorldPos);
                //点中了格子
                if (clicked != null)
                {
                    if (clicked.gridData.GridType == GridType.Obstacle)
                    {
                        //点中了障碍物!
                        Debug.Log("Clicked obstacle.");
                        return;
                    }
                    if (from == null)
                    {
                        //当前还没有选择起始地点
                        from = clicked;
                        from.GridRenderType = GridRenderType.Start;
                    }
                    else if (to == null)
                    {
                        //两次点中了起点
                        if (from.Equals(clicked))
                        {
                            return;
                        }

                        //当前没有选择终点
                        to = clicked;
                        to.GridRenderType = GridRenderType.End;
                        EUtilityHelperL.TimerStart();
                        int navTimes = 999;
                        int count    = navTimes;
                        while (count > 0)
                        {
                            //有起点有终点,开始导航
                            if (MapNavigator.Instance.Navigate(currentData.mapData, from.gridData, to.gridData, path, searched))
                            {
                            }
                            else
                            {
                                //没有找到路径
                                Debug.LogError("Navitation failed. No path.");
                                return;
                            }
                            --count;
                        }
                        TestGridRender();
                        EUtilityHelperL.Log(string.Format("Nav times:{0}, timeCost{1:00}", navTimes, EUtilityHelperL.TimerEnd()));
                    }
                    else
                    {
                        from.GridRenderType = GridRenderType.Normal;
                        from = null;
                        to.GridRenderType = GridRenderType.Normal;
                        to = null;
                        foreach (var item in searched)
                        {
                            gridUnits[item.column, item.row].GridRenderType = GridRenderType.Normal;
                        }

                        foreach (var item in path)
                        {
                            gridUnits[item.column, item.row].GridRenderType = GridRenderType.Normal;
                        }
                    }
                }
                //没有点中格子
                else
                {
                    if (from != null)
                    {
                        from.GridRenderType = GridRenderType.Normal;
                        from = null;
                    }
                    if (to != null)
                    {
                        to.GridRenderType = GridRenderType.Normal;
                        to = null;
                    }

                    foreach (var item in searched)
                    {
                        gridUnits[item.column, item.row].GridRenderType = GridRenderType.Normal;
                    }

                    foreach (var item in path)
                    {
                        gridUnits[item.column, item.row].GridRenderType = GridRenderType.Normal;
                    }
                }
            }
        }