trainingstate.cpp

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

CPP
707
字号
    m_ChainLabel->resize (screenScale);    m_GameOver.reset (            Surface::fromFile (File::getGraphicsFilePath ("gameover.png")));    m_GameOver->resize (screenScale);    m_LevelUp.reset (            Surface::fromFile (File::getGraphicsFilePath ("levelup.png")));    m_LevelUp->resize (screenScale);    m_Silhouettes.reset (            Surface::fromFile (File::getGraphicsFilePath ("silhouettes.png")));    m_Silhouettes->resize (screenScale);    m_ScoreFont.reset (Font::fromFile (File::getFontFilePath ("score")));    // Load the "Go!" and "Ready?" labels only if they are going to be shown.    if ( mustShowInitialLabels () )    {        m_Go.reset (Surface::fromFile (File::getGraphicsFilePath ("go.png")));        m_Ready.reset (                Surface::fromFile (File::getGraphicsFilePath ("ready.png")));    }    // Set the current amoeba's size and silhouette border size.    setAmoebasSize (static_cast<uint8_t> (k_MaxAmoebasSize * screenScale));    setSilhouetteBorder (static_cast<uint8_t> (screenScale *                                               k_SilhouetteBorder));}////// \brief Tells if the "Go!!" label should be displayed.////// \return \a true If the "Go!!" label should be displayed,///         \a false otherwise.///inline boolTrainingState::mustShowGoLabel (void) const{    return getGoTime () > 0;}////// \brief Tells if either the "Go!!" or the "Ready?" labels should be shown.////// \return \a true if either label must be displayed, \a false otherwise.///inline boolTrainingState::mustShowInitialLabels (void) const{    return mustShowGoLabel () || mustShowReadyLabel ();}////// \brief Tells if the "Ready?" label should be displayed.////// \return \a true If the "Ready?" label should be displayed,///         \a false otherwise.///inline boolTrainingState::mustShowReadyLabel (void) const{    return getReadyTime () > 0;}////// \brief Pauses or ends the game.////// If the game is not over it gets paused, otherwise return to the main menu/// except in the case that the score is a high score. In which case the system/// prompts the player's name before going to the main menu.///voidTrainingState::pauseOrEndGame (void){    if ( getPlayerGrid ()->isFilled () )    {        System::getInstance ().removeActiveState ();        if ( Options::getInstance ().isHighScore (getPlayerGrid ()->getScore ()) )        {            System::getInstance ()                .setActiveState (new NewHighScoreState (getPlayerGrid ()->getScore ()));        }    }    else    {        System::getInstance ().pause ();    }}////// \brief Sets the size of the amoebas.////// \param size The size of each amoeba. The size is the same for the///             width and for the height.///inline voidTrainingState::setAmoebasSize (uint8_t size){    m_AmoebasSize = size;}////// \brief Sets the current state.////// \param level The new current state.///inline voidTrainingState::setCurrentLevel (uint8_t level){    m_CurrentLevel = level;}////// \brief Sets the time to display the "Go!!" label.////// \param time The time to set as time to show the "Go!!" label. If it's///             negative, the "Go!!" label gets deleted.///inline voidTrainingState::setGoTime (int32_t time){    m_GoTime = time;    if ( 0 > time )    {        m_Go.reset (0);    }}////// \brief Sets the time to show the "Ready?" label.////// \param time The time to set to show the "Ready?" label. If it's negative///             the "Ready?" label gets deleted.///inline voidTrainingState::setReadyTime (int32_t time){    m_ReadyTime = time;    if ( 0 > time )    {        m_Ready.reset (0);    }}////// \brief Sets the size of the silhouettes' border.////// \param border The size of the silhouettes' border.///inline voidTrainingState::setSilhouetteBorder (uint8_t border){    m_SilhouetteBorder = border;}voidTrainingState::redrawBackground (SDL_Rect *region, SDL_Surface *screen){    m_Background->blit (region->x, region->y, region->w, region->h,                        region->x, region->y, screen);}voidTrainingState::render (SDL_Surface *screen){    // Get the clip rectangle of the grid, the queued amoebas, and    // the waiting ghost amoebas.    SDL_Rect gridRectangle;    gridRectangle.x = getPlayerGrid ()->getGridPositionX ();    gridRectangle.y = getPlayerGrid ()->getGridPositionY ();    gridRectangle.w = getPlayerGrid ()->getQueuePositionX () +                      getAmoebasSize () - gridRectangle.x;    gridRectangle.h = getAmoebasSize () * Grid::k_VisibleHeight;    SDL_Rect queueRectangle;    queueRectangle.x = getPlayerGrid ()->getQueuePositionX ();    queueRectangle.y = getPlayerGrid ()->getQueuePositionY ();    queueRectangle.w = getAmoebasSize ();    queueRectangle.h = 4 * getAmoebasSize ();    SDL_Rect waitingGhosts;    waitingGhosts.x = getPlayerGrid ()->getWaitingGhostPositionX ();    waitingGhosts.y = getPlayerGrid ()->getWaitingGhostPositionY ();    waitingGhosts.w = getAmoebasSize () * Grid::k_GridWidth;    waitingGhosts.h = getAmoebasSize ();    // Set the clip rectangle of the grid.    SDL_SetClipRect (screen, &gridRectangle);    // Draw the main falling amoeba's silhouette.    const Grid::FallingAmoeba mainAmoeba =        getPlayerGrid ()->getFallingMainAmoeba ();    if ( 0 != mainAmoeba.amoeba )    {        int8_t silhouetteFrame = getPlayerGrid ()->getSilhouetteFrame ();        if ( 0 < silhouetteFrame )        {            Amoeba *amoeba = mainAmoeba.amoeba;            uint8_t silhouetteSize = getAmoebasSize () +                                     2 * getSilhouetteBorder ();            m_Silhouettes->blit (silhouetteSize * silhouetteFrame,                    silhouetteSize * amoeba->getColour (),                    silhouetteSize, silhouetteSize,                    amoeba->getX () - getSilhouetteBorder (),                    amoeba->getY () - getSilhouetteBorder (),                    screen);        }    }    // Draw grid's amoebas.    const std::list<Amoeba *> &activeAmoebas = getPlayerGrid ()->getActiveAmoebas ();    for_each (activeAmoebas.begin (), activeAmoebas.end (),            DrawAmoeba (getAmoebasSize (), m_Amoebas.get (), screen));    // Set the clip rectangle for the qeued amoebas.    SDL_SetClipRect (screen, &queueRectangle);    const std::list<Amoeba *> &queuedAmoebas = getPlayerGrid ()->getQueuedAmoebas ();    for_each (queuedAmoebas.begin (), queuedAmoebas.end (),            DrawAmoeba (getAmoebasSize (), m_Amoebas.get (), screen));    // Set the clip rectangle for the ghost amoebas.    SDL_SetClipRect (screen, &waitingGhosts);    // Draw ghosts amoebas.    const std::vector<Amoeba *> &ghostAmoebas =        getPlayerGrid ()->getWaitingGhostAmoebas ();    for_each (ghostAmoebas.begin (), ghostAmoebas.end (),              DrawAmoeba (getAmoebasSize (), m_Amoebas.get (), screen));    // Remove the clipping area.    SDL_SetClipRect (screen, 0);    // Draw chain labels.    const std::list<ChainLabel *> &chainLabels = getPlayerGrid ()->getChainLabels ();    for_each (chainLabels.begin (), chainLabels.end (),              DrawChainLabel (m_ChainLabel.get (), m_ScoreFont.get (), screen));    // Draw the current score and the current level, both right aligned to    // its position.    const float scaleFactor = System::getInstance ().getScreenScaleFactor ();    std::string scoreString;
	char numstring[12];//    scoreString << 15;// getPlayerGrid ()->getScore ();	sprintf(numstring,"%d", getPlayerGrid ()->getScore ());
	scoreString.append(numstring);
    uint16_t scoreWidth = m_ScoreFont->getTextWidth (scoreString);    uint16_t scoreX = static_cast<uint16_t>(k_PositionXScore * scaleFactor) -                      scoreWidth;    uint16_t scoreY = static_cast<uint16_t>(k_PositionYScore * scaleFactor);    std::string levelString;    //levelString << getCurrentLevel ();
	sprintf(numstring,"%d", getCurrentLevel ());
	levelString.append(numstring);    uint16_t levelWidth = m_ScoreFont->getTextWidth (levelString);    uint16_t levelX = static_cast<uint16_t>(k_PositionXLevel * scaleFactor) -                      levelWidth;    uint16_t levelY = static_cast<uint16_t>(k_PositionYLevel * scaleFactor);    m_ScoreFont->write (scoreString, scoreX, scoreY, screen);    m_ScoreFont->write (levelString, levelX, levelY, screen);    // Draw the 'Level Up!' label if it must be show.    if ( m_LevelUpTime > 0 )    {        m_LevelUp->blit (getPlayerGrid ()->getGridPositionX () +                         getAmoebasSize () * Grid::k_GridWidth / 2 -                         m_LevelUp->getWidth () / 2,                         static_cast<int16_t>(m_LevelUpPosition), screen);    }    // Draw the 'Ready?' or 'Go!!' labels if they must be shown.    if ( mustShowInitialLabels () )    {        if ( mustShowReadyLabel () )        {            m_Ready->blit (getPlayerGrid ()->getGridPositionX () +                           getAmoebasSize () * Grid::k_GridWidth / 2 -                           m_Ready->getWidth () / 2,                           getPlayerGrid ()->getGridPositionY () +                           getAmoebasSize () * (Grid::k_VisibleHeight - 1) / 2 -                           m_Ready->getHeight () / 2, screen);        }        else        {            m_Go->blit (getPlayerGrid ()->getGridPositionX () +                        getAmoebasSize () * Grid::k_GridWidth / 2 -                        m_Go->getWidth () / 2,                        getPlayerGrid ()->getGridPositionY () +                        getAmoebasSize () * (Grid::k_VisibleHeight - 1) / 2 -                        m_Go->getHeight () / 2, screen);        }    }    // Draw the 'Game Over' text if the board is filled.    if ( getPlayerGrid ()->isFilled () )    {        m_GameOver->blit (gridRectangle.x + getAmoebasSize () *                          Grid::k_GridWidth / 2  -                          m_GameOver->getWidth () / 2,                          gridRectangle.y + gridRectangle.h / 2 -                          m_GameOver->getHeight () / 2,                          screen);    }}voidTrainingState::update (uint32_t elapsedTime){    if ( mustShowInitialLabels () )    {        if ( mustShowReadyLabel () )        {            setReadyTime (getReadyTime () - elapsedTime);        }        else        {            setGoTime (getGoTime () - elapsedTime);        }    }    else if ( !getPlayerGrid ()->isFilled () )    {        getPlayer ()->update (elapsedTime);        // If the game is over, stop the music and play the lose sound.        if ( getPlayerGrid ()->isFilled () )        {            m_BackgroundMusic->stop ();            m_SoundLose->play ();        }        // If the grid has a new falling pair (i.e., has no floating amoebas        // and the previous pair alredy felt.), then add ghost amoebas        // based on the current level and check if we must change the current        // level.        else if ( getPlayerGrid ()->hasNewFallingPair () )        {            addGhostAmoebas ();            checkLevelChange ();        }        // Otherwise, just update the "Level Up!" labe, in case it must be        // shown.        else if ( m_LevelUpTime > 0 )        {            m_LevelUpPosition -= k_LevelUpSpeed * elapsedTime;            m_LevelUpTime -= elapsedTime;        }    }}voidTrainingState::videoModeChanged (void){    loadGraphicResources ();}

⌨️ 快捷键说明

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