[2329] | 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 | |
---|
[2352] | 29 | /** |
---|
[7456] | 30 | @file QuestListener.cc |
---|
[2352] | 31 | @brief Implementation of the QuestListener class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[2329] | 34 | #include "QuestListener.h" |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/XMLPort.h" |
---|
[7456] | 38 | |
---|
[2329] | 39 | #include "Quest.h" |
---|
| 40 | #include "QuestManager.h" |
---|
| 41 | |
---|
[2435] | 42 | namespace orxonox |
---|
| 43 | { |
---|
[9667] | 44 | RegisterClass(QuestListener); |
---|
[2329] | 45 | |
---|
[7462] | 46 | // Initialization of the static variables for the modes as strings. |
---|
[7456] | 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 | |
---|
[2352] | 52 | /** |
---|
| 53 | @brief |
---|
| 54 | Constructor. Registers the object and initializes variables. |
---|
| 55 | */ |
---|
[9667] | 56 | QuestListener::QuestListener(Context* context) : BaseObject(context) |
---|
[2329] | 57 | { |
---|
| 58 | RegisterObject(QuestListener); |
---|
[6417] | 59 | |
---|
[3280] | 60 | this->mode_ = QuestListenerMode::All; |
---|
[11071] | 61 | this->quest_ = nullptr; |
---|
[2329] | 62 | } |
---|
[6417] | 63 | |
---|
[2352] | 64 | /** |
---|
| 65 | @brief |
---|
| 66 | Destructor. |
---|
| 67 | */ |
---|
[2329] | 68 | QuestListener::~QuestListener() |
---|
| 69 | { |
---|
| 70 | } |
---|
[6417] | 71 | |
---|
[2329] | 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); |
---|
[2352] | 81 | XMLPortParam(QuestListener, "mode", setMode, getMode, xmlelement, mode); |
---|
[2329] | 82 | |
---|
[11083] | 83 | std::string questid; |
---|
| 84 | |
---|
| 85 | if (this->quest_ != nullptr) |
---|
| 86 | { |
---|
[7456] | 87 | this->quest_->addListener(this); // Adds the QuestListener to the Quests list of listeners. |
---|
[11083] | 88 | questid = this->quest_->getId(); |
---|
| 89 | } |
---|
[6417] | 90 | |
---|
[11083] | 91 | orxout(verbose, context::quests) << "QuestListener created for quest: {" << questid << "} with mode '" << this->getMode() << "'." << endl; |
---|
[2329] | 92 | } |
---|
[6417] | 93 | |
---|
[2352] | 94 | /** |
---|
| 95 | @brief |
---|
[3370] | 96 | Makes all QuestListener in the list aware that a certain status change has occurred and executes them if the status change affects them. |
---|
[2352] | 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) |
---|
[2329] | 103 | { |
---|
[11071] | 104 | for (QuestListener* listener : listeners) // Iterate through all QuestListeners |
---|
[2329] | 105 | { |
---|
[7456] | 106 | if(listener->getMode() == status || listener->getMode() == QuestListener::ALL) // Check whether the status change affects the give QuestListener. |
---|
[2329] | 107 | listener->execute(); |
---|
| 108 | } |
---|
| 109 | } |
---|
[6417] | 110 | |
---|
[2352] | 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 | */ |
---|
[2329] | 119 | bool QuestListener::setQuestId(const std::string & id) |
---|
| 120 | { |
---|
[7456] | 121 | this->quest_ = QuestManager::getInstance().findQuest(id); // Find the Quest corresponding to the given questId. |
---|
[6417] | 122 | |
---|
[11071] | 123 | if(this->quest_ == nullptr) // If there is no such Quest. |
---|
[2329] | 124 | { |
---|
[2383] | 125 | ThrowException(Argument, "This is bad! The QuestListener has not found a Quest with a corresponding id.."); |
---|
[2329] | 126 | return false; |
---|
| 127 | } |
---|
[6417] | 128 | |
---|
[2329] | 129 | return true; |
---|
| 130 | } |
---|
[6417] | 131 | |
---|
[2352] | 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) |
---|
[2329] | 141 | { |
---|
[7456] | 142 | if(mode == QuestListener::ALL) |
---|
[3280] | 143 | this->mode_ = QuestListenerMode::All; |
---|
[7456] | 144 | else if(mode == QuestListener::START) |
---|
[3280] | 145 | this->mode_ = QuestListenerMode::Start; |
---|
[7456] | 146 | else if(mode == QuestListener::FAIL) |
---|
[3280] | 147 | this->mode_ = QuestListenerMode::Fail; |
---|
[7456] | 148 | else if(mode == QuestListener::COMPLETE) |
---|
[3280] | 149 | this->mode_ = QuestListenerMode::Complete; |
---|
[2329] | 150 | else |
---|
| 151 | { |
---|
[8858] | 152 | orxout(internal_warning, context::quests) << "QuestListener with invalid mode '" << mode << "' created. Mode set to 'all'." << endl; |
---|
[7456] | 153 | this->mode_ = QuestListenerMode::All; |
---|
| 154 | return false; |
---|
[2329] | 155 | } |
---|
[6417] | 156 | |
---|
[2352] | 157 | return true; |
---|
[2329] | 158 | } |
---|
[6417] | 159 | |
---|
[2352] | 160 | /** |
---|
| 161 | @brief |
---|
| 162 | Get the mode of the QuestListener. |
---|
| 163 | @return |
---|
[7456] | 164 | Return the mode of the QuestListener. Can be either 'all', 'start', 'fail' or 'complete'. |
---|
[2352] | 165 | */ |
---|
[3196] | 166 | std::string QuestListener::getMode(void) |
---|
[2329] | 167 | { |
---|
[7456] | 168 | switch(this->mode_) |
---|
[2329] | 169 | { |
---|
[7456] | 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; |
---|
[2352] | 180 | } |
---|
[2329] | 181 | } |
---|
| 182 | |
---|
[2352] | 183 | /** |
---|
| 184 | @brief |
---|
[3196] | 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 | */ |
---|
[6417] | 189 | const std::string & QuestListener::getQuestId(void) |
---|
[3196] | 190 | { |
---|
| 191 | return this->quest_->getId(); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | @brief |
---|
[2352] | 196 | Executes the QuestListener, resp. fires an Event. |
---|
| 197 | @return |
---|
| 198 | Returns true if successful. |
---|
| 199 | */ |
---|
| 200 | bool QuestListener::execute() |
---|
| 201 | { |
---|
[2383] | 202 | this->fireEvent(true); |
---|
[2352] | 203 | return true; |
---|
| 204 | } |
---|
[2329] | 205 | |
---|
| 206 | } |
---|