Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem2/src/orxonox/objects/quest/QuestEffectBeacon.cc @ 2221

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

Some tweaks and solved some bugs…

File size: 8.0 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 QuestEffectBeacon.cc
31    @brief
32        Implementation of the QuestEffectBeacon class.
33*/
34
35#include "OrxonoxStableHeaders.h"
36#include "QuestEffectBeacon.h"
37
38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40#include "core/Event.h"
41#include "core/EventIncludes.h"
42
43#include "orxonox/objects/infos/PlayerInfo.h"
44#include "orxonox/objects/worldentities/ControllableEntity.h"
45#include "orxonox/objects/worldentities/triggers/PlayerTrigger.h"
46#include "QuestEffect.h"
47
48namespace orxonox {
49
50    CreateFactory(QuestEffectBeacon);
51
52    /**
53    @brief
54        Constructor. Registers the object and initializes defaults.
55    */
56    QuestEffectBeacon::QuestEffectBeacon(BaseObject* creator) : PositionableEntity(creator)
57    {
58        RegisterObject(QuestEffectBeacon);
59       
60        this->status_ = QuestEffectBeaconStatus::active;
61        this->times_ = INFINITE;
62    }
63
64    /**
65        Destructor.
66    */
67    QuestEffectBeacon::~QuestEffectBeacon()
68    {
69    }
70   
71    /**
72    @brief
73        Method for creating a QuestEffectBeacon object through XML.
74    */
75    void QuestEffectBeacon::XMLPort(Element& xmlelement, XMLPort::Mode mode)
76    {
77        SUPER(QuestEffectBeacon, XMLPort, xmlelement, mode);
78
79        XMLPortParam(QuestEffectBeacon, "times", setTimes, getTimes, xmlelement, mode);
80        XMLPortObject(QuestEffectBeacon, QuestEffect, "effects", addEffect, getEffect, xmlelement, mode);
81    }
82   
83    /**
84    @brief
85        Processes an event for this QuestEffectBeacon.
86    */
87    void QuestEffectBeacon::processEvent(Event& event)
88    {
89        SUPER(QuestEffectBeacon, processEvent, event);
90   
91        SetSubclassEvent(QuestEffectBeacon, "execute", execute, event, PlayerTrigger);
92    }
93   
94    /**
95    @brief
96        Executes the QuestEffectBeacon.
97        This means extracting the ControllableEntity from the PlayerTrigger, provided by the Event causing the execution, and the extracting the PlayerInfo from the received ControllableEntity and invoking the QuestEffectbeacon's QuestEffects on the received PlayerInfo.
98    @param b
99        TDO: What is this???
100    @param trigger
101        Apointer to the PlayerTrigger that threw the Event.
102    @return
103        Returns true if successfully executed, false if not.
104    */
105    bool QuestEffectBeacon::execute(bool b, PlayerTrigger* trigger)
106    {
107        if(!b)
108        {
109            //TDO: Better message, please.
110            COUT(2) << "b is false." << std::endl;
111        }
112        if(!(this->isActive())) //!< If the QuestEffectBeacon is inactive it cannot be executed.
113        {
114            COUT(3) << "The QuestEffectBeacon is inactive." << std::endl;
115            return false;
116        }
117       
118        if(!trigger->isForPlayer()) //!< The PlayerTrigger is not exclusively for ControllableEntities which means we cannot extract one.
119        {
120            return false;
121        }
122
123        //! Extracting the ControllableEntity form the PlayerTrigger.
124        ControllableEntity* entity = trigger->getTriggeringPlayer();
125
126        if(entity == NULL)
127        {
128            COUT(2) << "No one triggered the beacon? Curious!" << std::endl;
129            return false;
130        }
131       
132        //! Extract the PlayerInfo from the ControllableEntity.
133        PlayerInfo* player = entity->getPlayer();
134       
135        if(player == NULL)
136        {
137            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
138            return false;
139        }
140
141        COUT(3) << "QuestEffectBeacon executed on player: " << player << " ." << std::endl;
142
143        bool check = QuestEffect::invokeEffects(player, this->effects_); //!< Invoke the QuestEffects on the PlayerInfo.
144        if(check)
145        {
146            this->decrementTimes(); //!< Decrement the number of times the beacon can be used.
147            return true;
148        }
149
150        return false;
151    }
152   
153    /**
154    @brief
155        Set the status of the QuestEffectBeacon.
156    @param activate
157        If true the QuestEffectBeacon is activated, if false it is deactivated.
158    @return
159        Returns whether the activation/deactivation was successful.
160    */
161    bool QuestEffectBeacon::setActive(bool activate)
162    {
163        if(this->getTimes() == 0 && activate) //!< A QuestEffectBeacon that can be executed only 0 times is always inactive.
164        {
165            return false;
166        }
167       
168        if(activate)
169        {
170            this->status_ = QuestEffectBeaconStatus::active;
171            return true;
172        }
173       
174        this->status_ = QuestEffectBeaconStatus::inactive;
175        return true;
176    }
177   
178    /**
179    @brief
180        Decrement the number of times the QuestEffectBeacon can be executed.
181    @return
182        Returns true if successful.
183    */
184    bool QuestEffectBeacon::decrementTimes(void)
185    {
186        if(!(this->isActive())) //!< The QuestEffectBeacon mus be active to decrement the number of times it can be executed.
187        {
188            return false;
189        }
190        if(this->getTimes() == INFINITE) //!< If times is infinity the QuestEffectBeacon can be executed an infinite number fo times.
191        {
192            return true;
193        }
194       
195        this->times_ = this->times_ - 1; //!< Decrement number of times the QuestEffectBeacon can be executed.
196        if(this->getTimes() == 0) //!< Set the QuestEffectBeacon to inactive when the number of times it can be executed is reduced to 0.
197        {
198            this->status_ = QuestEffectBeaconStatus::inactive;
199        }
200       
201        return true;
202    }
203   
204    /**
205    @brief
206        Set the number of times the QuestEffectBeacon can be executed.
207        The number must be eighter <= 0, or INFINITY which is '-1'.
208    @param n
209        The number of times the QuestEffectBeacon can be executed.
210        The number must be eighter <= 0, or INFINITY which is '-1'.
211    @return
212        Returns true if successful.
213    */
214    bool QuestEffectBeacon::setTimes(const int & n)
215    {
216        if(n < 0 && n != INFINITE)
217        {
218            return false;
219        }
220       
221        this->times_ = n;
222        return true;
223    }
224   
225    /**
226    @brief
227        Adds a QuestEffect to the QuestEffectBeacon.
228    @param effect
229        A pointer to the QuestEffect to be added.
230    @return
231        Returns true if successful.
232    */
233    bool QuestEffectBeacon::addEffect(QuestEffect* effect)
234    {
235        if(effect == NULL) //!< NULL-pointers are not well liked here...
236        {
237            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
238            return false;
239        }
240
241        this->effects_.push_back(effect);
242
243        COUT(3) << "A QuestEffect was added to a QuestEffectBeacon." << std::endl;
244        return true;
245    }
246   
247    /**
248    @brief
249        Returns the QuestEffect at the given index.
250    @param index
251        The index.
252    @return
253        Returns a pointer to the QuestEffect at the given index.
254    */
255    const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const
256    {
257        int i = index;
258        for (std::list<QuestEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
259        {
260            if(i == 0)
261            {
262               return *effect;
263            }
264            i--;
265        }
266        return NULL;
267    }
268
269}
Note: See TracBrowser for help on using the repository browser.