Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pong/PongScore.cc @ 9259

Last change on this file since 9259 was 9259, checked in by landauf, 12 years ago

refactored HUDEnemyHealthBar: now it uses ControllableEntity::getTarget() instead of searching the target itself

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