Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Did not get all xmlElements…

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