[12177] | 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 | RegisterClass(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(Context* context) : BaseObject(context) |
---|
| 57 | { |
---|
| 58 | RegisterObject(QuestListener); |
---|
| 59 | |
---|
| 60 | this->mode_ = QuestListenerMode::All; |
---|
| 61 | this->quest_ = nullptr; |
---|
| 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 | std::string questid; |
---|
| 84 | |
---|
| 85 | if (this->quest_ != nullptr) |
---|
| 86 | { |
---|
| 87 | this->quest_->addListener(this); // Adds the QuestListener to the Quests list of listeners. |
---|
| 88 | questid = this->quest_->getId(); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | orxout(verbose, context::quests) << "QuestListener created for quest: {" << questid << "} with mode '" << this->getMode() << "'." << endl; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | @brief |
---|
| 96 | Makes all QuestListener in the list aware that a certain status change has occurred and executes them if the status change affects them. |
---|
| 97 | @param listeners |
---|
| 98 | The list of QuestListeners that have to be made aware of the status change. |
---|
| 99 | @param status |
---|
| 100 | 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). |
---|
| 101 | */ |
---|
| 102 | /* static */ void QuestListener::advertiseStatusChange(std::list<QuestListener*> & listeners, const std::string & status) |
---|
| 103 | { |
---|
| 104 | for (QuestListener* listener : listeners) // Iterate through all QuestListeners |
---|
| 105 | { |
---|
| 106 | if(listener->getMode() == status || listener->getMode() == QuestListener::ALL) // Check whether the status change affects the give QuestListener. |
---|
| 107 | listener->execute(); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | @brief |
---|
| 113 | Sets the questId of the Quest the QuestListener reacts to. |
---|
| 114 | @param id |
---|
| 115 | The questId of the Quest the QUestListener reacts to. |
---|
| 116 | @return |
---|
| 117 | Returns true if successful. |
---|
| 118 | */ |
---|
| 119 | bool QuestListener::setQuestId(const std::string & id) |
---|
| 120 | { |
---|
| 121 | this->quest_ = QuestManager::getInstance().findQuest(id); // Find the Quest corresponding to the given questId. |
---|
| 122 | |
---|
| 123 | if(this->quest_ == nullptr) // If there is no such Quest. |
---|
| 124 | { |
---|
| 125 | ThrowException(Argument, "This is bad! The QuestListener has not found a Quest with a corresponding id.."); |
---|
| 126 | return false; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | return true; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | @brief |
---|
| 134 | Sets the mode of the QuestListener. |
---|
| 135 | @param mode |
---|
| 136 | The mode to be set. Can be eighter 'all', 'start', 'fail' or 'complete'. |
---|
| 137 | @return |
---|
| 138 | Returns true if successful. |
---|
| 139 | */ |
---|
| 140 | bool QuestListener::setMode(const std::string & mode) |
---|
| 141 | { |
---|
| 142 | if(mode == QuestListener::ALL) |
---|
| 143 | this->mode_ = QuestListenerMode::All; |
---|
| 144 | else if(mode == QuestListener::START) |
---|
| 145 | this->mode_ = QuestListenerMode::Start; |
---|
| 146 | else if(mode == QuestListener::FAIL) |
---|
| 147 | this->mode_ = QuestListenerMode::Fail; |
---|
| 148 | else if(mode == QuestListener::COMPLETE) |
---|
| 149 | this->mode_ = QuestListenerMode::Complete; |
---|
| 150 | else |
---|
| 151 | { |
---|
| 152 | orxout(internal_warning, context::quests) << "QuestListener with invalid mode '" << mode << "' created. Mode set to 'all'." << endl; |
---|
| 153 | this->mode_ = QuestListenerMode::All; |
---|
| 154 | return false; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | return true; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | @brief |
---|
| 162 | Get the mode of the QuestListener. |
---|
| 163 | @return |
---|
| 164 | Return the mode of the QuestListener. Can be either 'all', 'start', 'fail' or 'complete'. |
---|
| 165 | */ |
---|
| 166 | std::string QuestListener::getMode(void) |
---|
| 167 | { |
---|
| 168 | switch(this->mode_) |
---|
| 169 | { |
---|
| 170 | case QuestListenerMode::All: |
---|
| 171 | return QuestListener::ALL; |
---|
| 172 | case QuestListenerMode::Start: |
---|
| 173 | return QuestListener::START; |
---|
| 174 | case QuestListenerMode::Fail: |
---|
| 175 | return QuestListener::FAIL; |
---|
| 176 | case QuestListenerMode::Complete: |
---|
| 177 | return QuestListener::COMPLETE; |
---|
| 178 | default: // This will never happen. |
---|
| 179 | return QuestListener::ALL; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | @brief |
---|
| 185 | Get the questId of the Quest the QuestListener reacts to. |
---|
| 186 | @return |
---|
| 187 | Returns the questId of the Quest the QuestListener reacts to. |
---|
| 188 | */ |
---|
| 189 | const std::string & QuestListener::getQuestId(void) |
---|
| 190 | { |
---|
| 191 | return this->quest_->getId(); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | @brief |
---|
| 196 | Executes the QuestListener, resp. fires an Event. |
---|
| 197 | @return |
---|
| 198 | Returns true if successful. |
---|
| 199 | */ |
---|
| 200 | bool QuestListener::execute() |
---|
| 201 | { |
---|
| 202 | this->fireEvent(true); |
---|
| 203 | return true; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | } |
---|