| [2825] | 1 | /* | 
|---|
 | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  *                    > www.orxonox.net < | 
|---|
 | 4 |  * | 
|---|
 | 5 |  * | 
|---|
 | 6 |  *   License notice: | 
|---|
 | 7 |  * | 
|---|
 | 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
 | 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
 | 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
 | 11 |  *   of the License, or (at your option) any later version. | 
|---|
 | 12 |  * | 
|---|
 | 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
 | 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
 | 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
 | 16 |  *   GNU General Public License for more details. | 
|---|
 | 17 |  * | 
|---|
 | 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
 | 19 |  *   along with this program; if not, write to the Free Software | 
|---|
 | 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
 | 21 |  * | 
|---|
 | 22 |  *   Author: | 
|---|
 | 23 |  *      Fabian 'x3n' Landau | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
| [8108] | 29 | /** | 
|---|
 | 30 |     @file Pong.cc | 
|---|
 | 31 |     @brief Implementation of the Pong class. | 
|---|
 | 32 | */ | 
|---|
 | 33 |  | 
|---|
| [2825] | 34 | #include "Pong.h" | 
|---|
 | 35 |  | 
|---|
 | 36 | #include "core/CoreIncludes.h" | 
|---|
| [5929] | 37 | #include "core/EventIncludes.h" | 
|---|
| [7284] | 38 | #include "core/command/Executor.h" | 
|---|
| [9016] | 39 | #include "core/ConfigValueIncludes.h" | 
|---|
| [8108] | 40 |  | 
|---|
 | 41 | #include "gamestates/GSLevel.h" | 
|---|
| [9016] | 42 | #include "chat/ChatManager.h" | 
|---|
| [8108] | 43 |  | 
|---|
| [5725] | 44 | #include "PongCenterpoint.h" | 
|---|
 | 45 | #include "PongBall.h" | 
|---|
 | 46 | #include "PongBat.h" | 
|---|
 | 47 | #include "PongBot.h" | 
|---|
 | 48 | #include "PongAI.h" | 
|---|
| [2825] | 49 | namespace orxonox | 
|---|
 | 50 | { | 
|---|
| [8108] | 51 |     // Events to allow to react to scoring of a player, in the level-file. | 
|---|
| [5929] | 52 |     CreateEventName(PongCenterpoint, right); | 
|---|
 | 53 |     CreateEventName(PongCenterpoint, left); | 
|---|
| [6417] | 54 |  | 
|---|
| [2825] | 55 |     CreateUnloadableFactory(Pong); | 
|---|
 | 56 |  | 
|---|
| [8108] | 57 |     /** | 
|---|
 | 58 |     @brief | 
|---|
 | 59 |         Constructor. Registers and initializes the object. | 
|---|
 | 60 |     */ | 
|---|
| [2825] | 61 |     Pong::Pong(BaseObject* creator) : Deathmatch(creator) | 
|---|
 | 62 |     { | 
|---|
 | 63 |         RegisterObject(Pong); | 
|---|
 | 64 |  | 
|---|
 | 65 |         this->center_ = 0; | 
|---|
 | 66 |         this->ball_ = 0; | 
|---|
 | 67 |         this->bat_[0] = 0; | 
|---|
 | 68 |         this->bat_[1] = 0; | 
|---|
 | 69 |  | 
|---|
| [2890] | 70 |         this->setHUDTemplate("PongHUD"); | 
|---|
 | 71 |  | 
|---|
| [8108] | 72 |         // Pre-set the timer, but don't start it yet. | 
|---|
| [5929] | 73 |         this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Pong::startBall, this))); | 
|---|
| [2825] | 74 |         this->starttimer_.stopTimer(); | 
|---|
| [2839] | 75 |  | 
|---|
| [8108] | 76 |         // Set the type of Bots for this particular Gametype. | 
|---|
| [2839] | 77 |         this->botclass_ = Class(PongBot); | 
|---|
| [9258] | 78 |  | 
|---|
 | 79 |         this->scoreLimit_ = 10; | 
|---|
 | 80 |         this->setConfigValues(); | 
|---|
| [2825] | 81 |     } | 
|---|
 | 82 |  | 
|---|
| [8108] | 83 |     /** | 
|---|
 | 84 |     @brief | 
|---|
 | 85 |         Destructor. Cleans up, if initialized. | 
|---|
 | 86 |     */ | 
