Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/modules/questsystem/QuestListener.cc @ 6940

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

Resolved issue with the Questsystem causing a segfault when a level with Quests was loaded twice. The issue still persists, but not with the Questsystem anymore and it will be addressed with my next commit.
Also removed restrictions to the number of characters a quest/hint identifier must have.

  • 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        if(this->quest_ != NULL)
77            this->quest_->addListener(this); //!< Adds the QuestListener to the Quests list of listeners.
78
79        COUT(3) << "QuestListener created for quest: {" << this->quest_->getId() << "} with mode '" << this->getMode() << "'." << std::endl;
80    }
81
82    /**
83    @brief
84        Makes all QuestListener in the list aware that a certain status change has occurred and executes them if the status change affects them.
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)
91    {
92        for (std::list<QuestListener*>::iterator it = listeners.begin(); it != listeners.end(); ++it) //!< Iterate through all QuestListeners
93        {
94            QuestListener* listener = *it;
95            if(listener->getMode() == status || listener->getMode() == "all") //!< Check whether the status change affects the give QuestListener.
96            {
97                listener->execute();
98            }
99        }
100    }
101
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    */
110    bool QuestListener::setQuestId(const std::string & id)
111    {
112        this->quest_ = QuestManager::getInstance().findQuest(id); //!< Find the Quest corresponding to the given questId.
113
114        if(this->quest_ == NULL) //!< If there is no such Quest.
115        {
116            ThrowException(Argument, "This is bad! The QuestListener has not found a Quest with a corresponding id..");
117            return false;
118        }
119
120        return true;
121    }
122
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)
132    {
133        if(mode == "all")
134        {
135            this->mode_ = QuestListenerMode::All;
136        }
137        else if(mode == "start")
138        {
139            this->mode_ = QuestListenerMode::Start;
140        }
141        else if(mode == "fail")
142        {
143            this->mode_ = QuestListenerMode::Fail;
144        }
145        else if(mode == "complete")
146        {
147            this->mode_ = QuestListenerMode::Complete;
148        }
149        else
150        {
151            COUT(2) << "QuestListener with invalid mode '" << mode << "' created. Mode set to 'all'." << std::endl;
152        this->mode_ = QuestListenerMode::All;
153        return false;
154        }
155
156        return true;
157    }
158
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    */
165    std::string QuestListener::getMode(void)
166    {
167        if(this->mode_ == QuestListenerMode::All)
168        {
169            return "all";
170        }
171        else if(this->mode_ == QuestListenerMode::Start)
172        {
173            return "start";
174        }
175        else if(this->mode_ == QuestListenerMode::Fail)
176        {
177            return "fail";
178        }
179        else if(this->mode_ == QuestListenerMode::Complete)
180        {
181            return "complete";
182        }
183        else
184        {
185            COUT(1) << "An unforseen, never to happen, Error has occurred. This is impossible!" << std::endl;
186            return "";
187        }
188    }
189
190    /**
191    @brief
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    */
196    const std::string & QuestListener::getQuestId(void)
197    {
198        return this->quest_->getId();
199    }
200
201    /**
202    @brief
203        Executes the QuestListener, resp. fires an Event.
204    @return
205        Returns true if successful.
206    */
207    bool QuestListener::execute()
208    {
209        this->fireEvent(true);
210        return true;
211    }
212
213}
Note: See TracBrowser for help on using the repository browser.