/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * 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. * * Author: * Fabian 'x3n' Landau * Co-authors: * ... * */ /** @file OrxoBlox.cc @brief Implementation of the OrxoBlox class. */ #include "OrxoBlox.h" #include "core/CoreIncludes.h" #include "core/EventIncludes.h" #include "core/command/Executor.h" #include "core/config/ConfigValueIncludes.h" #include "gamestates/GSLevel.h" #include "chat/ChatManager.h" #include "OrxoBloxCenterpoint.h" #include "OrxoBloxBall.h" #include "OrxoBloxBat.h" #include "OrxoBloxBot.h" #include "OrxoBloxStones.h" namespace orxonox { // Events to allow to react to scoring of a player, in the level-file. CreateEventName(OrxoBloxCenterpoint, right); CreateEventName(OrxoBloxCenterpoint, left); RegisterUnloadableClass(OrxoBlox); /** @brief Constructor. Registers and initializes the object. */ OrxoBlox::OrxoBlox(Context* context) : Deathmatch(context) { RegisterObject(OrxoBlox); this->center_ = nullptr; this->ball_ = nullptr; this->bat_[0] = nullptr; this->futureWall_ = nullptr; //this->setHUDTemplate("pongHUD"); //Error when specified // Pre-set the timer, but don't start it yet. this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&OrxoBlox::startBall, this))); this->starttimer_.stopTimer(); // Set the type of Bots for this particular Gametype. this->botclass_ = Class(OrxoBloxBat); this->scoreLimit_ = 10; this->setConfigValues(); } /** @brief Destructor. Cleans up, if initialized. */ OrxoBlox::~OrxoBlox() { if (this->isInitialized()) this->cleanup(); } void OrxoBlox::setConfigValues() { SetConfigValue(scoreLimit_, 10).description("The player first reaching those points wins."); } /** @brief Cleans up the Gametype by destroying the ball and the bats. */ void OrxoBlox::cleanup() { if (this->ball_ != nullptr) // Destroy the ball, if present. { this->ball_->destroy(); this->ball_ = nullptr; } // Destroy both bats, if present. for (size_t i = 0; i < 2; ++i) { if (this->bat_[0] != nullptr) { this->bat_[0]->destroy(); this->bat_[0] = nullptr; } } } /** @brief Starts the OrxoBlox minigame. */ void OrxoBlox::start() { if (this->center_ != nullptr) // There needs to be a OrxoBloxCenterpoint, i.e. the area the game takes place. { if (this->ball_ == nullptr) // If there is no ball, create a new ball. { this->ball_ = new OrxoBloxBall(this->center_->getContext()); // Apply the template for the ball specified by the centerpoint. this->ball_->addTemplate(this->center_->getBalltemplate()); } // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to. this->center_->attach(this->ball_); this->ball_->setPosition(0, 0, 0); this->ball_->setFieldDimension(this->center_->getFieldDimension()); this->ball_->setSpeed(0); this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor()); this->ball_->setBatLength(this->center_->getBatLength()); // If one of the bats is missing, create it. Apply the template for the bats as specified in the centerpoint. for (WeakPtr& bat : this->bat_) { if (bat == nullptr) { bat = new OrxoBloxBat(this->center_->getContext()); bat->addTemplate(this->center_->getBattemplate()); } } // Attach the bats to the centerpoint and set the parameters as specified in the centerpoint, the bats are attached to. this->center_->attach(this->bat_[0]); this->center_->attach(this->bat_[1]); this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0); this->bat_[0]->yaw(Degree(-90)); this->bat_[0]->setSpeed(this->center_->getBatSpeed()); this->bat_[0]->setFieldHeight(this->center_->getFieldDimension().y); this->bat_[0]->setLength(this->center_->getBatLength()); // Set the bats for the ball. this->ball_->setBats(this->bat_); } else // If no centerpoint was specified, an error is thrown and the level is exited. { orxout(internal_error) << "OrxoBlox: No Centerpoint specified." << endl; GSLevel::startMainMenu(); return; } // Start the timer. After it has expired the ball is started. this->starttimer_.startTimer(); // Set variable to temporarily force the player to spawn. bool temp = this->bForceSpawn_; this->bForceSpawn_ = true; // Call start for the parent class. Deathmatch::start(); // Reset the variable. this->bForceSpawn_ = temp; } /** @brief Ends the OrxoBlox minigame. */ void OrxoBlox::end() { ChatManager::message("You suck!!"); this->cleanup(); // Call end for the parent class. Deathmatch::end(); } /** @brief Spawns players, and fills the rest up with bots. */ void OrxoBlox::spawnPlayersIfRequested() { // first spawn human players to assign always the left bat to the player in singleplayer for (const auto& mapEntry : this->players_) if (mapEntry.first->isHumanPlayer() && (mapEntry.first->isReadyToSpawn() || this->bForceSpawn_)) this->spawnPlayer(mapEntry.first); // now spawn bots for (const auto& mapEntry : this->players_) if (!mapEntry.first->isHumanPlayer() && (mapEntry.first->isReadyToSpawn() || this->bForceSpawn_)) this->spawnPlayer(mapEntry.first); } /** @brief Spawns the input player. @param player The player to be spawned. */ void OrxoBlox::spawnPlayer(PlayerInfo* player) { assert(player); // If the first (left) bat has no player. if (this->bat_[0]->getPlayer() == nullptr) { player->startControl(this->bat_[0]); this->players_[player].state_ = PlayerState::Alive; } else return; } /** @brief Is called when the player scored. */ void OrxoBlox::playerScored(PlayerInfo* player, int score) { Deathmatch::playerScored(player, score); if (this->center_ != nullptr) // If there is a centerpoint. { // Fire an event for the player that has scored, to be able to react to it in the level, e.g. by displaying fireworks. if (player == this->getLeftPlayer()) this->center_->fireEvent(FireEventName(OrxoBloxCenterpoint, left)); // Also announce, that the player has scored. if (player != nullptr) this->gtinfo_->sendAnnounceMessage(player->getName() + " scored"); } // If there is a ball present, reset its position, velocity and acceleration. if (this->ball_ != nullptr) { this->ball_->setPosition(Vector3::ZERO); this->ball_->setVelocity(Vector3::ZERO); this->ball_->setAcceleration(Vector3::ZERO); this->ball_->setSpeed(0); } // If there are bats reset them to the middle position. if (this->bat_[0] != nullptr && this->bat_[1] != nullptr) { this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0); } // Restart the timer to start the ball. this->starttimer_.startTimer(); } /*void OrxoBlox::createStonewall(void){ this->futureWall_ = new OrxoBolxWall(this->center_->getContext()); } /*void Tetris::createBrick(void) //TODO: random rotation offset between 0 and 3 (times 90°) { // create new futureBrick_ this->futureBrick_ = new TetrisBrick(this->center_->getContext()); // Apply the stone template to the stone. this->futureBrick_->addTemplate(this->center_->getBrickTemplate()); // Attach the brick to the Centerpoint and set the position of the brick to be at the left side. this->center_->attach(this->futureBrick_); float xPos = (this->center_->getWidth()*1.6f + ((this->center_->getWidth() % 2)*2-1)/2.0f)*this->center_->getStoneSize(); float yPos = (this->center_->getHeight()-5.1f)*this->center_->getStoneSize(); this->futureBrick_->setPosition(xPos, yPos, 0.0f); this->futureBrick_->setGame(this); } }*/ /** @brief Starts the ball with some default speed. */ void OrxoBlox::startBall() { if (this->ball_ != nullptr && this->center_ != nullptr) this->ball_->setSpeed(this->center_->getBallSpeed()); } /** @brief Get the left player. @return Returns a pointer to the player playing on the left. If there is no left player, nullptr is returned. */ PlayerInfo* OrxoBlox::getLeftPlayer() const { if (this->bat_[0] != nullptr) return this->bat_[0]->getPlayer(); else return nullptr; } }