|---|
| [7911] | 87 |     Pong::~Pong() | 
|---|
 | 88 |     { | 
|---|
 | 89 |         if (this->isInitialized()) | 
|---|
 | 90 |             this->cleanup(); | 
|---|
 | 91 |     } | 
|---|
 | 92 |  | 
|---|
| [9258] | 93 |     void Pong::setConfigValues() | 
|---|
 | 94 |     { | 
|---|
 | 95 |         SetConfigValue(scoreLimit_, 10).description("The player first reaching those points wins."); | 
|---|
 | 96 |     } | 
|---|
 | 97 |  | 
|---|
| [8108] | 98 |     /** | 
|---|
 | 99 |     @brief | 
|---|
 | 100 |         Cleans up the Gametype by destroying the ball and the bats. | 
|---|
 | 101 |     */ | 
|---|
| [7911] | 102 |     void Pong::cleanup() | 
|---|
 | 103 |     { | 
|---|
| [8108] | 104 |         if (this->ball_ != NULL) // Destroy the ball, if present. | 
|---|
| [7911] | 105 |         { | 
|---|
 | 106 |             this->ball_->destroy(); | 
|---|
 | 107 |             this->ball_ = 0; | 
|---|
 | 108 |         } | 
|---|
 | 109 |  | 
|---|
| [8108] | 110 |         // Destroy both bats, if present. | 
|---|
| [7911] | 111 |         for (size_t i = 0; i < 2; ++i) | 
|---|
 | 112 |         { | 
|---|
| [8108] | 113 |             if (this->bat_[0] != NULL) | 
|---|
| [7911] | 114 |             { | 
|---|
 | 115 |                 this->bat_[0]->destroy(); | 
|---|
 | 116 |                 this->bat_[0] = 0; | 
|---|
 | 117 |             } | 
|---|
 | 118 |         } | 
|---|
 | 119 |     } | 
|---|
 | 120 |  | 
|---|
| [8108] | 121 |     /** | 
|---|
 | 122 |     @brief | 
|---|
 | 123 |         Starts the Pong minigame. | 
|---|
 | 124 |     */ | 
|---|
| [2825] | 125 |     void Pong::start() | 
|---|
 | 126 |     { | 
|---|
| [8108] | 127 |         if (this->center_ != NULL) // There needs to be a PongCenterpoint, i.e. the area the game takes place. | 
|---|
| [2825] | 128 |         { | 
|---|
| [8108] | 129 |             if (this->ball_ == NULL) // If there is no ball, create a new ball. | 
|---|
| [2825] | 130 |             { | 
|---|
 | 131 |                 this->ball_ = new PongBall(this->center_); | 
|---|
| [8108] | 132 |                 // Apply the template for the ball specified by the centerpoint. | 
|---|
| [2825] | 133 |                 this->ball_->addTemplate(this->center_->getBalltemplate()); | 
|---|
 | 134 |             } | 
|---|
 | 135 |  | 
|---|
| [8108] | 136 |             // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to. | 
|---|
| [2825] | 137 |             this->center_->attach(this->ball_); | 
|---|
 | 138 |             this->ball_->setPosition(0, 0, 0); | 
|---|
 | 139 |             this->ball_->setFieldDimension(this->center_->getFieldDimension()); | 
|---|
 | 140 |             this->ball_->setSpeed(0); | 
|---|
| [5929] | 141 |             this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor()); | 
|---|
| [2825] | 142 |             this->ball_->setBatLength(this->center_->getBatLength()); | 
|---|
 | 143 |  | 
|---|
| [8108] | 144 |             // If one of the bats is missing, create it. Apply the template for the bats as specified in the centerpoint. | 
|---|
 | 145 |             for (size_t i = 0; i < 2; ++i) | 
|---|
| [2825] | 146 |             { | 
|---|
| [8108] | 147 |                 if (this->bat_[i] == NULL) | 
|---|
 | 148 |                 { | 
|---|
 | 149 |                     this->bat_[i] = new PongBat(this->center_); | 
|---|
 | 150 |                     this->bat_[i]->addTemplate(this->center_->getBattemplate()); | 
|---|
 | 151 |                 } | 
|---|
| [2825] | 152 |             } | 
|---|
 | 153 |  | 
|---|
| [8108] | 154 |             // Attach the bats to the centerpoint and set the parameters as specified in the centerpoint, the bats are attached to. | 
|---|
| [2825] | 155 |             this->center_->attach(this->bat_[0]); | 
|---|
 | 156 |             this->center_->attach(this->bat_[1]); | 
|---|
 | 157 |             this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0); | 
