[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 | /** |
---|
| 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" |
---|
[7403] | 41 | #include "interfaces/PlayerTrigger.h" |
---|
| 42 | #include "infos/PlayerInfo.h" |
---|
| 43 | #include "worldentities/pawns/Pawn.h" |
---|
[7193] | 44 | |
---|
| 45 | namespace 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 | */ |
---|
[7407] | 74 | void NotificationDispatcher::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[7193] | 75 | { |
---|
[7407] | 76 | SUPER(NotificationDispatcher, XMLPort, xmlelement, mode); |
---|
[7193] | 77 | |
---|
[7407] | 78 | XMLPortEventSink(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode); //TODO: Change BaseObject to MultiTrigger as soon as MultiTrigger is the base of all triggers. |
---|
[7193] | 79 | } |
---|
| 80 | |
---|
[7407] | 81 | void NotificationDispatcher::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[7193] | 82 | { |
---|
[7407] | 83 | SUPER(NotificationDispatcher, XMLEventPort, xmlelement, mode); |
---|
[7193] | 84 | |
---|
[7407] | 85 | XMLPortEventState(NotificationDispatcher, BaseObject, "trigger", trigger, xmlelement, mode); |
---|
[7193] | 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | @brief |
---|
| 90 | Dispatches a Notification with a message supplied by the createNotificationMessage() method, which can be overloaded. |
---|
[7403] | 91 | @param clientId |
---|
| 92 | The id of the client the notification should be dispatched to. |
---|
[7193] | 93 | */ |
---|
[7403] | 94 | void NotificationDispatcher::dispatch(unsigned int clientId) |
---|
[7193] | 95 | { |
---|
| 96 | const std::string message = this->createNotificationMessage(); |
---|
| 97 | Notification* notification = new Notification(this, message); |
---|
| 98 | |
---|
[7403] | 99 | notification->send(clientId, this->getSender()); |
---|
[7193] | 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. |
---|
[7408] | 107 | @param trigger |
---|
| 108 | The object that caused the event to be fired. |
---|
[7193] | 109 | @return |
---|
| 110 | Returns true if the NotificationDispatcher was successfully triggered. |
---|
| 111 | */ |
---|
[7403] | 112 | bool NotificationDispatcher::trigger(bool triggered, BaseObject* trigger) |
---|
[7193] | 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 | |
---|
[7403] | 119 | PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger); |
---|
| 120 | Pawn* pawn = NULL; |
---|
[7193] | 121 | |
---|
[7403] | 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 | |
---|
[7193] | 150 | return true; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | } |
---|