| [3078] | 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 PongScore.cc | 
|---|
|  | 31 | @brief Implementation of the PongScore class. | 
|---|
|  | 32 | */ | 
|---|
|  | 33 |  | 
|---|
| [3078] | 34 | #include "PongScore.h" | 
|---|
|  | 35 |  | 
|---|
|  | 36 | #include "core/CoreIncludes.h" | 
|---|
|  | 37 | #include "core/XMLPort.h" | 
|---|
| [8108] | 38 | #include "util/Convert.h" | 
|---|
|  | 39 |  | 
|---|
| [5735] | 40 | #include "infos/PlayerInfo.h" | 
|---|
| [3078] | 41 |  | 
|---|
| [8108] | 42 | #include "Pong.h" | 
|---|
| [9939] | 43 | #include "sound/WorldSound.h" ///////////////////////////// | 
|---|
| [8108] | 44 |  | 
|---|
| [3078] | 45 | namespace orxonox | 
|---|
|  | 46 | { | 
|---|
| [9667] | 47 | RegisterClass(PongScore); | 
|---|
| [3078] | 48 |  | 
|---|
| [8108] | 49 | /** | 
|---|
|  | 50 | @brief | 
|---|
|  | 51 | Constructor. Registers and initializes the object. | 
|---|
|  | 52 | */ | 
|---|
| [9667] | 53 | PongScore::PongScore(Context* context) : OverlayText(context) | 
|---|
| [3078] | 54 | { | 
|---|
|  | 55 | RegisterObject(PongScore); | 
|---|
|  | 56 |  | 
|---|
|  | 57 | this->owner_ = 0; | 
|---|
|  | 58 |  | 
|---|
|  | 59 | this->bShowName_ = false; | 
|---|
|  | 60 | this->bShowScore_ = false; | 
|---|
|  | 61 | this->bShowLeftPlayer_ = false; | 
|---|
|  | 62 | this->bShowRightPlayer_ = false; | 
|---|
|  | 63 | } | 
|---|
|  | 64 |  | 
|---|
| [8108] | 65 | /** | 
|---|
|  | 66 | @brief | 
|---|
|  | 67 | Destructor. | 
|---|
|  | 68 | */ | 
|---|
| [3078] | 69 | PongScore::~PongScore() | 
|---|
|  | 70 | { | 
|---|
|  | 71 | } | 
|---|
|  | 72 |  | 
|---|
| [8108] | 73 | /** | 
|---|
|  | 74 | @brief | 
|---|
|  | 75 | Method to create a PongScore through XML. | 
|---|
|  | 76 | */ | 
|---|
| [3078] | 77 | void PongScore::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
|  | 78 | { | 
|---|
|  | 79 | SUPER(PongScore, XMLPort, xmlelement, mode); | 
|---|
|  | 80 |  | 
|---|
|  | 81 | XMLPortParam(PongScore, "showname",        setShowName,        getShowName,        xmlelement, mode).defaultValues(false); | 
|---|
|  | 82 | XMLPortParam(PongScore, "showscore",       setShowScore,       getShowScore,       xmlelement, mode).defaultValues(false); | 
|---|
|  | 83 | XMLPortParam(PongScore, "showleftplayer",  setShowLeftPlayer,  getShowLeftPlayer,  xmlelement, mode).defaultValues(false); | 
|---|
|  | 84 | XMLPortParam(PongScore, "showrightplayer", setShowRightPlayer, getShowRightPlayer, xmlelement, mode).defaultValues(false); | 
|---|
|  | 85 | } | 
|---|
|  | 86 |  | 
|---|
| [8108] | 87 | /** | 
|---|
|  | 88 | @brief | 
|---|
|  | 89 | Is called each tick. | 
|---|
|  | 90 | Creates and sets the caption to be displayed by the PongScore. | 
|---|
|  | 91 | @param dt | 
|---|
|  | 92 | The time that has elapsed since the last tick. | 
|---|
|  | 93 | */ | 
|---|
| [3078] | 94 | void PongScore::tick(float dt) | 
|---|
|  | 95 | { | 
|---|
|  | 96 | SUPER(PongScore, tick, dt); | 
|---|
|  | 97 |  | 
|---|
| [8108] | 98 | // If the owner is set. The owner being a Pong game. | 
|---|
|  | 99 | if (this->owner_ != NULL) | 
|---|
| [3078] | 100 | { | 
|---|
| [9258] | 101 | if (!this->owner_->hasEnded()) | 
|---|
| [3078] | 102 | { | 
|---|
| [9258] | 103 | // Get the two players. | 
|---|
| [9016] | 104 | player1_ = this->owner_->getLeftPlayer(); | 
|---|
|  | 105 | player2_ = this->owner_->getRightPlayer(); | 
|---|
| [3078] | 106 | } | 
|---|
|  | 107 |  | 
|---|
| [9258] | 108 | std::string name1; | 
|---|
|  | 109 | std::string name2; | 
|---|
| [3078] | 110 |  | 
|---|
| [9258] | 111 | std::string score1("0"); | 
|---|
|  | 112 | std::string score2("0"); | 
|---|
| [9016] | 113 |  | 
|---|
| [9258] | 114 | // Save the name and score of each player as a string. | 
|---|
|  | 115 | if (player1_ != NULL) | 
|---|
|  | 116 | { | 
|---|
|  | 117 | name1 = player1_->getName(); | 
|---|
|  | 118 | score1 = multi_cast<std::string>(this->owner_->getScore(player1_)); | 
|---|
|  | 119 | } | 
|---|
|  | 120 | if (player2_ != NULL) | 
|---|
|  | 121 | { | 
|---|
|  | 122 | name2 = player2_->getName(); | 
|---|
|  | 123 | score2 = multi_cast<std::string>(this->owner_->getScore(player2_)); | 
|---|
|  | 124 | } | 
|---|
| [9016] | 125 |  | 
|---|
| [9258] | 126 | // Assemble the strings, depending on what should all be displayed. | 
|---|
|  | 127 | std::string output1; | 
|---|
|  | 128 | if (this->bShowLeftPlayer_) | 
|---|
|  | 129 | { | 
|---|
|  | 130 | if (this->bShowName_ && this->bShowScore_ && player1_ != NULL) | 
|---|
| [9259] | 131 | output1 = name1 + " - " + score1; | 
|---|
| [9258] | 132 | else if (this->bShowScore_) | 
|---|
| [9259] | 133 | output1 = score1; | 
|---|
| [9258] | 134 | else if (this->bShowName_) | 
|---|
| [9259] | 135 | output1 = name1; | 
|---|
| [9258] | 136 | } | 
|---|
| [9016] | 137 |  | 
|---|
| [9258] | 138 | std::string output2; | 
|---|
|  | 139 | if (this->bShowRightPlayer_) | 
|---|
|  | 140 | { | 
|---|
| [9016] | 141 | if (this->bShowName_ && this->bShowScore_ && player2_ != NULL) | 
|---|
| [3078] | 142 | output2 = score2 + " - " + name2; | 
|---|
|  | 143 | else if (this->bShowScore_) | 
|---|
|  | 144 | output2 = score2; | 
|---|
|  | 145 | else if (this->bShowName_) | 
|---|
|  | 146 | output2 = name2; | 
|---|
|  | 147 | } | 
|---|
|  | 148 |  | 
|---|
| [9258] | 149 | std::string output("PONG"); | 
|---|
|  | 150 | if (this->bShowName_ || this->bShowScore_) | 
|---|
|  | 151 | { | 
|---|
|  | 152 | if (this->bShowLeftPlayer_ && this->bShowRightPlayer_) | 
|---|
|  | 153 | output = output1 + ':' + output2; | 
|---|
|  | 154 | else if (this->bShowLeftPlayer_ || this->bShowRightPlayer_) | 
|---|
|  | 155 | output = output1 + output2; | 
|---|
| [3078] | 156 | } | 
|---|
| [9258] | 157 |  | 
|---|
|  | 158 | this->setCaption(output); | 
|---|
| [3078] | 159 | } | 
|---|
|  | 160 | } | 
|---|
|  | 161 |  | 
|---|
| [8108] | 162 | /** | 
|---|
|  | 163 | @brief | 
|---|
|  | 164 | Is called when the owner changes. | 
|---|
|  | 165 | Sets the owner to NULL, if it is not a pointer to a Pong game. | 
|---|
|  | 166 | */ | 
|---|
| [3078] | 167 | void PongScore::changedOwner() | 
|---|
|  | 168 | { | 
|---|
|  | 169 | SUPER(PongScore, changedOwner); | 
|---|
|  | 170 |  | 
|---|
| [8108] | 171 | if (this->getOwner() != NULL && this->getOwner()->getGametype()) | 
|---|
| [5929] | 172 | this->owner_ = orxonox_cast<Pong*>(this->getOwner()->getGametype().get()); | 
|---|
| [3078] | 173 | else | 
|---|
|  | 174 | this->owner_ = 0; | 
|---|
|  | 175 | } | 
|---|
|  | 176 | } | 
|---|