Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7525 was 7489, checked in by dafrick, 14 years ago

Mostly more documentation.

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