Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/quest/QuestListener.cc @ 3110

Last change on this file since 3110 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

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