Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7163 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

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