|---|
 | 158 |             this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0); | 
|---|
 | 159 |             this->bat_[0]->yaw(Degree(-90)); | 
|---|
 | 160 |             this->bat_[1]->yaw(Degree(90)); | 
|---|
 | 161 |             this->bat_[0]->setSpeed(this->center_->getBatSpeed()); | 
|---|
 | 162 |             this->bat_[1]->setSpeed(this->center_->getBatSpeed()); | 
|---|
 | 163 |             this->bat_[0]->setFieldHeight(this->center_->getFieldDimension().y); | 
|---|
 | 164 |             this->bat_[1]->setFieldHeight(this->center_->getFieldDimension().y); | 
|---|
 | 165 |             this->bat_[0]->setLength(this->center_->getBatLength()); | 
|---|
 | 166 |             this->bat_[1]->setLength(this->center_->getBatLength()); | 
|---|
 | 167 |  | 
|---|
| [8108] | 168 |             // Set the bats for the ball. | 
|---|
| [2825] | 169 |             this->ball_->setBats(this->bat_); | 
|---|
 | 170 |         } | 
|---|
| [8108] | 171 |         else // If no centerpoint was specified, an error is thrown and the level is exited. | 
|---|
| [2825] | 172 |         { | 
|---|
| [8858] | 173 |             orxout(internal_error) << "Pong: No Centerpoint specified." << endl; | 
|---|
| [8108] | 174 |             GSLevel::startMainMenu(); | 
|---|
 | 175 |             return; | 
|---|
| [2825] | 176 |         } | 
|---|
 | 177 |  | 
|---|
| [8108] | 178 |         // Start the timer. After it has expired the ball is started. | 
|---|
| [2825] | 179 |         this->starttimer_.startTimer(); | 
|---|
 | 180 |  | 
|---|
| [8108] | 181 |         // Set variable to temporarily force the player to spawn. | 
|---|
| [2885] | 182 |         bool temp = this->bForceSpawn_; | 
|---|
 | 183 |         this->bForceSpawn_ = true; | 
|---|
 | 184 |  | 
|---|
| [8108] | 185 |         // Call start for the parent class. | 
|---|
| [2825] | 186 |         Deathmatch::start(); | 
|---|
| [2885] | 187 |  | 
|---|
| [8108] | 188 |         // Reset the variable. | 
|---|
| [2885] | 189 |         this->bForceSpawn_ = temp; | 
|---|
| [2825] | 190 |     } | 
|---|
 | 191 |  | 
|---|
| [8108] | 192 |     /** | 
|---|
 | 193 |     @brief | 
|---|
 | 194 |         Ends the Pong minigame. | 
|---|
 | 195 |     */ | 
|---|
| [2825] | 196 |     void Pong::end() | 
|---|
 | 197 |     { | 
|---|
| [7911] | 198 |         this->cleanup(); | 
|---|
| [2825] | 199 |  | 
|---|
| [8108] | 200 |         // Call end for the parent class. | 
|---|
| [2825] | 201 |         Deathmatch::end(); | 
|---|
 | 202 |     } | 
|---|
 | 203 |  | 
|---|
| [8108] | 204 |     /** | 
|---|
 | 205 |     @brief | 
|---|
 | 206 |         Spawns players, and fills the rest up with bots. | 
|---|
 | 207 |     */ | 
|---|
| [7865] | 208 |     void Pong::spawnPlayersIfRequested() | 
|---|
 | 209 |     { | 
|---|
 | 210 |         // first spawn human players to assign always the left bat to the player in singleplayer | 
|---|
 | 211 |         for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) | 
|---|
 | 212 |             if (it->first->isHumanPlayer() && (it->first->isReadyToSpawn() || this->bForceSpawn_)) | 
|---|
 | 213 |                 this->spawnPlayer(it->first); | 
|---|
 | 214 |         // now spawn bots | 
