Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed warning

  • 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    }
63
64    /**
65    @brief
66        Destructor.
67    */
68    PongScore::~PongScore()
69    {
70    }
71
72    /**
73    @brief
74        Method to create a PongScore through XML.
75    */
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
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    */
93    void PongScore::tick(float dt)
94    {
95        SUPER(PongScore, tick, dt);
96
97        // If the owner is set. The owner being a Pong game.
98        if (this->owner_ != NULL)
99        {
100            if (!this->owner_->hasEnded())
101            {
102                // Get the two players.
103                player1_ = this->owner_->getLeftPlayer();
104                player2_ = this->owner_->getRightPlayer();
105            }
106
107            std::string name1;
108            std::string name2;
109
110            std::string score1("0");
111            std::string score2("0");
112
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            }
124
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)
130                    output1 = name1 + " - " + score1;
131                else if (this->bShowScore_)
132                    output1 = score1;
133                else if (this->bShowName_)
134                    output1 = name1;
135            }
136
137            std::string output2;
138            if (this->bShowRightPlayer_)
139            {
140                if (this->bShowName_ && this->bShowScore_ && player2_ != NULL)
141                    output2 = score2 + " - " + name2;
142                else if (this->bShowScore_)
143                    output2 = score2;
144                else if (this->bShowName_)
145                    output2 = name2;
146            }
147
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;
155            }
156
157            this->setCaption(output);
158        }
159    }
160
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    */
166    void PongScore::changedOwner()
167    {
168        SUPER(PongScore, changedOwner);
169
170        if (this->getOwner() != NULL && this->getOwner()->getGametype())
171            this->owner_ = orxonox_cast<Pong*>(this->getOwner()->getGametype().get());
172        else
173            this->owner_ = 0;
174    }
175}
Note: See TracBrowser for help on using the repository browser.