Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10226 was 9939, checked in by jo, 11 years ago

presentationHS13 branch merged into trunk

  • 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#include "sound/WorldSound.h" /////////////////////////////
44
45namespace orxonox
46{
47    RegisterClass(PongScore);
48
49    /**
50    @brief
51        Constructor. Registers and initializes the object.
52    */
53    PongScore::PongScore(Context* context) : OverlayText(context)
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
65    /**
66    @brief
67        Destructor.
68    */
69    PongScore::~PongScore()
70    {
71    }
72
73    /**
74    @brief
75        Method to create a PongScore through XML.
76    */
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
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    */
94    void PongScore::tick(float dt)
95    {
96        SUPER(PongScore, tick, dt);
97
98        // If the owner is set. The owner being a Pong game.
99        if (this->owner_ != NULL)
100        {
101            if (!this->owner_->hasEnded())
102            {
103                // Get the two players.
104                player1_ = this->owner_->getLeftPlayer();
105                player2_ = this->owner_->getRightPlayer();
106            }
107
108            std::string name1;
109            std::string name2;
110
111            std::string score1("0");
112            std::string score2("0");
113
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            }
125
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)
131                    output1 = name1 + " - " + score1;
132                else if (this->bShowScore_)
133                    output1 = score1;
134                else if (this->bShowName_)
135                    output1 = name1;
136            }
137
138            std::string output2;
139            if (this->bShowRightPlayer_)
140            {
141                if (this->bShowName_ && this->bShowScore_ && player2_ != NULL)
142                    output2 = score2 + " - " + name2;
143                else if (this->bShowScore_)
144                    output2 = score2;
145                else if (this->bShowName_)
146                    output2 = name2;
147            }
148
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;
156            }
157
158            this->setCaption(output);
159        }
160    }
161
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    */
167    void PongScore::changedOwner()
168    {
169        SUPER(PongScore, changedOwner);
170
171        if (this->getOwner() != NULL && this->getOwner()->getGametype())
172            this->owner_ = orxonox_cast<Pong*>(this->getOwner()->getGametype().get());
173        else
174            this->owner_ = 0;
175    }
176}
Note: See TracBrowser for help on using the repository browser.