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