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