grid.cpp

来自「Source code (C++) of the Amoebax game fo」· C++ 代码 · 共 1,733 行 · 第 1/5 页

CPP
1,733 行
字号
                {                    scheduleFallingPair ();                }        }        else        {            makeGhostsFall ();        }    }}////// \brief Marks the grid as filled.////// The grid must be marked as filled when the falling amoebas/// can't enter the grid.////// \see isFilled().///voidGrid::markAsFilled (void){    m_Filled = true;}////// \brief Moves the falling amoeba pair a position to the left.///voidGrid::moveLeft (void){    if ( 0 < std::min (m_FallingPair.main.x,                       m_FallingPair.satellite.x) &&         0 == getAmoebaAt (m_FallingPair.main.x - 1, m_FallingPair.main.y) &&         0 == getAmoebaAt (m_FallingPair.satellite.x - 1,                           m_FallingPair.satellite.y) )    {        --m_FallingPair.main.x;        --m_FallingPair.satellite.x;    }}////// \brief Moves the falling amoeba pair a position to the right.///voidGrid::moveRight (void){    if ( k_GridWidth - 1  > std::max (m_FallingPair.main.x,                                      m_FallingPair.satellite.x) &&        0 == getAmoebaAt (m_FallingPair.main.x + 1, m_FallingPair.main.y) &&        0 == getAmoebaAt (m_FallingPair.satellite.x + 1,                          m_FallingPair.satellite.y) )    {        ++m_FallingPair.main.x;        ++m_FallingPair.satellite.x;    }}////// \brief Removes some waiting ghost amoebas.////// \param newGhostAmoebas The number of waiting ghost amoebas to remove./// \return The number of ghost amoebas remaining after removing///         \p newGhostAmoebas ghost amoebas.///uint8_tGrid::removeGhostAmoebas (uint8_t newGhostAmoebas){    uint8_t remainingGhostAmoebas = 0;    if ( getNumberOfWaitingGhosts () < newGhostAmoebas )    {        remainingGhostAmoebas = newGhostAmoebas - getNumberOfWaitingGhosts ();        setNumberOfWaitingGhosts (0);    }    else    {        setNumberOfWaitingGhosts (getNumberOfWaitingGhosts () - newGhostAmoebas);    }    updateWaitingGhosts ();    return remainingGhostAmoebas;}////// \brief Rotates the falling pair clockwise.///voidGrid::rotateClockwise (void){    if ( !isSatelliteRotating () )    {        m_DegreesUntilRotationDone = 90.0f;        m_RotationDirection = -1.0f;        if ( m_FallingPair.satellite.y < m_FallingPair.main.y )        {            if ( k_GridWidth - 1 > m_FallingPair.main.x &&                    0 == getAmoebaAt (m_FallingPair.main.x + 1, m_FallingPair.main.y) )            {                m_FallingPair.satellite.x++;                m_FallingPair.satellite.y++;            }            else if ( 1 < m_FallingPair.main.x &&                    0 == getAmoebaAt (m_FallingPair.main.x - 1, m_FallingPair.main.y) )            {                m_FallingPair.satellite.y++;                m_FallingPair.main.x--;            }            else            {                // Can't rotate                m_DegreesUntilRotationDone = 0.0f;            }        }        else if ( m_FallingPair.satellite.y > m_FallingPair.main.y )        {            if ( 0 <  m_FallingPair.main.x &&                    0 == getAmoebaAt (m_FallingPair.main.x - 1, m_FallingPair.main.y) )            {                m_FallingPair.satellite.x--;                m_FallingPair.satellite.y--;            }            else if ( k_GridWidth - 1 > m_FallingPair.main.x &&                    0 == getAmoebaAt (m_FallingPair.main.x + 1, m_FallingPair.main.y) )            {                m_FallingPair.satellite.y--;                m_FallingPair.main.x++;            }            else            {                // Can't rotate                m_DegreesUntilRotationDone = 0.0f;            }        }        else if ( m_FallingPair.satellite.x > m_FallingPair.main.x )        {            if ( 0 == getAmoebaAt (m_FallingPair.main.x, m_FallingPair.main.y + 1) )            {                m_FallingPair.satellite.x--;                m_FallingPair.satellite.y++;            }            else            {                // Can't rotate                m_DegreesUntilRotationDone = 0.0f;            }        }        else        {            m_FallingPair.satellite.x++;            m_FallingPair.satellite.y--;        }    }}////// \brief Rotates the falling pair counterclockwise.///voidGrid::rotateCounterClockwise (void){    if ( !isSatelliteRotating () )    {        m_DegreesUntilRotationDone = 90.0f;        m_RotationDirection = 1.0f;        if ( m_FallingPair.satellite.y < m_FallingPair.main.y )        {            if ( 0 < m_FallingPair.main.x &&                    0 == getAmoebaAt (m_FallingPair.main.x - 1, m_FallingPair.main.y) )            {                m_FallingPair.satellite.x--;                m_FallingPair.satellite.y++;            }            else if ( k_GridWidth - 1 > m_FallingPair.main.x &&                    0 == getAmoebaAt (m_FallingPair.main.x + 1, m_FallingPair.main.y) )            {                m_FallingPair.satellite.y++;                m_FallingPair.main.x++;            }            else            {                // Can't rotate                m_DegreesUntilRotationDone = 0.0f;            }        }        else if ( m_FallingPair.satellite.y > m_FallingPair.main.y )        {            if ( k_GridWidth - 1 >  m_FallingPair.main.x &&                    0 == getAmoebaAt (m_FallingPair.main.x + 1, m_FallingPair.main.y) )            {                m_FallingPair.satellite.x++;                m_FallingPair.satellite.y--;            }            else if ( 1 < m_FallingPair.main.x &&                    0 == getAmoebaAt (m_FallingPair.main.x - 1, m_FallingPair.main.y) )            {                m_FallingPair.satellite.y--;                m_FallingPair.main.x--;            }            else            {                // Can't rotate                m_DegreesUntilRotationDone = 0.0f;            }        }        else if ( m_FallingPair.satellite.x < m_FallingPair.main.x )        {            if ( 0 == getAmoebaAt (m_FallingPair.main.x, m_FallingPair.main.y + 1) )            {                m_FallingPair.satellite.x++;                m_FallingPair.satellite.y++;            }            else            {                // Can't rotate                m_DegreesUntilRotationDone = 0.0f;            }        }        else        {            m_FallingPair.satellite.x--;            m_FallingPair.satellite.y--;        }    }}////// \brief Rotates the satellite amoeba.////// \param degreesToRotate The degrees to make rotate the satellite amoeba.///voidGrid::rotateSatelliteAmoeba (float degreesToRotate){    if ( degreesToRotate > m_DegreesUntilRotationDone )    {        degreesToRotate = m_DegreesUntilRotationDone;    }    m_DegreesUntilRotationDone -= degreesToRotate;    degreesToRotate *= m_RotationDirection;    m_CurrentRotationDegrees += degreesToRotate;    if ( m_CurrentRotationDegrees > 360.0f )    {        m_CurrentRotationDegrees -= 360.0f;    }    else if ( m_CurrentRotationDegrees < 0 )    {        m_CurrentRotationDegrees += 360.0f;    }    setFallingAmoebaScreenPosition (m_FallingPair.satellite,                                    m_FallingPair.verticalOffset,                                    isSatelliteRotating ());}////// \brief Sets the amoeba at a given grid's position.////// \param x The X position to set the amoeba to./// \param y The Y position to set the amoeba to./// \param amoeba The amoeba to set at grid's position \p x and \p y.///voidGrid::setAmoebaAt (int16_t x, int16_t y, Amoeba *amoeba){    if ( 0 <= x && x < k_GridWidth &&         0 <= y && y < k_GridHeight )    {        m_Grid[y * k_GridWidth + x] = amoeba;    }}////// \brief Sets the status of an amoeba at a given position.////// This checks the neighbors of the amoeba at grid's position \a x and \a y/// to check how many amoebas of the same colour there are.////// \param x The grid's X position of the amoeba to set the state to./// \param y The grid's Y position of the amoeba to set the state to./// \param neighbors If set to \a true, this function sets the state of///                  the neighbors amoebas.///voidGrid::setAmoebaStateAt (int16_t x, int16_t y, bool neighbors){    Amoeba *currentAmoeba = getAmoebaAt (x, y);    if ( 0 != currentAmoeba &&         Amoeba::ColourGhost != currentAmoeba->getColour () )    {        int state = Amoeba::StateNone;        if ( isOfSameColour (x, y - 1, currentAmoeba) )        {            state |= Amoeba::StateTop;        }        if ( isOfSameColour (x + 1, y, currentAmoeba) )        {            state |= Amoeba::StateRight;        }        if ( isOfSameColour (x, y + 1, currentAmoeba) )        {            state |= Amoeba::StateBottom;        }        if ( isOfSameColour (x - 1, y, currentAmoeba) )        {            state |= Amoeba::StateLeft;        }        currentAmoeba->setState (static_cast<Amoeba::State>(state));    }    // If we must check neighbors, set the state of the direct    // neighbors (i.e., no diagonals).    if ( neighbors )    {        setAmoebaStateAt (x, y - 1, false);        setAmoebaStateAt (x + 1, y, false);        setAmoebaStateAt (x, y + 1, false);        setAmoebaStateAt (x - 1, y, false);    }}////// \brief Gets the next pair from the inactive list and sets it to the queue.////// The next pair will be the last element in the queue, so it should/// be called when the first element of the queue becomes the falling pair.////// If the inactive amoebas list becomes empty, this function request/// to the pair generator a new pair.////// This function is used when moving the queue step by step instead of/// just setting the queued amoeba behind the scene.///voidGrid::scheduleFallingPair (void){    assert ( 0 != m_Generator &&            "Tried to get the next pair without a generator.");    assert ( 2 <= m_InactiveAmoebas.size () &&            "Tried to get the next pair, but the inactive list is empty.");    // Gets the next pair from the inactive list.    Amoeba *main = m_InactiveAmoebas.front ();    m_InactiveAmoebas.pop_front ();    Amoeba *satellite = m_InactiveAmoebas.front ();    m_InactiveAmoebas.pop_front ();    // And set them to queued.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?