Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/quest/QuestListener.cc @ 3149

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

Extracted OrxAssert from Exception.h to OrxAssert.h since it doesn't really have anything to do with exceptions.

  • 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
39#include "Quest.h"
40#include "QuestManager.h"
41
42namespace orxonox
43{
44    CreateFactory(QuestListener);
45
46    /**
47    @brief
48        Constructor. Registers the object and initializes variables.
49    */
50    QuestListener::QuestListener(BaseObject* creator) : BaseObject(creator)
51    {
52        RegisterObject(QuestListener);
53       
54        this->mode_ = questListenerMode::all;
55        this->quest_ = NULL;
56    }
57   
58    /**
59    @brief
60        Destructor.
61    */
62    QuestListener::~QuestListener()
63    {
64    }
65   
66    /**
67    @brief
68        Method for creating a Quest object through XML.
69    */
70    void QuestListener::XMLPort(Element& xmlelement, XMLPort::Mode mode)
71    {
72        SUPER(QuestListener, XMLPort, xmlelement, mode);
73
74        XMLPortParam(QuestListener, "questId", setQuestId, getQuestId, xmlelement, mode);
75        XMLPortParam(QuestListener, "mode", setMode, getMode, xmlelement, mode);
76
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 occured 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    const 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 occured. This is impossible!" << std::endl;
186        return "";
187        }
188    }
189
190    /**
191    @brief
192        Executes the QuestListener, resp. fires an Event.
193    @return
194        Returns true if successful.
195    */
196    bool QuestListener::execute()
197    {
198        this->fireEvent(true);
199        return true;
200    }
201
202}
Note: See TracBrowser for help on using the repository browser.