Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9029 was 9016, checked in by jo, 13 years ago

Merging presentation2011 branch to trunk. Please check for possible bugs.

  • Property svn:eol-style set to native
File size: 5.6 KB
RevLine 
[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]44namespace 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;
[9016]62        this->player1_ = NULL;
63        this->player2_ = NULL;
[3078]64    }
65
[8108]66    /**
67    @brief
68        Destructor.
69    */
[3078]70    PongScore::~PongScore()
71    {
72    }
73
[8108]74    /**
75    @brief
76        Method to create a PongScore through XML.
77    */
[3078]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
[8108]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    */
[3078]95    void PongScore::tick(float dt)
96    {
97        SUPER(PongScore, tick, dt);
98
[8108]99        // If the owner is set. The owner being a Pong game.
100        if (this->owner_ != NULL)
[3078]101        {
[9016]102            if(!this->owner_->hasEnded())
[3078]103            {
[9016]104                //get the two players
105                player1_ = this->owner_->getLeftPlayer();
106                player2_ = this->owner_->getRightPlayer();
[3078]107            }
108
[9016]109            if(this->owner_->hasStarted())
[3078]110            {
[9016]111                // Get the two players.
[3078]112
[9016]113                std::string name1;
114                std::string name2;
115
116                std::string score1("0");
117                std::string score2("0");
118
119                // Save the name and score of each player as a string.
120                if (player1_ != NULL)
121                {
122                    name1 = player1_->getName();
123                    score1 = multi_cast<std::string>(this->owner_->getScore(player1_));
124                }
125                if (player2_ != NULL)
126                {
127                    name2 = player2_->getName();
128                    score2 = multi_cast<std::string>(this->owner_->getScore(player2_));
129                }
130
131                // Assemble the strings, depending on what should all be displayed.
132                std::string output1;
133                if (this->bShowLeftPlayer_)
134                {
135                    if (this->bShowName_ && this->bShowScore_ && player1_ != NULL)
136                         output1 = name1 + " - " + score1;
137                    else if (this->bShowScore_)
138                         output1 = score1;
139                    else if (this->bShowName_)
140                         output1 = name1;
141                }
142
143                std::string output2;
144                if (this->bShowRightPlayer_)
145                {
146                if (this->bShowName_ && this->bShowScore_ && player2_ != NULL)
[3078]147                    output2 = score2 + " - " + name2;
148                else if (this->bShowScore_)
149                    output2 = score2;
150                else if (this->bShowName_)
151                    output2 = name2;
152            }
153
[9016]154                std::string output("PONG");
155                if (this->bShowName_ || this->bShowScore_)
156                {
157                    if (this->bShowLeftPlayer_ && this->bShowRightPlayer_)
158                        output = output1 + ':' + output2;
159                    else if (this->bShowLeftPlayer_ || this->bShowRightPlayer_)
160                        output = output1 + output2;
161                }
162                this->setCaption(output);
[3078]163            }
164        }
165    }
166
[8108]167    /**
168    @brief
169        Is called when the owner changes.
170        Sets the owner to NULL, if it is not a pointer to a Pong game.
171    */
[3078]172    void PongScore::changedOwner()
173    {
174        SUPER(PongScore, changedOwner);
175
[8108]176        if (this->getOwner() != NULL && this->getOwner()->getGametype())
[5929]177            this->owner_ = orxonox_cast<Pong*>(this->getOwner()->getGametype().get());
[3078]178        else
179            this->owner_ = 0;
180    }
181}
Note: See TracBrowser for help on using the repository browser.