Beispiel #1
0
        //根据点击位置获取点中的格子
        private GridUnitRenderer GetGridClicked(Vector3 clickedWorldPos)
        {
            //转换空间到格子组织节点(GridUnits)的空间
            clickedWorldPos = gridUnitsRoot.transform.InverseTransformPoint(clickedWorldPos);
            //初步判定所在行列
            int row    = Mathf.FloorToInt((clickedWorldPos.y - EGameConstL.Map_GridOffsetY * 0.5f) / -EGameConstL.Map_GridOffsetY);
            int column = Mathf.FloorToInt((clickedWorldPos.x + EGameConstL.Map_GridWidth * 0.5f - ((row & 1) == (EGameConstL.Map_FirstRowOffset ? 1 : 0) ? 0f : (EGameConstL.Map_GridWidth * 0.5f))) / EGameConstL.Map_GridWidth);

            int testRow    = 0;
            int testColumn = 0;
            //二次判定,判定周围格子
            GridUnitRenderer clickedGrid = null;
            float            minDis      = Mathf.Infinity;

            for (int r = -1; r <= 1; ++r)
            {
                for (int c = -1; c <= 1; ++c)
                {
                    testRow    = row + r;
                    testColumn = column + c;
                    if (testRow < 0 || testRow >= battleField.battleMap.mapHeight ||
                        testColumn < 0 || testColumn >= battleField.battleMap.mapWidth)
                    {
                        continue;
                    }
                    float distance = UtilityHelper.CalcDistanceInXYAxis(clickedWorldPos, battleField.battleMap.mapGrids[testColumn, testRow].localPosition);
                    if (distance < minDis && distance < EGameConstL.Map_HexRadius)
                    {
                        minDis      = distance;
                        clickedGrid = battleField.battleMap.mapGrids[testColumn, testRow].gridUnitRenderer;
                    }
                }
            }
            return(clickedGrid);
        }