Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel3/src/orxonox/infos/GametypeInfo.cc @ 8453

Last change on this file since 8453 was 8453, checked in by dafrick, 13 years ago

Merging tutoriallevel2 branch into tutoriallevel3 branch.

  • Property svn:eol-style set to native
File size: 13.7 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 *      Damian 'Mozork' Frick
26 *
27 */
28
29/**
30    @file GametypeInfo.cc
31    @brief Implementation of the GametypeInfo class
32*/
33
34#include "GametypeInfo.h"
35
36#include "core/CoreIncludes.h"
37#include "core/GameMode.h"
38#include "network/Host.h"
39#include "network/NetworkFunction.h"
40#include "util/Convert.h"
41
42#include "interfaces/GametypeMessageListener.h"
43#include "interfaces/NotificationListener.h"
44
45#include "PlayerInfo.h"
46
47namespace orxonox
48{
49    CreateUnloadableFactory(GametypeInfo);
50
51    registerMemberNetworkFunction(GametypeInfo, dispatchAnnounceMessage);
52    registerMemberNetworkFunction(GametypeInfo, dispatchKillMessage);
53    registerMemberNetworkFunction(GametypeInfo, dispatchDeathMessage);
54    registerMemberNetworkFunction(GametypeInfo, dispatchStaticMessage);
55    registerMemberNetworkFunction(GametypeInfo, dispatchFadingMessage);
56
57    /*static*/ const std::string GametypeInfo::NOTIFICATION_SENDER("gameinfo");
58
59    /**
60    @brief
61        Registers and initializes the object.
62    */
63    GametypeInfo::GametypeInfo(BaseObject* creator) : Info(creator)
64    {
65        RegisterObject(GametypeInfo);
66
67        this->bActive_ = false; // At first the GametypeInfo is inactive.
68       
69        this->bStarted_ = false;
70        this->bEnded_ = false;
71        this->startCountdown_ = 0.0f;
72        this->bStartCountdownRunning_ = false;
73        this->counter_ = 0;
74
75        this->registerVariables();
76    }
77
78    GametypeInfo::~GametypeInfo()
79    {
80    }
81
82    void GametypeInfo::registerVariables()
83    {
84        registerVariable(this->bStarted_,               VariableDirection::ToClient, new NetworkCallback<GametypeInfo>(this, &GametypeInfo::changedStarted));
85        registerVariable(this->bEnded_,                 VariableDirection::ToClient, new NetworkCallback<GametypeInfo>(this, &GametypeInfo::changedEnded));
86        registerVariable(this->bStartCountdownRunning_, VariableDirection::ToClient, new NetworkCallback<GametypeInfo>(this, &GametypeInfo::changedStartCountdownRunning));
87        registerVariable(this->startCountdown_,         VariableDirection::ToClient);
88        registerVariable(this->counter_,                VariableDirection::ToClient, new NetworkCallback<GametypeInfo>(this, &GametypeInfo::changedCountdownCounter));
89        registerVariable(this->hudtemplate_,            VariableDirection::ToClient);
90    }
91
92    /**
93    @brief
94        Is called when the activity has changed.
95    */
96    void GametypeInfo::changedActivity(void)
97    {
98        SUPER(GametypeInfo, changedActivity);
99
100        // If the GametypeInfo has become active the "Press [Fire] to start the match" notification is sent.
101        if(this->isActive())
102            NotificationListener::sendNotification("Press [Fire] to start the match", GametypeInfo::NOTIFICATION_SENDER);
103    }
104
105    /**
106    @brief
107        Is called when the game has changed to started.
108    */
109    void GametypeInfo::changedStarted(void)
110    {
111       
112    }
113
114    /**
115    @brief
116        Is called when the game has changed to ended.
117    */
118    void GametypeInfo::changedEnded(void)
119    {
120        // If the game has ended, a "Game has ended" notification is displayed.
121        if(this->hasEnded())
122            NotificationListener::sendNotification("Game has ended", GametypeInfo::NOTIFICATION_SENDER);
123    }
124
125    /**
126    @brief
127        Is called when the start countdown has been either started or stopped.
128    */
129    void GametypeInfo::changedStartCountdownRunning(void)
130    {
131        // Clear the notifications if the countdown has stopped running.
132        if(!this->hasStarted() && !this->isStartCountdownRunning() && !this->hasEnded())
133            NotificationListener::sendCommand("clear", GametypeInfo::NOTIFICATION_SENDER);
134        // Send first countdown notification if the countdown has started.
135        if(!this->hasStarted() && this->isStartCountdownRunning() && !this->hasEnded())
136            NotificationListener::sendNotification(multi_cast<std::string>(this->counter_), GametypeInfo::NOTIFICATION_SENDER);
137    }
138
139    /**
140    @brief
141        Is called when the start countdown counter has changed.
142    */
143    void GametypeInfo::changedCountdownCounter(void)
144    {
145        // Send countdown notification if the counter has gone down.
146        if(!this->hasStarted() && this->isStartCountdownRunning() && !this->hasEnded())
147            NotificationListener::sendNotification(multi_cast<std::string>(this->counter_), GametypeInfo::NOTIFICATION_SENDER);
148    }
149
150    /**
151    @brief
152        Is called when a player has become ready to spawn.
153    @param player
154        The player that has become ready to spawn.
155    */
156    void GametypeInfo::changedReadyToSpawn(PlayerInfo* player)
157    {
158        // Send "Waiting for other players" over the network to the right player if a player has become ready to spawn.
159        if(!this->hasStarted() && !this->isStartCountdownRunning() && !this->hasEnded())
160            NotificationListener::sendNotification("Waiting for other players", GametypeInfo::NOTIFICATION_SENDER, notificationMessageType::info, notificationSendMode::network, player->getClientID());
161        // Clear the notifications if the player has respawned.
162        if(this->hasStarted() && !this->hasEnded())
163            NotificationListener::sendCommand("clear", GametypeInfo::NOTIFICATION_SENDER, notificationSendMode::network, player->getClientID());
164    }
165
166    /**
167    @brief
168        Inform the GametypeInfo that the game has started.
169    */
170    void GametypeInfo::start(void)
171    {
172        if(this->bStarted_)
173            return;
174       
175        this->bStarted_ = true;
176        this->changedStarted();
177    }
178
179    /**
180    @brief
181        Inform the GametypeInfo that the game has ended.
182    */
183    void GametypeInfo::end(void)
184    {
185        if(this->bEnded_)
186            return;
187
188        this->bEnded_ = true;
189        this->changedEnded();
190    }
191
192    /**
193    @brief
194        Set the start countdown to the input value.
195    @param countdown
196        The countdown to be set.
197    */
198    void GametypeInfo::setStartCountdown(float countdown)
199    {
200        if(this->startCountdown_ == countdown || countdown < 0.0f)
201            return;
202       
203        this->startCountdown_ = countdown;
204        // Set the counter to the ceiling of the current countdown.
205        this->counter_ = ceil(countdown);
206        this->changedCountdownCounter();
207    }
208
209    /**
210    @brief
211        Count down the start countdown by the specified value.
212    @param countDown
213        The amount by which we count down.
214    */
215    void GametypeInfo::countdownStartCountdown(float countDown)
216    {
217        float newCountdown = this->startCountdown_ - countDown;
218        // If we have switched integers or arrived at zero, we also count down the start countdown counter.
219        if(ceil(newCountdown) != ceil(this->startCountdown_) || newCountdown <= 0.0f)
220            this->countDown();
221        this->startCountdown_ = newCountdown;
222    }
223
224    /**
225    @brief
226        Count down the start countdown counter.
227    */
228    void GametypeInfo::countDown()
229    {
230        if(this->counter_ == 0)
231            return;
232       
233        this->counter_--;
234        this->changedCountdownCounter();
235    }
236
237    /**
238    @brief
239        Inform the GametypeInfo about the start of the start countdown.
240    */
241    void GametypeInfo::startStartCountdown(void)
242    {
243        if(this->bStartCountdownRunning_)
244            return;
245       
246        this->bStartCountdownRunning_ = true;
247        this->changedStartCountdownRunning();
248    }
249
250    /**
251    @brief
252        Inform the GametypeInfo about the stop of the start countdown.
253    */
254    void GametypeInfo::stopStartCountdown(void)
255    {
256        if(!this->bStartCountdownRunning_)
257            return;
258       
259        this->bStartCountdownRunning_ = false;
260        this->changedStartCountdownRunning();
261    }
262
263    /**
264    @brief
265        Inform the GametypeInfo about a player that is ready to spawn.
266    @param player
267        The player that is ready to spawn.
268    */
269    void GametypeInfo::playerReadyToSpawn(PlayerInfo* player)
270    {
271        // If the player has spawned already.
272        if(this->spawned_.find(player) != this->spawned_.end())
273            return;
274
275        this->spawned_.insert(player);
276        this->changedReadyToSpawn(player);
277    }
278
279    /**
280    @brief
281        Inform the GametypeInfo about a player whose Pawn has been killed.
282    @param player
283        The player whose Pawn has been killed.
284    */
285    void GametypeInfo::pawnKilled(PlayerInfo* player)
286    {
287        NotificationListener::sendNotification("Press [Fire] to respawn", GametypeInfo::NOTIFICATION_SENDER, notificationMessageType::info, notificationSendMode::network, player->getClientID());
288        // Remove the player from the list of players that have spawned, since it currently is not.
289        this->spawned_.erase(player);
290    }
291
292    /**
293    @brief
294        Inform the GametypeInfo about a player that has spawned.
295    @param player
296        The player that has spawned.
297    */
298    void GametypeInfo::playerSpawned(PlayerInfo* player)
299    {
300        if(this->hasStarted() && !this->hasEnded())
301            NotificationListener::sendCommand("clear", GametypeInfo::NOTIFICATION_SENDER, notificationSendMode::network, player->getClientID());
302    }
303
304    void GametypeInfo::sendAnnounceMessage(const std::string& message)
305    {
306        if (GameMode::isMaster())
307        {
308            callMemberNetworkFunction(GametypeInfo, dispatchAnnounceMessage, this->getObjectID(), NETWORK_PEER_ID_BROADCAST, message);
309            this->dispatchAnnounceMessage(message);
310        }
311    }
312
313    void GametypeInfo::sendAnnounceMessage(const std::string& message, unsigned int clientID)
314    {
315        if (GameMode::isMaster())
316        {
317            if (clientID == CLIENTID_SERVER)
318                this->dispatchAnnounceMessage(message);
319            else
320                callMemberNetworkFunction(GametypeInfo, dispatchAnnounceMessage, this->getObjectID(), clientID, message);
321        }
322    }
323
324    void GametypeInfo::sendKillMessage(const std::string& message, unsigned int clientID)
325    {
326        if (GameMode::isMaster())
327        {
328            if (clientID == CLIENTID_SERVER)
329                this->dispatchKillMessage(message);
330            else
331                callMemberNetworkFunction(GametypeInfo, dispatchKillMessage, this->getObjectID(), clientID, message);
332        }
333    }
334
335    void GametypeInfo::sendDeathMessage(const std::string& message, unsigned int clientID)
336    {
337        if (GameMode::isMaster())
338        {
339            if (clientID == CLIENTID_SERVER)
340                this->dispatchDeathMessage(message);
341            else
342                callMemberNetworkFunction(GametypeInfo, dispatchDeathMessage, this->getObjectID(), clientID, message);
343        }
344    }
345
346    void GametypeInfo::sendStaticMessage(const std::string& message, unsigned int clientID, const ColourValue& colour)
347    {
348        if (GameMode::isMaster())
349        {
350            if (clientID == CLIENTID_SERVER)
351                this->dispatchStaticMessage(message, colour);
352            else
353                callMemberNetworkFunction(GametypeInfo, dispatchStaticMessage, this->getObjectID(), clientID, message, colour);
354        }
355    }
356
357    void GametypeInfo::sendFadingMessage(const std::string& message, unsigned int clientID)
358    {
359        if (GameMode::isMaster())
360        {
361            if (clientID == CLIENTID_SERVER)
362                this->dispatchFadingMessage(message);
363            else
364                callMemberNetworkFunction(GametypeInfo, dispatchFadingMessage, this->getObjectID(), clientID, message);
365        }
366    }
367
368    void GametypeInfo::dispatchAnnounceMessage(const std::string& message)
369    {
370        for (ObjectList<GametypeMessageListener>::iterator it = ObjectList<GametypeMessageListener>::begin(); it != ObjectList<GametypeMessageListener>::end(); ++it)
371            it->announcemessage(this, message);
372    }
373
374    void GametypeInfo::dispatchKillMessage(const std::string& message)
375    {
376        for (ObjectList<GametypeMessageListener>::iterator it = ObjectList<GametypeMessageListener>::begin(); it != ObjectList<GametypeMessageListener>::end(); ++it)
377            it->killmessage(this, message);
378    }
379
380    void GametypeInfo::dispatchDeathMessage(const std::string& message)
381    {
382        for (ObjectList<GametypeMessageListener>::iterator it = ObjectList<GametypeMessageListener>::begin(); it != ObjectList<GametypeMessageListener>::end(); ++it)
383            it->deathmessage(this, message);
384    }
385
386     void GametypeInfo::dispatchStaticMessage(const std::string& message, const ColourValue& colour)
387    {
388        for (ObjectList<GametypeMessageListener>::iterator it = ObjectList<GametypeMessageListener>::begin(); it != ObjectList<GametypeMessageListener>::end(); ++it)
389            it->staticmessage(this, message, colour);
390    }
391
392     void GametypeInfo::dispatchFadingMessage(const std::string& message)
393    {
394        for (ObjectList<GametypeMessageListener>::iterator it = ObjectList<GametypeMessageListener>::begin(); it != ObjectList<GametypeMessageListener>::end(); ++it)
395            it->fadingmessage(this, message);
396    }
397}
Note: See TracBrowser for help on using the repository browser.