Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5760 was 5738, checked in by landauf, 15 years ago

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