Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: sandbox/src/modules/questsystem/QuestListener.cc @ 5773

Last change on this file since 5773 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
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
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#include "Quest.h"
39#include "QuestManager.h"
40
41namespace orxonox
42{
43    CreateFactory(QuestListener);
44
45    /**
46    @brief
47        Constructor. Registers the object and initializes variables.
48    */
49    QuestListener::QuestListener(BaseObject* creator) : BaseObject(creator)
50    {
51        RegisterObject(QuestListener);
52       
53        this->mode_ = QuestListenerMode::All;
54        this->quest_ = NULL;
55    }
56   
57    /**
58    @brief
59        Destructor.
60    */
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);
74        XMLPortParam(QuestListener, "mode", setMode, getMode, xmlelement, mode);
75
76        this->quest_->addListener(this); //!< Adds the QuestListener to the Quests list of listeners.
77       
78        COUT(3) << "QuestListener created for quest: {" << this->quest_->getId() << "} with mode '" << this->getMode() << "'." << std::endl;
79    }
80   
81    /**
82    @brief
83        Makes all QuestListener in the list aware that a certain status change has occurred and executes them if the status change affects them.
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)
90    {
91        for (std::list<QuestListener*>::iterator it = listeners.begin(); it != listeners.end(); ++it) //!< Iterate through all QuestListeners
92        {
93            QuestListener* listener = *it;
94            if(listener->getMode() == status || listener->getMode() == "all") //!< Check whether the status change affects the give QuestListener.
95            {
96                listener->execute();
97            }
98        }
99    }
100   
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    */
109    bool QuestListener::setQuestId(const std::string & id)
110    {
111        this->quest_ = QuestManager::getInstance().findQuest(id); //!< Find the Quest corresponding to the given questId.
112       
113        if(this->quest_ == NULL) //!< If there is no such Quest.
114        {
115            ThrowException(Argument, "This is bad! The QuestListener has not found a Quest with a corresponding id..");
116            return false;
117        }
118       
119        return true;
120    }
121   
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)
131    {
132        if(mode == "all")
133        {
134            this->mode_ = QuestListenerMode::All;
135        }
136        else if(mode == "start")
137        {
138            this->mode_ = QuestListenerMode::Start;
139        }
140        else if(mode == "fail")
141        {
142            this->mode_ = QuestListenerMode::Fail;
143        }
144        else if(mode == "complete")
145        {
146            this->mode_ = QuestListenerMode::Complete;
147        }
148        else
149        {
150            COUT(2) << "QuestListener with invalid mode '" << mode << "' created. Mode set to 'all'." << std::endl;
151        this->mode_ = QuestListenerMode::All;
152        return false;
153        }
154       
155        return true;
156    }
157   
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    */
164    std::string QuestListener::getMode(void)
165    {
166        if(this->mode_ == QuestListenerMode::All)
167        {
168            return "all";
169        }
170        else if(this->mode_ == QuestListenerMode::Start)
171        {
172            return "start";
173        }
174        else if(this->mode_ == QuestListenerMode::Fail)
175        {
176            return "fail";
177        }
178        else if(this->mode_ == QuestListenerMode::Complete)
179        {
180            return "complete";
181        }
182        else
183        {
184            COUT(1) << "An unforseen, never to happen, Error has occurred. This is impossible!" << std::endl;
185        return "";
186        }
187    }
188
189    /**
190    @brief
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
202        Executes the QuestListener, resp. fires an Event.
203    @return
204        Returns true if successful.
205    */
206    bool QuestListener::execute()
207    {
208        this->fireEvent(true);
209        return true;
210    }
211
212}
Note: See TracBrowser for help on using the repository browser.