/* * 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 OrxoBloxScore.cc @brief Implementation of the OrxoBloxScore class. */ #include "OrxoBloxScore.h" #include "core/CoreIncludes.h" #include "core/XMLPort.h" #include "util/Convert.h" #include "infos/PlayerInfo.h" #include "OrxoBlox.h" #include "sound/WorldSound.h" ///////////////////////////// namespace orxonox { RegisterClass(OrxoBloxScore); /** @brief Constructor. Registers and initializes the object. */ OrxoBloxScore::OrxoBloxScore(Context* context) : OverlayText(context) { RegisterObject(OrxoBloxScore); this->owner_ = nullptr; this->bShowName_ = false; this->bShowScore_ = false; this->bShowLeftPlayer_ = false; this->bShowRightPlayer_ = false; } /** @brief Destructor. */ OrxoBloxScore::~OrxoBloxScore() { } /** @brief Method to create a OrxoBloxScore through XML. */ void OrxoBloxScore::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(OrxoBloxScore, XMLPort, xmlelement, mode); XMLPortParam(OrxoBloxScore, "showname", setShowName, getShowName, xmlelement, mode).defaultValues(false); XMLPortParam(OrxoBloxScore, "showscore", setShowScore, getShowScore, xmlelement, mode).defaultValues(false); XMLPortParam(OrxoBloxScore, "showleftplayer", setShowLeftPlayer, getShowLeftPlayer, xmlelement, mode).defaultValues(false); XMLPortParam(OrxoBloxScore, "showrightplayer", setShowRightPlayer, getShowRightPlayer, xmlelement, mode).defaultValues(false); } /** @brief Is called each tick. Creates and sets the caption to be displayed by the OrxoBloxScore. @param dt The time that has elapsed since the last tick. */ void OrxoBloxScore::tick(float dt) { SUPER(OrxoBloxScore, tick, dt); // If the owner is set. The owner being a OrxoBlox game. if (this->owner_ != nullptr) { if (!this->owner_->hasEnded()) { // Get the two players. player1_ = this->owner_->getLeftPlayer(); player2_ = this->owner_->getRightPlayer(); } std::string name1; std::string name2; std::string score1("0"); std::string score2("0"); // Save the name and score of each player as a string. if (player1_ != nullptr) { name1 = player1_->getName(); score1 = multi_cast(this->owner_->getScore(player1_)); } if (player2_ != nullptr) { name2 = player2_->getName(); score2 = multi_cast(this->owner_->getScore(player2_)); } // Assemble the strings, depending on what should all be displayed. std::string output1; if (this->bShowLeftPlayer_) { if (this->bShowName_ && this->bShowScore_ && player1_ != nullptr) output1 = name1 + " - " + score1; else if (this->bShowScore_) output1 = score1; else if (this->bShowName_) output1 = name1; } std::string output2; if (this->bShowRightPlayer_) { if (this->bShowName_ && this->bShowScore_ && player2_ != nullptr) output2 = score2 + " - " + name2; else if (this->bShowScore_) output2 = score2; else if (this->bShowName_) output2 = name2; } std::string output("OrxoBlox"); if (this->bShowName_ || this->bShowScore_) { if (this->bShowLeftPlayer_ && this->bShowRightPlayer_) output = output1 + ':' + output2; else if (this->bShowLeftPlayer_ || this->bShowRightPlayer_) output = output1 + output2; } this->setCaption(output); } } /** @brief Is called when the owner changes. Sets the owner to nullptr, if it is not a pointer to a OrxoBlox game. */ void OrxoBloxScore::changedOwner() { SUPER(OrxoBloxScore, changedOwner); if (this->getOwner() != nullptr && this->getOwner()->getGametype()) this->owner_ = orxonox_cast(this->getOwner()->getGametype()); else this->owner_ = nullptr; } }