|---|
 | 215 |         for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) | 
|---|
 | 216 |             if (!it->first->isHumanPlayer() && (it->first->isReadyToSpawn() || this->bForceSpawn_)) | 
|---|
 | 217 |                 this->spawnPlayer(it->first); | 
|---|
 | 218 |     } | 
|---|
 | 219 |  | 
|---|
| [8108] | 220 |     /** | 
|---|
 | 221 |     @brief | 
|---|
 | 222 |         Spawns the input player. | 
|---|
 | 223 |     @param player | 
|---|
 | 224 |         The player to be spawned. | 
|---|
 | 225 |     */ | 
|---|
| [2825] | 226 |     void Pong::spawnPlayer(PlayerInfo* player) | 
|---|
 | 227 |     { | 
|---|
| [8108] | 228 |         assert(player); | 
|---|
 | 229 |  | 
|---|
 | 230 |         // If the first (left) bat has no player. | 
|---|
 | 231 |         if (this->bat_[0]->getPlayer() == NULL) | 
|---|
| [2825] | 232 |         { | 
|---|
 | 233 |             player->startControl(this->bat_[0]); | 
|---|
 | 234 |             this->players_[player].state_ = PlayerState::Alive; | 
|---|
 | 235 |         } | 
|---|
| [8108] | 236 |         // If the second (right) bat has no player. | 
|---|
 | 237 |         else if (this->bat_[1]->getPlayer() == NULL) | 
|---|
| [2825] | 238 |         { | 
|---|
 | 239 |             player->startControl(this->bat_[1]); | 
|---|
 | 240 |             this->players_[player].state_ = PlayerState::Alive; | 
|---|
 | 241 |         } | 
|---|
| [8108] | 242 |         // If both bats are taken. | 
|---|
| [2839] | 243 |         else | 
|---|
 | 244 |             return; | 
|---|
 | 245 |  | 
|---|
| [8108] | 246 |         // If the player is an AI, it receives a pointer to the ball. | 
|---|
 | 247 |         if (player->getController() != NULL && player->getController()->isA(Class(PongAI))) | 
|---|
| [2839] | 248 |         { | 
|---|
| [3325] | 249 |             PongAI* ai = orxonox_cast<PongAI*>(player->getController()); | 
|---|
| [2839] | 250 |             ai->setPongBall(this->ball_); | 
|---|
 | 251 |         } | 
|---|
| [2825] | 252 |     } | 
|---|
 | 253 |  | 
|---|
| [8108] | 254 |     /** | 
|---|
 | 255 |     @brief | 
|---|
 | 256 |         Is called when the player scored. | 
|---|
 | 257 |     */ | 
|---|
| [9348] | 258 |     void Pong::playerScored(PlayerInfo* player, int score) | 
|---|
| [2825] | 259 |     { | 
|---|
| [9348] | 260 |         Deathmatch::playerScored(player, score); | 
|---|
| [2825] | 261 |  | 
|---|
| [8108] | 262 |         if (this->center_ != NULL) // If there is a centerpoint. | 
|---|
| [2872] | 263 |         { | 
|---|
| [8108] | 264 |             // Fire an event for the player that has scored, to be able to react to it in the level, e.g. by displaying fireworks. | 
|---|
| [5929] | 265 |             if (player == this->getRightPlayer()) | 
|---|
 | 266 |                 this->center_->fireEvent(FireEventName(PongCenterpoint, right)); | 
|---|
 | 267 |             else if (player == this->getLeftPlayer()) | 
|---|
 | 268 |                 this->center_->fireEvent(FireEventName(PongCenterpoint, left)); | 
|---|
| [6417] | 269 |  | 
|---|
| [8108] | 270 |             // Also announce, that the player has scored. | 
|---|
 | 271 |             if (player != NULL) | 
|---|
| [5929] | 272 |                 this->gtinfo_->sendAnnounceMessage(player->getName() + " scored"); | 
|---|
| [2872] | 273 |         } | 
|---|
 | 274 |  | 
|---|
| [8108] | 275 |         // If there is a ball present, reset its position, velocity and acceleration. | 
|---|
 | 276 |         if (this->ball_ != NULL) | 
|---|
| [2825] | 277 |         { | 
|---|
 | 278 |             this->ball_->setPosition(Vector3::ZERO); | 
|---|
 | 279 |             this->ball_->setVelocity(Vector3::ZERO); | 
|---|
| [5929] | 280 |             this->ball_->setAcceleration(Vector3::ZERO); | 
|---|
| [2825] | 281 |             this->ball_->setSpeed(0); | 
|---|
 | 282 |         } | 
|---|
 | 283 |  | 
|---|
| [8108] | 284 |         // If there are bats reset them to the middle position. | 
|---|
 | 285 |         if (this->bat_[0] != NULL && this->bat_[1] != NULL) | 
|---|
| [2825] | 286 |         { | 
|---|
 | 287 |             this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0); | 
|---|
 | 288 |             this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0); | 
