Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11823 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 6.6 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"
[10624]39#include "network/NetworkFunctionIncludes.h"
[7474]40#include "network/Host.h"
41
42#include "infos/PlayerInfo.h"
[8706]43#include "interfaces/NotificationListener.h"
[7403]44#include "interfaces/PlayerTrigger.h"
45#include "worldentities/pawns/Pawn.h"
[7193]46
47namespace orxonox
48{
49
[9667]50    RegisterUnloadableClass(NotificationDispatcher);
[7193]51
[8891]52    registerMemberNetworkFunction(NotificationDispatcher, broadcastHelper);
[7474]53    registerMemberNetworkFunction(NotificationDispatcher, dispatch);
54
[7193]55    /**
56    @brief
57        Default constructor. Initializes the object.
58    */
[9667]59    NotificationDispatcher::NotificationDispatcher(Context* context) : BaseObject(context), Synchronisable(context)
[7193]60    {
61        RegisterObject(NotificationDispatcher);
62
[8706]63        this->sender_ = NotificationListener::NONE;
[8891]64        this->bBroadcast_ = false;
[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
[8891]85        XMLPortParam(NotificationDispatcher, "sender", setSender, getSender, xmlelement, mode);
86        XMLPortParam(NotificationDispatcher, "broadcast", setBroadcasting, isBroadcasting, xmlelement, mode);
87
88        XMLPortEventSink(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode);
[7193]89    }
90
[7407]91    void NotificationDispatcher::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
[7193]92    {
[7407]93        SUPER(NotificationDispatcher, XMLEventPort, xmlelement, mode);
[7193]94
[7407]95        XMLPortEventState(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode);
[7193]96    }
97
[7484]98    /**
99    @brief
100        Registers variables for synchronisation.
101    */
[7474]102    void NotificationDispatcher::registerVariables(void)
103    {
104        registerVariable(this->sender_, VariableDirection::ToClient);
105    }
106
[7193]107    /**
108    @brief
[8891]109        Broadcasts a specific Notification.
110    */
111    void NotificationDispatcher::broadcast(void)
112    {
113        // TODO: Needed?
114        const std::string message = this->createNotificationMessage();
[11071]115        NotificationListener::sendNotification(message, this->getSender(), NotificationMessageType::info, NotificationSendMode::local);
[8891]116
117        // Broadcast
118        if(!GameMode::isStandalone())
119        {
[10624]120            callMemberNetworkFunction(&NotificationDispatcher::broadcastHelper, this->getObjectID(), NETWORK_PEER_ID_BROADCAST);
[8891]121        }
122    }
123
124    /**
125    @brief
126        Helper function for broadcast.
127    */
128    void NotificationDispatcher::broadcastHelper(void)
129    {
130        this->dispatch(Host::getPlayerID());
131    }
132
133    /**
134    @brief
[7193]135        Dispatches a Notification with a message supplied by the createNotificationMessage() method, which can be overloaded.
[7403]136    @param clientId
137        The id of the client the notification should be dispatched to.
[7193]138    */
[7403]139    void NotificationDispatcher::dispatch(unsigned int clientId)
[7193]140    {
[8891]141        // We don't call sendNotification() directly on the server, because if might be necessary that createNotificationMessage() is executed on the client as the message may be client-specific.
142        if(GameMode::isStandalone() || Host::getPlayerID() == clientId || this->getSyncMode() == ObjectDirection::None)
[7474]143        {
144            const std::string message = this->createNotificationMessage();
[8706]145            // TODO: Make the type configurable.
[11071]146            NotificationListener::sendNotification(message, this->getSender(), NotificationMessageType::info, NotificationSendMode::network, clientId);
[7474]147        }
148        else if(GameMode::isServer())
149        {
[10624]150            callMemberNetworkFunction(&NotificationDispatcher::dispatch, this->getObjectID(), clientId, clientId);
[7474]151        }
[7193]152    }
153
154    /**
155    @brief
156        Is called when the NotificationDispatcher is triggered.
157    @param triggered
158        Whether it has been triggered or untriggered. The NotificationDispatcher only reacts to the first kind of events.
[7408]159    @param trigger
160        The object that caused the event to be fired.
[7193]161    @return
162        Returns true if the NotificationDispatcher was successfully triggered.
163    */
[7403]164    bool NotificationDispatcher::trigger(bool triggered, BaseObject* trigger)
[7193]165    {
166        if(!triggered || !this->isActive()) // If the NotificationDispatcher is inactive it cannot be executed.
167            return false;
168
[8858]169        orxout(verbose, context::notifications) << "NotificationDispatcher (&" << this << ") triggered." << endl;
[7193]170
[8891]171        // If the NotificationDispatcher is set to broadcast.
172        if(this->isBroadcasting())
173        {
174            this->broadcast();
175            return true;
176        }
177
[7403]178        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
[11071]179        PlayerInfo* player = nullptr;
[7193]180
[7403]181        // If the trigger is a PlayerTrigger.
[11071]182        if(pTrigger != nullptr)
[7403]183        {
[7474]184            if(!pTrigger->isForPlayer())  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
[7403]185                return false;
186            else
[8706]187                player = pTrigger->getTriggeringPlayer();
[7403]188        }
189        else
190            return false;
191
[11071]192        if(player == nullptr)
[7403]193        {
[8858]194            orxout(verbose, context::notifications) << "The NotificationDispatcher was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << endl;
[7403]195            return false;
196        }
197
198        this->dispatch(player->getClientID());
199
[7193]200        return true;
201    }
202
203}
Note: See TracBrowser for help on using the repository browser.