Beispiel #1
0
 /// <summary>
 /// 测试点中的格子
 /// </summary>
 private void TestSelected()
 {
     //如果点击了鼠标左键
     if (Input.GetMouseButtonDown(0))
     {
         //计算点击位置
         Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition);
         clickedWorldPos.z = 0;
         //判断是否有格子被点中?
         GridUnitRenderer clicked = GetGridClicked(clickedWorldPos);
         //点中了格子
         if (clicked != null)
         {
             if (selectedGrid != null)
             {
                 if (selectedGrid.Equals(clicked))
                 {
                     //重复点中相同的格子
                     return;
                 }
                 else
                 {
                     selectedGrid.ResetGridRenderType();
                 }
             }
             selectedGrid = clicked;
             selectedGrid.AppendGridRenderType(GridRenderType.Selected);
         }
         //没有点中格子,但是当前有选中,取消选中
         else if (selectedGrid != null)
         {
             selectedGrid.ResetGridRenderType();
             selectedGrid = null;
         }
     }
 }
Beispiel #2
0
 private void TestNearestGrid()
 {
     //如果点击了鼠标左键
     if (Input.GetMouseButtonDown(0))
     {
         //计算点击位置
         Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition);
         clickedWorldPos.z = 0;
         //判断是否有格子被点中?
         GridUnitRenderer clicked = GetGridClicked(clickedWorldPos);
         //点中了格子
         if (clicked != null)
         {
             if (from == null || from.Equals(clicked))
             {
                 from = clicked;
                 from.gridUnit.battleUnit = new BattleUnit();
                 from.AppendGridRenderType(GridRenderType.Start);
             }
             else if (to == null || to.Equals(clicked))
             {
                 to = clicked;
                 to.gridUnit.battleUnit = new BattleUnit();
                 to.AppendGridRenderType(GridRenderType.End);
                 //求最近的格子
                 var nearest = battleField.battleMap.GetEmptyGrid(from.gridUnit, to.gridUnit, path, -1);
                 if (nearest == null)
                 {
                     UtilityHelper.LogError("Can not find out nearest grid.");
                     from.ResetGridRenderType();
                     to.ResetGridRenderType();
                     to.gridUnit.battleUnit   = null;
                     from.gridUnit.battleUnit = null;
                     from = null;
                     to   = null;
                 }
                 else
                 {
                     nearestGrid = nearest.gridUnitRenderer;
                     nearestGrid.AppendGridRenderType(GridRenderType.Range);
                     SetPathHighlightActive(true, path.ToArray());
                 }
             }
             else
             {
                 from.ResetGridRenderType();
                 to.ResetGridRenderType();
                 nearestGrid.ResetGridRenderType();
                 to.gridUnit.battleUnit   = null;
                 from.gridUnit.battleUnit = null;
                 from        = null;
                 to          = null;
                 nearestGrid = null;
                 SetPathHighlightActive(false, null);
                 path.Clear();
             }
         }
         //没有点中格子
         else
         {
         }
     }
 }
Beispiel #3
0
        private void TestNavigation()
        {
            //如果点击了鼠标左键
            if (Input.GetMouseButtonDown(0))
            {
                //计算点击位置
                Vector3 clickedWorldPos = BattleCamera.ScreenToWorldPoint(Input.mousePosition);
                clickedWorldPos.z = 0;
                //判断是否有格子被点中?
                GridUnitRenderer clicked = GetGridClicked(clickedWorldPos);
                //点中了格子
                if (clicked != null)
                {
                    if (clicked.gridUnit.GridType == GridType.Obstacle)
                    {
                        //点中了障碍物!
                        Debug.Log("Clicked obstacle.");
                        return;
                    }
                    if (from == null)
                    {
                        //当前还没有选择起始地点
                        from = clicked;
                        from.AppendGridRenderType(GridRenderType.Start);
                    }
                    else if (to == null)
                    {
                        //两次点中了起点
                        if (from.Equals(clicked))
                        {
                            return;
                        }

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

                        foreach (var item in path)
                        {
                            battleField.battleMap.mapGrids[item.column, item.row].gridUnitRenderer.ResetGridRenderType();
                        }
                    }
                }
                //没有点中格子
                else
                {
                    if (from != null)
                    {
                        from.ResetGridRenderType();
                        from = null;
                    }
                    if (to != null)
                    {
                        to.ResetGridRenderType();
                        to = null;
                    }

                    foreach (var item in searched)
                    {
                        battleField.battleMap.mapGrids[item.column, item.row].gridUnitRenderer.ResetGridRenderType();
                    }

                    foreach (var item in path)
                    {
                        battleField.battleMap.mapGrids[item.column, item.row].gridUnitRenderer.ResetGridRenderType();
                    }
                }
            }
        }