|---|
 | 289 |         } | 
|---|
 | 290 |  | 
|---|
| [9258] | 291 |         // If a player gets enough points, he won the game -> end of game | 
|---|
 | 292 |         PlayerInfo* winningPlayer = NULL; | 
|---|
 | 293 |         if (this->getLeftPlayer() && this->getScore(this->getLeftPlayer()) >= scoreLimit_) | 
|---|
 | 294 |             winningPlayer = this->getLeftPlayer(); | 
|---|
 | 295 |         else if (this->getRightPlayer() && this->getScore(this->getRightPlayer()) >= scoreLimit_) | 
|---|
 | 296 |             winningPlayer = this->getRightPlayer(); | 
|---|
 | 297 |  | 
|---|
 | 298 |         if (winningPlayer) | 
|---|
| [9016] | 299 |         { | 
|---|
| [9258] | 300 |              ChatManager::message(winningPlayer->getName() + " has won!"); | 
|---|
| [9016] | 301 |              this->end(); | 
|---|
 | 302 |         } | 
|---|
| [9258] | 303 |  | 
|---|
| [8108] | 304 |         // Restart the timer to start the ball. | 
|---|
| [2825] | 305 |         this->starttimer_.startTimer(); | 
|---|
 | 306 |     } | 
|---|
 | 307 |  | 
|---|
| [8108] | 308 |     /** | 
|---|
 | 309 |     @brief | 
|---|
 | 310 |         Starts the ball with some default speed. | 
|---|
 | 311 |     */ | 
|---|
| [2825] | 312 |     void Pong::startBall() | 
|---|
 | 313 |     { | 
|---|
| [8108] | 314 |         if (this->ball_ != NULL && this->center_ != NULL) | 
|---|
| [2825] | 315 |             this->ball_->setSpeed(this->center_->getBallSpeed()); | 
|---|
 | 316 |     } | 
|---|
| [2890] | 317 |  | 
|---|
| [8108] | 318 |     /** | 
|---|
 | 319 |     @brief | 
|---|
 | 320 |         Get the left player. | 
|---|
 | 321 |     @return | 
|---|
 | 322 |         Returns a pointer to the player playing on the left. If there is no left player, NULL is returned. | 
|---|
 | 323 |     */ | 
|---|
| [2890] | 324 |     PlayerInfo* Pong::getLeftPlayer() const | 
|---|
 | 325 |     { | 
|---|
| [8108] | 326 |         if (this->bat_ != NULL && this->bat_[0] != NULL) | 
|---|
| [2890] | 327 |             return this->bat_[0]->getPlayer(); | 
|---|
 | 328 |         else | 
|---|
 | 329 |             return 0; | 
|---|
 | 330 |     } | 
|---|
 | 331 |  | 
|---|
| [8108] | 332 |     /** | 
|---|
 | 333 |     @brief | 
|---|
 | 334 |         Get the right player. | 
|---|
 | 335 |     @return | 
|---|
 | 336 |         Returns a pointer to the player playing on the right. If there is no right player, NULL is returned. | 
|---|
 | 337 |     */ | 
|---|
| [2890] | 338 |     PlayerInfo* Pong::getRightPlayer() const | 
|---|
 | 339 |     { | 
|---|
| [8108] | 340 |         if (this->bat_ != NULL && this->bat_[1] != NULL) | 
|---|
| [2890] | 341 |             return this->bat_[1]->getPlayer(); | 
|---|
 | 342 |         else | 
|---|
 | 343 |             return 0; | 
|---|
 | 344 |     } | 
|---|
| [2825] | 345 | } | 
|---|