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