Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/notifications/NotificationDispatcher.cc @ 7480

Last change on this file since 7480 was 7474, checked in by dafrick, 14 years ago

Synchronizing Notifications.
In the course of that, notifications are not longer sent by creating a Notification and the calling notification.send() bur by letting the NotificationManager handle all this: NotificationManager::getInstance().sendNotification(message)
This made QuestNotification obsolete, thus it was removde.

Also did some work on synchronizing the Script class. It should work properly most of the time, but the current solution is unreliable and unsatisfactory. So this will change as soon as I know how.

  • Property svn:eol-style set to native
File size: 5.3 KB
RevLine 
[7193]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 thes
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 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
[7456]30    @file NotificationDispatcher.cc
[7193]31    @brief Implementation of the NotificationDispatcher class.
32*/
33
34#include "NotificationDispatcher.h"
35
36#include "core/CoreIncludes.h"
[7474]37#include "core/EventIncludes.h"
[7193]38#include "core/XMLPort.h"
[7474]39#include "network/NetworkFunction.h"
40#include "network/Host.h"
41
42#include "infos/PlayerInfo.h"
[7403]43#include "interfaces/PlayerTrigger.h"
44#include "worldentities/pawns/Pawn.h"
[7193]45
[7474]46#include "Notification.h"
47#include "NotificationManager.h"
48
[7193]49namespace orxonox
50{
51
52    CreateUnloadableFactory(NotificationDispatcher);
53
[7474]54    registerMemberNetworkFunction(NotificationDispatcher, dispatch);
55
[7193]56    /**
57    @brief
58        Default constructor. Initializes the object.
59    */
[7474]60    NotificationDispatcher::NotificationDispatcher(BaseObject* creator) : BaseObject(creator), Synchronisable(creator)
[7193]61    {
62        RegisterObject(NotificationDispatcher);
63
64        this->sender_ = NotificationManager::NONE;
[7474]65        this->registerVariables();
[7193]66    }
67
68    /**
69    @brief
70        Destructor.
71    */
72    NotificationDispatcher::~NotificationDispatcher()
73    {
74
75    }
76
77    /**
78    @brief
79        Method for creating a NotificationDispatcher object through XML.
80    */
[7407]81    void NotificationDispatcher::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[7193]82    {
[7407]83        SUPER(NotificationDispatcher, XMLPort, xmlelement, mode);
[7193]84
[7407]85        XMLPortEventSink(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode); //TODO: Change BaseObject to MultiTrigger as soon as MultiTrigger is the base of all triggers.
[7193]86    }
87
[7407]88    void NotificationDispatcher::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
[7193]89    {
[7407]90        SUPER(NotificationDispatcher, XMLEventPort, xmlelement, mode);
[7193]91
[7407]92        XMLPortEventState(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode);
[7193]93    }
94
[7474]95    void NotificationDispatcher::registerVariables(void)
96    {
97        registerVariable(this->sender_, VariableDirection::ToClient);
98    }
99
[7193]100    /**
101    @brief
102        Dispatches a Notification with a message supplied by the createNotificationMessage() method, which can be overloaded.
[7403]103    @param clientId
104        The id of the client the notification should be dispatched to.
[7193]105    */
[7403]106    void NotificationDispatcher::dispatch(unsigned int clientId)
[7193]107    {
[7474]108        if(GameMode::isStandalone() || Host::getPlayerID() == clientId || this->getSyncMode() == 0x0)
109        {
110            const std::string message = this->createNotificationMessage();
111            NotificationManager::sendNotification(message, clientId, this->getSender());
112        }
113        else if(GameMode::isServer())
114        {
115            callMemberNetworkFunction(NotificationDispatcher, dispatch, this->getObjectID(), clientId, clientId);
116        }
[7193]117    }
118
119    /**
120    @brief
121        Is called when the NotificationDispatcher is triggered.
122    @param triggered
123        Whether it has been triggered or untriggered. The NotificationDispatcher only reacts to the first kind of events.
[7408]124    @param trigger
125        The object that caused the event to be fired.
[7193]126    @return
127        Returns true if the NotificationDispatcher was successfully triggered.
128    */
[7403]129    bool NotificationDispatcher::trigger(bool triggered, BaseObject* trigger)
[7193]130    {
131        if(!triggered || !this->isActive()) // If the NotificationDispatcher is inactive it cannot be executed.
132            return false;
133
134        COUT(4) << "NotificationDispatcher (&" << this << ") triggered." << std::endl;
135
[7403]136        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
137        Pawn* pawn = NULL;
[7193]138
[7403]139        // If the trigger is a PlayerTrigger.
140        if(pTrigger != NULL)
141        {
[7474]142            if(!pTrigger->isForPlayer())  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
[7403]143                return false;
144            else
145                pawn = pTrigger->getTriggeringPlayer();
146        }
147        else
148            return false;
149
150        if(pawn == NULL)
151        {
[7474]152            COUT(4) << "The NotificationDispatcher was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << std::endl;
[7403]153            return false;
154        }
155
[7474]156        // Extract the PlayerInfo from the Pawn.
[7403]157        PlayerInfo* player = pawn->getPlayer();
158
159        if(player == NULL)
160        {
161            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
162            return false;
163        }
164
165        this->dispatch(player->getClientID());
166
[7193]167        return true;
168    }
169
170}
Note: See TracBrowser for help on using the repository browser.