| 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 the |
|---|
| 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 QuestListener.cc |
|---|
| 31 | @brief Implementation of the QuestListener class. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include "QuestListener.h" |
|---|
| 35 | |
|---|
| 36 | #include "core/CoreIncludes.h" |
|---|
| 37 | #include "core/XMLPort.h" |
|---|
| 38 | |
|---|
| 39 | #include "Quest.h" |
|---|
| 40 | #include "QuestManager.h" |
|---|
| 41 | |
|---|
| 42 | namespace orxonox |
|---|
| 43 | { |
|---|
| 44 | CreateFactory(QuestListener); |
|---|
| 45 | |
|---|
| 46 | // Initialization of the static variables for the modes as strings. |
|---|
| 47 | /*static*/ const std::string QuestListener::ALL = "all"; |
|---|
| 48 | /*static*/ const std::string QuestListener::START = "start"; |
|---|
| 49 | /*static*/ const std::string QuestListener::FAIL = "fail"; |
|---|
| 50 | /*static*/ const std::string QuestListener::COMPLETE = "complete"; |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | @brief |
|---|
| 54 | Constructor. Registers the object and initializes variables. |
|---|
| 55 | */ |
|---|
| 56 | QuestListener::QuestListener(BaseObject* creator) : BaseObject(creator) |
|---|
| 57 | { |
|---|
| 58 | RegisterObject(QuestListener); |
|---|
| 59 | |
|---|
| 60 | this->mode_ = QuestListenerMode::All; |
|---|
| 61 | this->quest_ = NULL; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | @brief |
|---|
| 66 | Destructor. |
|---|
| 67 | */ |
|---|
| 68 | QuestListener::~QuestListener() |
|---|
| 69 | { |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | @brief |
|---|
| 74 | Method for creating a Quest object through XML. |
|---|
| 75 | */ |
|---|
| 76 | void QuestListener::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 77 | { |
|---|
| 78 | SUPER(QuestListener, XMLPort, xmlelement, mode); |
|---|
| 79 | |
|---|
| 80 | XMLPortParam(QuestListener, "questId", setQuestId, getQuestId, xmlelement, mode); |
|---|
| 81 | XMLPortParam(QuestListener, "mode", setMode, getMode, xmlelement, mode); |
|---|
| 82 | |
|---|
| 83 | if(this->quest_ != NULL) |
|---|
| 84 | this->quest_->addListener(this); // Adds the QuestListener to the Quests list of listeners. |
|---|
| 85 | |
|---|
| 86 | orxout(verbose, context::quests) << "QuestListener created for quest: {" << this->quest_->getId() << "} with mode '" << this->getMode() << "'." << endl; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | @brief |
|---|
| 91 | Makes all QuestListener in the list aware that a certain status change has occurred and executes them if the status change affects them. |
|---|
| 92 | @param listeners |
|---|
| 93 | The list of QuestListeners that have to be made aware of the status change. |
|---|
| 94 | @param status |
|---|
| 95 | The status that has changed. Can be 'start' (if the Quest was started), 'complete' (if the Quest was completed) or 'fail' (if the Quest was failed). |
|---|
| 96 | */ |
|---|
| 97 | /* static */ void QuestListener::advertiseStatusChange(std::list<QuestListener*> & listeners, const std::string & status) |
|---|
| 98 | { |
|---|
| 99 | for (std::list<QuestListener*>::iterator it = listeners.begin(); it != listeners.end(); ++it) // Iterate through all QuestListeners |
|---|
| 100 | { |
|---|
| 101 | QuestListener* listener = *it; |
|---|
| 102 | if(listener->getMode() == status || listener->getMode() == QuestListener::ALL) // Check whether the status change affects the give QuestListener. |
|---|
| 103 | listener->execute(); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | @brief |
|---|
| 109 | Sets the questId of the Quest the QuestListener reacts to. |
|---|
| 110 | @param id |
|---|
| 111 | The questId of the Quest the QUestListener reacts to. |
|---|
| 112 | @return |
|---|
| 113 | Returns true if successful. |
|---|
| 114 | */ |
|---|
| 115 | bool QuestListener::setQuestId(const std::string & id) |
|---|
| 116 | { |
|---|
| 117 | this->quest_ = QuestManager::getInstance().findQuest(id); // Find the Quest corresponding to the given questId. |
|---|
| 118 | |
|---|
| 119 | if(this->quest_ == NULL) // If there is no such Quest. |
|---|
| 120 | { |
|---|
| 121 | ThrowException(Argument, "This is bad! The QuestListener has not found a Quest with a corresponding id.."); |
|---|
| 122 | return false; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | return true; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /** |
|---|
| 129 | @brief |
|---|
| 130 | Sets the mode of the QuestListener. |
|---|
| 131 | @param mode |
|---|
| 132 | The mode to be set. Can be eighter 'all', 'start', 'fail' or 'complete'. |
|---|
| 133 | @return |
|---|
| 134 | Returns true if successful. |
|---|
| 135 | */ |
|---|
| 136 | bool QuestListener::setMode(const std::string & mode) |
|---|
| 137 | { |
|---|
| 138 | if(mode == QuestListener::ALL) |
|---|
| 139 | this->mode_ = QuestListenerMode::All; |
|---|
| 140 | else if(mode == QuestListener::START) |
|---|
| 141 | this->mode_ = QuestListenerMode::Start; |
|---|
| 142 | else if(mode == QuestListener::FAIL) |
|---|
| 143 | this->mode_ = QuestListenerMode::Fail; |
|---|
| 144 | else if(mode == QuestListener::COMPLETE) |
|---|
| 145 | this->mode_ = QuestListenerMode::Complete; |
|---|
| 146 | else |
|---|
| 147 | { |
|---|
| 148 | orxout(internal_warning, context::quests) << "QuestListener with invalid mode '" << mode << "' created. Mode set to 'all'." << endl; |
|---|
| 149 | this->mode_ = QuestListenerMode::All; |
|---|
| 150 | return false; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | return true; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | @brief |
|---|
| 158 | Get the mode of the QuestListener. |
|---|
| 159 | @return |
|---|
| 160 | Return the mode of the QuestListener. Can be either 'all', 'start', 'fail' or 'complete'. |
|---|
| 161 | */ |
|---|
| 162 | std::string QuestListener::getMode(void) |
|---|
| 163 | { |
|---|
| 164 | switch(this->mode_) |
|---|
| 165 | { |
|---|
| 166 | case QuestListenerMode::All: |
|---|
| 167 | return QuestListener::ALL; |
|---|
| 168 | case QuestListenerMode::Start: |
|---|
| 169 | return QuestListener::START; |
|---|
| 170 | case QuestListenerMode::Fail: |
|---|
| 171 | return QuestListener::FAIL; |
|---|
| 172 | case QuestListenerMode::Complete: |
|---|
| 173 | return QuestListener::COMPLETE; |
|---|
| 174 | default: // This will never happen. |
|---|
| 175 | return QuestListener::ALL; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | @brief |
|---|
| 181 | Get the questId of the Quest the QuestListener reacts to. |
|---|
| 182 | @return |
|---|
| 183 | Returns the questId of the Quest the QuestListener reacts to. |
|---|
| 184 | */ |
|---|
| 185 | const std::string & QuestListener::getQuestId(void) |
|---|
| 186 | { |
|---|
| 187 | return this->quest_->getId(); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | /** |
|---|
| 191 | @brief |
|---|
| 192 | Executes the QuestListener, resp. fires an Event. |
|---|
| 193 | @return |
|---|
| 194 | Returns true if successful. |
|---|
| 195 | */ |
|---|
| 196 | bool QuestListener::execute() |
|---|
| 197 | { |
|---|
| 198 | this->fireEvent(true); |
|---|
| 199 | return true; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | } |
|---|