twoplayersstate.cpp
来自「Source code (C++) of the Amoebax game fo」· C++ 代码 · 共 804 行 · 第 1/2 页
CPP
804 行
m_Go.reset (Surface::fromFile (File::getGraphicsFilePath ("go.png"))); m_Ready.reset ( Surface::fromFile (File::getGraphicsFilePath ("ready.png"))); }}////// \brief Tells if the "Go!!" label should be displayed.////// \return \a true If the "Go!!" label should be displayed,/// \a false otherwise.///inline boolTwoPlayersState::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 boolTwoPlayersState::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 boolTwoPlayersState::mustShowReadyLabel (void) const{ return getReadyTime () > 0;}voidTwoPlayersState::redrawBackground (SDL_Rect *region, SDL_Surface *screen){ m_Background->blit (region->x, region->y, region->w, region->h, region->x, region->y, screen);}voidTwoPlayersState::render (SDL_Surface *screen){ // Draw left grid's amoebas. { SDL_Rect gridRectangle; gridRectangle.x = getLeftGrid ()->getGridPositionX (); gridRectangle.y = getLeftGrid ()->getGridPositionY (); gridRectangle.w = getLeftGrid ()->getQueuePositionX () + getAmoebasSize () - gridRectangle.x; gridRectangle.h = getAmoebasSize () * Grid::k_VisibleHeight; SDL_SetClipRect (screen, &gridRectangle); // Draw the main falling amoeba's silhouette. const Grid::FallingAmoeba mainAmoeba = getLeftGrid ()->getFallingMainAmoeba (); if ( 0 != mainAmoeba.amoeba ) { int8_t silhouetteFrame = getLeftGrid ()->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); } } const std::list<Amoeba *> &leftActiveAmoebas = getLeftGrid ()->getActiveAmoebas (); std::for_each (leftActiveAmoebas.begin (), leftActiveAmoebas.end (), DrawAmoeba (getAmoebasSize (), m_Amoebas.get (), screen)); } // Draw left queued amoebas. { SDL_Rect queueRectangle; queueRectangle.x = getLeftGrid ()->getQueuePositionX (); queueRectangle.y = getLeftGrid ()->getQueuePositionY (); queueRectangle.w = getAmoebasSize (); queueRectangle.h = 4 * getAmoebasSize (); SDL_SetClipRect (screen, &queueRectangle); const std::list<Amoeba *> &queuedAmoebas = getLeftGrid ()->getQueuedAmoebas (); for_each (queuedAmoebas.begin (), queuedAmoebas.end (), DrawAmoeba (getAmoebasSize (), m_Amoebas.get (), screen)); } // Draw left waiting amoebas. { SDL_Rect waitingGhosts; waitingGhosts.x = getLeftGrid ()->getWaitingGhostPositionX (); waitingGhosts.y = getLeftGrid ()->getWaitingGhostPositionY (); waitingGhosts.w = getAmoebasSize () * Grid::k_GridWidth; waitingGhosts.h = getAmoebasSize (); SDL_SetClipRect (screen, &waitingGhosts); const std::vector<Amoeba *> &leftGhostAmoebas = getLeftGrid ()->getWaitingGhostAmoebas (); std::for_each (leftGhostAmoebas.begin (), leftGhostAmoebas.end (), DrawAmoeba (getAmoebasSize (), m_Amoebas.get (), screen)); } // Draw right grid's amoebas. { SDL_Rect gridRectangle; gridRectangle.x = getRightGrid ()->getQueuePositionX (); gridRectangle.y = getRightGrid ()->getQueuePositionY (); gridRectangle.w = getRightGrid ()->getGridPositionY () + getAmoebasSize () * Grid::k_GridWidth - gridRectangle.x; gridRectangle.h = getAmoebasSize () * Grid::k_VisibleHeight; SDL_SetClipRect (screen, &gridRectangle); // Draw the main falling amoeba's silhouette. const Grid::FallingAmoeba mainAmoeba = getRightGrid ()->getFallingMainAmoeba (); if ( 0 != mainAmoeba.amoeba ) { int8_t silhouetteFrame = getRightGrid ()->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); } } const std::list<Amoeba *> &rightActiveAmoebas = getRightGrid ()->getActiveAmoebas (); std::for_each (rightActiveAmoebas.begin (), rightActiveAmoebas.end (), DrawAmoeba (getAmoebasSize (), m_Amoebas.get (), screen)); } // Draw right queued amoebas. { SDL_Rect queueRectangle; queueRectangle.x = getRightGrid ()->getQueuePositionX (); queueRectangle.y = getRightGrid ()->getQueuePositionY (); queueRectangle.w = getAmoebasSize (); queueRectangle.h = 4 * getAmoebasSize (); SDL_SetClipRect (screen, &queueRectangle); const std::list<Amoeba *> &queuedAmoebas = getRightGrid ()->getQueuedAmoebas (); for_each (queuedAmoebas.begin (), queuedAmoebas.end (), DrawAmoeba (getAmoebasSize (), m_Amoebas.get (), screen)); } // Draw right waiting amoebas. { SDL_Rect waitingGhosts; waitingGhosts.x = getRightGrid ()->getWaitingGhostPositionX (); waitingGhosts.y = getRightGrid ()->getWaitingGhostPositionY (); waitingGhosts.w = getAmoebasSize () * Grid::k_GridWidth; waitingGhosts.h = getAmoebasSize (); SDL_SetClipRect (screen, &waitingGhosts); const std::vector<Amoeba *> &rightGhostAmoebas = getRightGrid ()->getWaitingGhostAmoebas (); std::for_each (rightGhostAmoebas.begin (), rightGhostAmoebas.end (), DrawAmoeba (getAmoebasSize (), m_Amoebas.get (), screen)); } // Remove any clipping ares. SDL_SetClipRect (screen, 0); // Draw all grids' chain labels. { const std::list<ChainLabel *> rightChainLabels = getRightGrid()->getChainLabels (); std::list<ChainLabel *> chainLabels (getLeftGrid ()->getChainLabels ()); std::copy (rightChainLabels.begin (), rightChainLabels.end (), std::back_inserter (chainLabels)); std::for_each (chainLabels.begin (), chainLabels.end (), DrawChainLabel (m_ChainLabel.get (), m_ScoreFont.get (), screen)); } // Draw the current players' score. The right player has its // score right aligned and the left player left aligned. const float scaleFactor = System::getInstance ().getScreenScaleFactor (); char numstring[12];
std::string leftScoreString; //leftScoreString << getLeftGrid ()->getScore ();
sprintf(numstring,"%d", getLeftGrid ()->getScore ());
leftScoreString.append(numstring);
uint16_t leftScoreX = static_cast<uint16_t>(k_PositionXLeftScore * scaleFactor); uint16_t leftScoreY = static_cast<uint16_t>(k_PositionYLeftScore * scaleFactor); std::string rightScoreString; //rightScoreString << getRightGrid ()->getScore ();
sprintf(numstring,"%d", getRightGrid ()->getScore ());
rightScoreString.append(numstring);
uint16_t rightScoreWidth = m_ScoreFont->getTextWidth (rightScoreString); uint16_t rightScoreX = static_cast<uint16_t>(k_PositionXRightScore * scaleFactor) - rightScoreWidth; uint16_t rightScoreY = static_cast<uint16_t>(k_PositionYRightScore * scaleFactor); m_ScoreFont->write (leftScoreString, leftScoreX, leftScoreY, screen); m_ScoreFont->write (rightScoreString, rightScoreX, rightScoreY, screen); // Draw the 'You Lose', 'You Win!!' labels if the game is over. if ( gameIsOver () ) { Surface *leftPlayer = IPlayer::LeftSide == getWinnerSide () ? m_YouWin.get () : m_YouLose.get (); leftPlayer->blit (getLeftGrid ()->getGridPositionX () + getAmoebasSize () * Grid::k_GridWidth / 2 - leftPlayer->getWidth () / 2, getLeftGrid ()->getGridPositionY () + getAmoebasSize () * Grid::k_VisibleHeight / 2 - leftPlayer->getHeight () / 2, screen); Surface *rightPlayer = IPlayer::RightSide == getWinnerSide () ? m_YouWin.get () : m_YouLose.get (); rightPlayer->blit (getRightGrid ()->getGridPositionX () + getAmoebasSize () * Grid::k_GridWidth / 2 - rightPlayer->getWidth () / 2, getRightGrid ()->getGridPositionY () + getAmoebasSize () * Grid::k_VisibleHeight / 2 - rightPlayer->getHeight () / 2, screen); } else if ( mustShowInitialLabels () ) { if ( mustShowReadyLabel () ) { m_Ready->blit (getLeftGrid ()->getGridPositionX () + getAmoebasSize () * Grid::k_GridWidth / 2 - m_Ready->getWidth () / 2, getLeftGrid ()->getGridPositionY () + getAmoebasSize () * (Grid::k_VisibleHeight - 1) / 2 - m_Ready->getHeight () / 2, screen); m_Ready->blit (getRightGrid ()->getGridPositionX () + getAmoebasSize () * Grid::k_GridWidth / 2 - m_Ready->getWidth () / 2, getRightGrid ()->getGridPositionY () + getAmoebasSize () * (Grid::k_VisibleHeight - 1) / 2 - m_Ready->getHeight () / 2, screen); } else { m_Go->blit (getLeftGrid ()->getGridPositionX () + getAmoebasSize () * Grid::k_GridWidth / 2 - m_Go->getWidth () / 2, getLeftGrid ()->getGridPositionY () + getAmoebasSize () * (Grid::k_VisibleHeight - 1) / 2 - m_Go->getHeight () / 2, screen); m_Go->blit (getRightGrid ()->getGridPositionX () + getAmoebasSize () * Grid::k_GridWidth / 2 - m_Go->getWidth () / 2, getRightGrid ()->getGridPositionY () + getAmoebasSize () * (Grid::k_VisibleHeight - 1) / 2 - m_Go->getHeight () / 2, screen); } }}////// \brief Sets the size of the amoebas.////// \param size The new size of the amoebas. The amoebas are squared,/// so this size if the same for the width and the height.///inline voidTwoPlayersState::setAmoebasSize (uint8_t size){ assert ( 0 < size && "Tried to set an invalid amoebas' size." ); m_AmoebasSize = size;}////// \brief The game is over.////// Checks which is the winner player's side and marks the/// game as over. If there's an attached observer, also/// notifies it.///voidTwoPlayersState::setGameOver (void){ m_GameIsOver = true; m_Winner = getLeftGrid ()->isFilled () ? IPlayer::RightSide : IPlayer::LeftSide; if ( m_Observer != 0 ) { m_Observer->endOfMatch (m_Winner, getLeftGrid ()->getScore (), getRightGrid ()->getScore ()); }}////// \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 voidTwoPlayersState::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 voidTwoPlayersState::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 voidTwoPlayersState::setSilhouetteBorder (uint8_t border){ m_SilhouetteBorder = border;}voidTwoPlayersState::update (uint32_t elapsedTime){ if ( !gameIsOver () ) { if ( mustShowInitialLabels () ) { if ( mustShowReadyLabel () ) { setReadyTime (getReadyTime () - elapsedTime); } else { setGoTime (getGoTime () - elapsedTime); } } else { getLeftPlayer ()->update (elapsedTime); getRightPlayer ()->update (elapsedTime); // Update the waiting ghost amoebas from the opponent. getLeftGrid ()->incrementNumberOfWaitingGhosts ( getRightGrid ()->getOpponentGhostAmoebas ()); getRightGrid ()->incrementNumberOfWaitingGhosts ( getLeftGrid ()->getOpponentGhostAmoebas ()); if (getRightGrid ()->isFilled () || getLeftGrid ()->isFilled ()) { setGameOver (); Music::stop (); } } }}voidTwoPlayersState::videoModeChanged (void){ loadGraphicsResources ();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?