grid.cpp

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

CPP
1,733 行
字号
    m_QueuedAmoebas.push_back (main);    m_QueuedAmoebas.push_back (satellite);    // Sets they position to be the last pair on the queue.    // Take into account the side where the queue is located at    // to choose the offset to apply to this pair.    uint16_t lastPairOffsetX = getAmoebaSize () / 2;    if ( LayoutVertical == getLayout () )    {        if ( Grid::QueueSideLeft == getQueueSide () )        {            lastPairOffsetX = -lastPairOffsetX;        }        main->setX (getQueuePositionX () + lastPairOffsetX);        main->setY (getQueuePositionY () + getAmoebaSize () * 5);        satellite->setX (getQueuePositionX () + lastPairOffsetX);        satellite->setY (getQueuePositionY () + getAmoebaSize () * 4);    }    else    {        if (Grid::QueueSideLeft == getQueueSide () )        {            main->setX (getQueuePositionX () - getAmoebaSize () * 6);            main->setY (getQueuePositionY () + lastPairOffsetX);            satellite->setX (getQueuePositionX () - getAmoebaSize () * 5);            satellite->setY (getQueuePositionY () + lastPairOffsetX);        }        else        {            main->setX (getQueuePositionX () + getAmoebaSize () * 5);            main->setY (getQueuePositionY () - getAmoebaSize () - lastPairOffsetX);            satellite->setX (getQueuePositionX () + getAmoebaSize () * 4);            satellite->setY (getQueuePositionY () - getAmoebaSize () - lastPairOffsetX);        }    }    // And set up the falling pair structure for when they fall.    FallingPair fallingPair;    fallingPair.main.amoeba = main;    fallingPair.main.x = 3;    fallingPair.main.y = k_FirstVisibleHeight - 2;    fallingPair.satellite.amoeba = satellite;    fallingPair.satellite.x = 3;    fallingPair.satellite.y = k_FirstVisibleHeight - 3;    fallingPair.verticalOffset = 0;    fallingPair.fallingTime = k_DefaultFallingTime;    m_Queue.push_back (fallingPair);    // Request a new pair if we are out of inactive pairs.    if ( m_InactiveAmoebas.empty () )    {        m_Generator->generate ();    }    // Tells that the queue is moving.    m_RemainingQueueHorizontalOffset = getAmoebaSize () / 2;    m_RemainingQueueVerticalOffset = 2 * getAmoebaSize ();    m_QueueIsMoving = true;}////// \brief Sets the global screen position to a falling amoeba.////// \param fallingAmoeba The falling amoeba to set its screen position./// \param verticalOffset The offset to add to the amoeba's Y coordinate./// \param rotating Tells if the amoeba is rotating and has to take into///                 account the current rotation degrees.  Used for the///                 satellite.///voidGrid::setFallingAmoebaScreenPosition (FallingAmoeba &fallingAmoeba,                                      int16_t verticalOffset, bool rotating){    int16_t x = getGridPositionX ();    int16_t y = getGridPositionY ();    if (Grid::LayoutVertical == getLayout ())    {        y += verticalOffset  - getAmoebaSize () * k_FirstVisibleHeight;        if ( rotating )        {            float rotationX = k_Cos[getCurrentRotationDegree ()];            float rotationY = k_Sin[getCurrentRotationDegree ()];            int16_t directionX = 0;            int16_t directionY = 0;            if ( 0 <= getCurrentRotationDegree () &&                      getCurrentRotationDegree () < 90 )            {                if ( m_RotationDirection < 0.0f )                {                    directionX = -1;                    directionY = 0;                }                else                {                    directionX = 0;                    directionY = 1;                }            }            else if ( 90 <= getCurrentRotationDegree () &&                            getCurrentRotationDegree () < 180 )            {                if ( m_RotationDirection < 0.0f )                {                    directionX = 0;                    directionY = 1;                }                else                {                    directionX = 1;                    directionY = 0;                }            }            else if ( 180 <= getCurrentRotationDegree () &&                             getCurrentRotationDegree () < 270 )            {                if ( m_RotationDirection < 0.0f )                {                    directionX = 1;                    directionY = 0;                }                else                {                    directionX = 0;                    directionY = -1;                }            }            else if ( getCurrentRotationDegree () >= 270 )            {                if ( m_RotationDirection < 0.0f )                {                    directionX = 0;                    directionY = -1;                }                else                {                    directionX = -1;                    directionY = 0;                }            }            x += static_cast<uint16_t> ((fallingAmoeba.x + directionX) *                    getAmoebaSize () +                    getAmoebaSize () * rotationX);            y += static_cast<uint16_t> ((fallingAmoeba.y + directionY) *                    getAmoebaSize () -                    getAmoebaSize () * rotationY);        }        else        {            x += fallingAmoeba.x * getAmoebaSize ();            y += fallingAmoeba.y * getAmoebaSize ();        }    }    else    {        float rotationY = k_Cos[getCurrentRotationDegree ()];        float rotationX = k_Sin[getCurrentRotationDegree ()];        int16_t directionY = 0;        int16_t directionX = 0;        if ( rotating )        {            if ( 0 <= getCurrentRotationDegree () &&                    getCurrentRotationDegree () < 90 )            {                if ( m_RotationDirection < 0.0f )                {                    directionY = -1;                    directionX = 0;                }                else                {                    directionY = 0;                    directionX = 1;                }            }            else if ( 90 <= getCurrentRotationDegree () &&                    getCurrentRotationDegree () < 180 )            {                if ( m_RotationDirection < 0.0f )                {                    directionY = 0;                    directionX = 1;                }                else                {                    directionY = 1;                    directionX = 0;                }            }            else if ( 180 <= getCurrentRotationDegree () &&                    getCurrentRotationDegree () < 270 )            {                if ( m_RotationDirection < 0.0f )                {                    directionY = 1;                    directionX = 0;                }                else                {                    directionY = 0;                    directionX = -1;                }            }            else if ( getCurrentRotationDegree () >= 270 )            {                if ( m_RotationDirection < 0.0f )                {                    directionY = 0;                    directionX = -1;                }                else                {                    directionY = -1;                    directionX = 0;                }            }        }        if (Grid::QueueSideLeft == getQueueSide ())        {            if (rotating)            {                x -= (fallingAmoeba.y + 1 + directionX) * getAmoebaSize () -                    static_cast<uint16_t> (getAmoebaSize () * rotationX) +                    verticalOffset;                y += (fallingAmoeba.x + directionY) * getAmoebaSize () +                    static_cast<uint16_t> (getAmoebaSize () * rotationY);            }            else            {                x -= (fallingAmoeba.y + 1) * getAmoebaSize () + verticalOffset;                y += fallingAmoeba.x * getAmoebaSize ();            }        }        else        {            if ( rotating )            {                x += (fallingAmoeba.y + directionX) * getAmoebaSize () -                    static_cast<uint16_t> (getAmoebaSize () * rotationX) +                    verticalOffset;                y -= (fallingAmoeba.x + 1 + directionY) * getAmoebaSize () +                    static_cast<uint16_t> (getAmoebaSize () * rotationY);            }            else            {                x += fallingAmoeba.y * getAmoebaSize () + verticalOffset;                y -= (fallingAmoeba.x + 1) * getAmoebaSize ();            }        }    }    fallingAmoeba.amoeba->setX (x);    fallingAmoeba.amoeba->setY (y);}////// \brief Sets the generator.////// When the list of waiting amoebas reaches zero, this generator/// will be the used to generate a new pair.////// \param generator The generator that the class is linked to and///                  will use to request more pairs.///voidGrid::setGenerator (PairGenerator *generator){    m_Generator = generator;}////// \brief Sets the falling speed to maximum.///voidGrid::setMaxFallingSpeed (void){    m_MaxFallingSpeed = true;}////// \brief Sets the status of the new falling pair.////// \param hasNewFallingPair Set it to \a true when a new falling pair///                          starts to fall. Otherwise set it to \a false./// \see hasNewFallingPair().///inline voidGrid::setHasNewFallingPair (bool hasNewFallingPair){    m_HasNewFallingPair = hasNewFallingPair;}////// \brief Sets the next falling pair.////// Gets the next falling pair from the list of waiting pairs and/// moves the last element to be the next.///voidGrid::setupFallingPair (void){    // Get the two first queued amoebas and set it to be active.    m_ActiveAmoebas.push_back (m_QueuedAmoebas.front ());    m_QueuedAmoebas.pop_front ();    m_ActiveAmoebas.push_back (m_QueuedAmoebas.front ());    m_QueuedAmoebas.pop_front ();    m_FallingPair = m_Queue.front ();    m_Queue.pop_front ();    // Set the screen position without any vertical offset. The grid    // position is set at getNextPair().    setFallingAmoebaScreenPosition (m_FallingPair.main, 0, false);    setFallingAmoebaScreenPosition (m_FallingPair.satellite, 0, false);    // Reset the current rotation.    m_CurrentRotationDegrees = 90.0f;    m_DegreesUntilRotationDone = 0.0f;    m_RotationDirection = 1.0f;    // Now set the remainig pair to be the next to fall and    // add a new pair.    FallingPair remainingPair (m_Queue.front ());    if ( Grid::LayoutVertical == getLayout () )    {        remainingPair.main.amoeba->setX (getQueuePositionX ());        remainingPair.main.amoeba->setY (getQueuePositionY () + getAmoebaSize ());        remainingPair.satellite.amoeba->setX (getQueuePositionX ());        remainingPair.satellite.amoeba->setY (getQueuePositionY ());    }    else    {        if (Grid::QueueSideLeft == getQueueSide () )        {            remainingPair.main.amoeba->setX (getQueuePositionX () - getAmoebaSize () * 2);            remainingPair.main.amoeba->setY (getQueuePositionY ());            remainingPair.satellite.amoeba->setX (getQueuePositionX () - getAmoebaSize ());            remainingPair.satellite.amoeba->setY (getQueuePositionY ());        }        else        {            remainingPair.main.amoeba->setX (getQueuePositionX () + getAmoebaSize ());            remainingPair.main.amoeba->setY (getQueuePositionY () - getAmoebaSize ());            remainingPair.satellite.amoeba->setX (getQueuePositionX ());            remainingPair.satellite.amoeba->setY (getQueuePositionY () - getAmoebaSize ());  

⌨️ 快捷键说明

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