Exemple #1
0
    // 計算路徑
    private bool SetMoveDirection()
    {
        // 暫時預設0,0為起點, 最後一點為終點
        MapCube startMap = m_Maps[0, 0];
        MapCube endMap   = m_Maps[m_MapMaxX - 1, m_MapMaxY - 1];

        startMap.m_State = MapCube.EMapState.eStart;
        endMap.m_State   = MapCube.EMapState.eEnd;

        // 開始計算
        MapCube moveMap = startMap;

        while (moveMap != endMap)
        {
            Debug.LogFormat("move map name: " + moveMap.name);
            for (MapCube.EDirection i = MapCube.EDirection.eUp; i < MapCube.EDirection.eMax; i++)
            {
                MapCube nextMapCube = CheckNext(moveMap, i);
                if (nextMapCube != null)
                {
                    moveMap.SetDirection(i, nextMapCube.transform);
                    moveMap = nextMapCube;
                    break;
                }
            }
        }

        return(true);
    }
Exemple #2
0
    private MapCube CheckNext(MapCube _mapCube, MapCube.EDirection _direction)
    {
        int xIndex = _mapCube.m_X;
        int yIndex = _mapCube.m_Y;

        MapCube nextMapCube = null;

        MapCube.EDirection checkDirection = MapCube.EDirection.eUp;
        if (_direction == MapCube.EDirection.eUp)
        {
            yIndex = yIndex - 1;
            if (yIndex < 0)
            {
                return(null);
            }
            nextMapCube    = m_Maps[xIndex, yIndex];
            checkDirection = MapCube.EDirection.eDown;
        }
        else if (_direction == MapCube.EDirection.eDown)
        {
            yIndex = yIndex + 1;
            if (yIndex >= m_MapMaxY)
            {
                return(null);
            }
            nextMapCube    = m_Maps[xIndex, yIndex];
            checkDirection = MapCube.EDirection.eUp;
        }
        else if (_direction == MapCube.EDirection.eLeft)
        {
            xIndex = xIndex - 1;
            if (xIndex < 0)
            {
                return(null);
            }
            nextMapCube    = m_Maps[xIndex, yIndex];
            checkDirection = MapCube.EDirection.eRight;
        }
        else if (_direction == MapCube.EDirection.eRight)
        {
            xIndex = xIndex + 1;
            if (xIndex >= m_MapMaxX)
            {
                return(null);
            }
            nextMapCube    = m_Maps[xIndex, yIndex];
            checkDirection = MapCube.EDirection.eLeft;
        }

        if (nextMapCube.CheckNextMove(checkDirection, _mapCube.m_Cost) == false)
        {
            nextMapCube = null;
        }

        return(nextMapCube);
    }