Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/quest/QuestListener.cc @ 2385

Last change on this file since 2385 was 2385, checked in by dafrick, 15 years ago

Merged questsystem3.

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