Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Adding new level notifications.oxw, to "showcase", or at this stage rather "test" Notifications.
Restoring tutorial.oxw to its old state, before it was hijacked by me for testing.

Extending Script class. Now also normal orxonox code can be executed with it, the execution of code can be triggered with Triggers (obviously) and cod can also executed on load.
I needed this to load the NotificationLayer in levels where it is needed.
Also inserted a Script that loads the NotificationQueue to display Notifications in all levels it was needed.

  • Property svn:eol-style set to native
File size: 4.6 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    @return
108        Returns true if the NotificationDispatcher was successfully triggered.
109    */
110    bool NotificationDispatcher::trigger(bool triggered, BaseObject* trigger)
111    {
112        if(!triggered || !this->isActive()) // If the NotificationDispatcher is inactive it cannot be executed.
113            return false;
114
115        COUT(4) << "NotificationDispatcher (&" << this << ") triggered." << std::endl;
116
117        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
118        Pawn* pawn = NULL;
119
120        // If the trigger is a PlayerTrigger.
121        if(pTrigger != NULL)
122        {
123            if(!pTrigger->isForPlayer())  //!< The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
124                return false;
125            else
126                pawn = pTrigger->getTriggeringPlayer();
127        }
128        else
129            return false;
130
131        if(pawn == NULL)
132        {
133            COUT(4) << "The QuestEffectBeacon was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << std::endl;
134            return false;
135        }
136
137        //! Extract the PlayerInfo from the Pawn.
138        PlayerInfo* player = pawn->getPlayer();
139
140        if(player == NULL)
141        {
142            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
143            return false;
144        }
145
146        this->dispatch(player->getClientID());
147
148        return true;
149    }
150
151}
Note: See TracBrowser for help on using the repository browser.