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