Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel2/src/modules/pong/PongScore.cc @ 8526

Last change on this file since 8526 was 8108, checked in by dafrick, 14 years ago

Merging changes from tetris branch into trunk, since they are also useful, there.

  • Property svn:eol-style set to native
File size: 5.2 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            // Get the two players.
101            PlayerInfo* player1 = this->owner_->getLeftPlayer();
102            PlayerInfo* player2 = this->owner_->getRightPlayer();
103
104            std::string name1;
105            std::string name2;
106
107            std::string score1("0");
108            std::string score2("0");
109
110            // Save the name and score of each player as a string.
111            if (player1 != NULL)
112            {
113                name1 = player1->getName();
114                score1 = multi_cast<std::string>(this->owner_->getScore(player1));
115            }
116            if (player2 != NULL)
117            {
118                name2 = player2->getName();
119                score2 = multi_cast<std::string>(this->owner_->getScore(player2));
120            }
121
122            // Assemble the strings, depending on what should all be displayed.
123            std::string output1;
124            if (this->bShowLeftPlayer_)
125            {
126                if (this->bShowName_ && this->bShowScore_ && player1 != NULL)
127                    output1 = name1 + " - " + score1;
128                else if (this->bShowScore_)
129                    output1 = score1;
130                else if (this->bShowName_)
131                    output1 = name1;
132            }
133
134            std::string output2;
135            if (this->bShowRightPlayer_)
136            {
137                if (this->bShowName_ && this->bShowScore_ && player2 != NULL)
138                    output2 = score2 + " - " + name2;
139                else if (this->bShowScore_)
140                    output2 = score2;
141                else if (this->bShowName_)
142                    output2 = name2;
143            }
144
145            std::string output("PONG");
146            if (this->bShowName_ || this->bShowScore_)
147            {
148                if (this->bShowLeftPlayer_ && this->bShowRightPlayer_)
149                    output = output1 + ':' + output2;
150                else if (this->bShowLeftPlayer_ || this->bShowRightPlayer_)
151                    output = output1 + output2;
152            }
153
154            this->setCaption(output);
155        }
156    }
157
158    /**
159    @brief
160        Is called when the owner changes.
161        Sets the owner to NULL, if it is not a pointer to a Pong game.
162    */
163    void PongScore::changedOwner()
164    {
165        SUPER(PongScore, changedOwner);
166
167        if (this->getOwner() != NULL && this->getOwner()->getGametype())
168            this->owner_ = orxonox_cast<Pong*>(this->getOwner()->getGametype().get());
169        else
170            this->owner_ = 0;
171    }
172}
Note: See TracBrowser for help on using the repository browser.