trainingstate.cpp
来自「Source code (C++) of the Amoebax game fo」· C++ 代码 · 共 707 行 · 第 1/2 页
CPP
707 行
//// Cross-platform free Puyo-Puyo clone.// Copyright (C) 2006, 2007 Emma's Software//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.//#if defined (HAVE_CONFIG_H)#include <config.h>#endif // HAVE_CONFIG_H#include <algorithm>#include <cassert>#include <SDL.h>#include <sstream>#include "Amoeba.h"#include "DrawAmoeba.h"#include "DrawChainLabel.h"#include "File.h"#include "HumanPlayer.h"#include "NewHighScoreState.h"#include "Options.h"#include "System.h"#include "TrainingState.h"using namespace Amoebax;/// The speed in which the "Level Up!" label goes up (px. / 100ms.)static const float k_LevelUpSpeed = 3.0f / 100.0f;/// The size of the silhouettes' border in pixels.static const uint8_t k_SilhouetteBorder = 8;namespace Amoebax{ /// /// \struct Level /// \brief Definition of training levels. /// const static struct Level { /// The minimum score points to have to be in this level. uint32_t minScore; /// The probability to add ghost amoebas each time (in %). uint8_t probabilityToAdd; /// The max number of ghost amobeas that can be added each time (0-30). uint8_t maxAmoebas; } /// The possible levels and the score to have to "level up!" k_Levels[] = { { 0, 0, 0}, { 440, 20, 1}, { 1240, 25, 1}, { 2200, 30, 1}, { 3712, 30, 2}, { 5896, 40, 1}, { 8080, 40, 2}, {10000, 45, 1}, {10500, 30, 3}, {13000, 50, 2}, {16000, 35, 4}, {18500, 40, 4}, {21000, 45, 4}, {23500, 40, 5}, {26000, 60, 6} };}const static uint8_t k_NumLevels = sizeof (k_Levels) / sizeof (k_Levels[0]);////// \brief Default constructor.///TrainingState::TrainingState (void): IState (), m_Amoebas (0), m_AmoebasSize (k_MaxAmoebasSize), m_Background (0), m_BackgroundMusic (Music::fromFile (File::getMusicFilePath ("training.ogg"))), m_ChainLabel (0), m_CurrentLevel (1), m_GameOver (0), m_Generator (new PairGenerator ()), m_Go (0), m_GoTime (k_LevelUpTime / 4), m_LevelUp (0), m_LevelUpPosition (0.0f), m_LevelUpSound (Sound::fromFile (File::getSoundFilePath ("levelup.wav"))), m_LevelUpTime (0), m_Player (new HumanPlayer (IPlayer::RightSide)), m_Ready (0), m_ReadyTime (k_LevelUpTime / 2), m_ScoreFont (0), m_SilhouetteBorder (k_SilhouetteBorder), m_Silhouettes (0), m_SoundLose (Sound::fromFile (File::getSoundFilePath ("youlose.wav"))){ loadGraphicResources (); const float screenScale = System::getInstance ().getScreenScaleFactor (); getPlayer ()->setGrid ( new Grid (static_cast<uint16_t>(k_PositionXGrid * screenScale), static_cast<uint16_t>(k_PositionYGrid * screenScale), static_cast<uint16_t>(k_PositionXQueue * screenScale), static_cast<uint16_t>(k_PositionYQueue * screenScale), static_cast<uint16_t>(k_PositionXWaiting * screenScale), static_cast<uint16_t>(k_PositionYWaiting * screenScale), getAmoebasSize (), Grid::QueueSideRight)); // Create the pair generator, add the grids and then generate 4 pair. m_Generator->addGrid (getPlayerGrid ()); m_Generator->generate (4);}voidTrainingState::activate (void){ getPlayer ()->loadOptions (); if ( Music::isPaused () ) { Music::resume (); } else { m_BackgroundMusic->play (); }}////// \brief Adds ghost amoebas to the grid.////// Based on the current level and its constrains, this/// function adds at random ghost amoebas at the grid.///voidTrainingState::addGhostAmoebas (void){ const Level ¤tLevel = k_Levels[getCurrentLevel () - 1]; if ( currentLevel.maxAmoebas != 0 && (rand () % 101) < currentLevel.probabilityToAdd ) { getPlayerGrid ()->incrementNumberOfWaitingGhosts ((rand () % currentLevel.maxAmoebas) + 1); }}////// \brief Checks if the current level should be changed.///voidTrainingState::checkLevelChange (void){ // If we are not at the last level, and the current grid's score // passes the minimum score for the next level, then incrememnt // the current level and prepare the label. bool levelChanged = false; if ( getCurrentLevel () < k_NumLevels ) { while ( getCurrentLevel () < k_NumLevels && getPlayerGrid ()->getScore () > k_Levels[getCurrentLevel ()].minScore ) { incrementCurrentLevel (); levelChanged = true; } if ( levelChanged ) { m_LevelUpSound->play (); m_LevelUpPosition = Options::getInstance ().getScreenHeight () / 2; m_LevelUpTime = k_LevelUpTime; } }}////// \brief Gets the size of an amoeba.////// \return The size (the same for width and height) of an amoeba.///inline uint8_tTrainingState::getAmoebasSize (void) const{ return m_AmoebasSize;}////// \brief Gets the current training's level.////// \return The current level the player is training.///inline uint16_tTrainingState::getCurrentLevel (void) const{ return m_CurrentLevel;}////// \brief Gets the time to display the "Go!" label.////// \return The time that the "Go!" label should be visible.///inline int32_tTrainingState::getGoTime (void) const{ return m_GoTime;}////// \brief Gets the training player.////// \return The training's player.///inline IPlayer *TrainingState::getPlayer (void) const{ return m_Player.get ();}////// \brief Gets the player's grid.////// \return The player's grid.///inline Grid *TrainingState::getPlayerGrid (void) const{ IPlayer *player = getPlayer (); assert ( 0 != player && "There's no player to get the grid from." ); return player->getGrid ();}////// \brief Gets the time to display the "Ready?" label.////// \return The time that the "Ready?" label should be visible.///inline int32_tTrainingState::getReadyTime (void) const{ return m_ReadyTime;}////// \brief Gets the size of the silhouettes' border.////// \return The size of the silhouettes's border.///inline uint8_tTrainingState::getSilhouetteBorder (void) const{ return m_SilhouetteBorder;}////// \brief Increments the current level.////// \param amount The amount to increment the current level.///inline voidTrainingState::incrementCurrentLevel (uint8_t amount){ setCurrentLevel (getCurrentLevel () + amount);}voidTrainingState::joyMotion (uint8_t joystick, uint8_t axis, int16_t value){ getPlayer ()->joyMotion (joystick, axis, value);}voidTrainingState::joyDown (uint8_t joystick, uint8_t button){#if defined (IS_GP2X_HOST)&& !defined (__SYMBIAN32__) switch (button) { case GP2X_BUTTON_START: case GP2X_BUTTON_X: pauseOrEndGame (); break; default: getPlayer ()->joyDown (joystick, button); break; }#else // !IS_GP2X_HOST getPlayer ()->joyDown (joystick, button);#endif // IS_GP2X_HOST}voidTrainingState::joyUp (uint8_t joystick, uint8_t button){ getPlayer ()->joyUp (joystick, button);}#if !defined (IS_GP2X_HOST)|| defined (__SYMBIAN32__)voidTrainingState::keyDown (uint32_t key){ if ( SDLK_ESCAPE == key ) { pauseOrEndGame (); } else if ( getPlayerGrid ()->isFilled () && (SDLK_RETURN == key || SDLK_F5 == key)) { pauseOrEndGame (); } else { getPlayer ()->keyDown (key); }}voidTrainingState::keyUp (uint32_t key){ getPlayer ()->keyUp (key);}#endif // !IS_GP2X_HOST////// \brief Loads all graphic resources.///voidTrainingState::loadGraphicResources (void){ const float screenScale = System::getInstance ().getScreenScaleFactor (); m_Amoebas.reset ( Surface::fromFile (File::getGraphicsFilePath ("amoebas.png"))); m_Amoebas->resize (screenScale); m_Background.reset ( Surface::fromFile (File::getGraphicsFilePath ("menuBackground.png"))); { std::auto_ptr<Surface> gridBackground ( Surface::fromFile (File::getGraphicsFilePath ("training.png"))); gridBackground->blit (m_Background->toSDLSurface ()); } m_Background->resize (screenScale); m_ChainLabel.reset ( Surface::fromFile (File::getGraphicsFilePath ("chain.png")));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?