Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/QuestListener.cc @ 11071

Last change on this file since 11071 was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
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
42namespace 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        if(this->quest_ != nullptr)
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 (QuestListener* listener : listeners) // Iterate through all QuestListeners
100        {
101            if(listener->getMode() == status || listener->getMode() == QuestListener::ALL) // Check whether the status change affects the give QuestListener.
102                listener->execute();
103        }
104    }
105
106    /**
107    @brief
108        Sets the questId of the Quest the QuestListener reacts to.
109    @param id
110        The questId of the Quest the QUestListener reacts to.
111    @return
112        Returns true if successful.
113    */
114    bool QuestListener::setQuestId(const std::string & id)
115    {
116        this->quest_ = QuestManager::getInstance().findQuest(id); // Find the Quest corresponding to the given questId.
117
118        if(this->quest_ == nullptr) // If there is no such Quest.
119        {
120            ThrowException(Argument, "This is bad! The QuestListener has not found a Quest with a corresponding id..");
121            return false;
122        }
123
124        return true;
125    }
126
127    /**
128    @brief
129        Sets the mode of the QuestListener.
130    @param mode
131        The mode to be set. Can be eighter 'all', 'start', 'fail' or 'complete'.
132    @return
133        Returns true if successful.
134    */
135    bool QuestListener::setMode(const std::string & mode)
136    {
137        if(mode == QuestListener::ALL)
138            this->mode_ = QuestListenerMode::All;
139        else if(mode == QuestListener::START)
140            this->mode_ = QuestListenerMode::Start;
141        else if(mode == QuestListener::FAIL)
142            this->mode_ = QuestListenerMode::Fail;
143        else if(mode == QuestListener::COMPLETE)
144            this->mode_ = QuestListenerMode::Complete;
145        else
146        {
147            orxout(internal_warning, context::quests) << "QuestListener with invalid mode '" << mode << "' created. Mode set to 'all'." << endl;
148            this->mode_ = QuestListenerMode::All;
149            return false;
150        }
151
152        return true;
153    }
154
155    /**
156    @brief
157        Get the mode of the QuestListener.
158    @return
159        Return the mode of the QuestListener. Can be either 'all', 'start', 'fail' or 'complete'.
160    */
161    std::string QuestListener::getMode(void)
162    {
163        switch(this->mode_)
164        {
165            case QuestListenerMode::All:
166                return QuestListener::ALL;
167            case QuestListenerMode::Start:
168                return QuestListener::START;
169            case QuestListenerMode::Fail:
170                return QuestListener::FAIL;
171            case QuestListenerMode::Complete:
172                return QuestListener::COMPLETE;
173            default: // This will never happen.
174                return QuestListener::ALL;
175        }
176    }
177
178    /**
179    @brief
180        Get the questId of the Quest the QuestListener reacts to.
181    @return
182        Returns the questId of the Quest the QuestListener reacts to.
183    */
184    const std::string & QuestListener::getQuestId(void)
185    {
186        return this->quest_->getId();
187    }
188
189    /**
190    @brief
191        Executes the QuestListener, resp. fires an Event.
192    @return
193        Returns true if successful.
194    */
195    bool QuestListener::execute()
196    {
197        this->fireEvent(true);
198        return true;
199    }
200
201}
Note: See TracBrowser for help on using the repository browser.