Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Resolved some issues, should be complete now.
Done some testing, no obvious errors uncovered.

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        COUT(3) << "New QuestEffectBeacon created." << std::endl;
83    }
84   
85    /**
86    @brief
87        Processes an event for this QuestEffectBeacon.
88    */
89    void QuestEffectBeacon::processEvent(Event& event)
90    {
91        SUPER(QuestEffectBeacon, processEvent, event);
92   
93        SetSubclassEvent(QuestEffectBeacon, "execute", execute, event, PlayerTrigger);
94    }
95   
96    /**
97    @brief
98        Executes the QuestEffectBeacon.
99        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.
100    @param b
101        TDO: What is this???
102    @param trigger
103        Apointer to the PlayerTrigger that threw the Event.
104    @return
105        Returns true if successfully executed, false if not.
106    */
107    bool QuestEffectBeacon::execute(bool b, PlayerTrigger* trigger)
108    {
109        if(!b)
110        {
111            return false;
112        }
113        if(!(this->isActive())) //!< If the QuestEffectBeacon is inactive it cannot be executed.
114        {
115            COUT(3) << "The QuestEffectBeacon is inactive." << std::endl;
116            return false;
117        }
118       
119        if(!trigger->isForPlayer()) //!< The PlayerTrigger is not exclusively for ControllableEntities which means we cannot extract one.
120        {
121            return false;
122        }
123
124        //! Extracting the ControllableEntity form the PlayerTrigger.
125        ControllableEntity* entity = trigger->getTriggeringPlayer();
126
127        if(entity == NULL)
128        {
129            COUT(2) << "The QuestEffectBeacon was triggered by an entity other than a ControllableEntity." << std::endl;
130            return false;
131        }
132       
133        //! Extract the PlayerInfo from the ControllableEntity.
134        PlayerInfo* player = entity->getPlayer();
135       
136        if(player == NULL)
137        {
138            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
139            return false;
140        }
141
142        COUT(3) << "QuestEffectBeacon executed on player: " << player << " ." << std::endl;
143
144        bool check = QuestEffect::invokeEffects(player, this->effects_); //!< Invoke the QuestEffects on the PlayerInfo.
145        if(check)
146        {
147            this->decrementTimes(); //!< Decrement the number of times the beacon can be used.
148            return true;
149        }
150
151        return false;
152    }
153   
154    /**
155    @brief
156        Set the status of the QuestEffectBeacon.
157    @param activate
158        If true the QuestEffectBeacon is activated, if false it is deactivated.
159    @return
160        Returns whether the activation/deactivation was successful.
161    */
162    bool QuestEffectBeacon::setActive(bool activate)
163    {
164        if(this->getTimes() == 0 && activate) //!< A QuestEffectBeacon that can be executed only 0 times is always inactive.
165        {
166            return false;
167        }
168       
169        if(activate)
170        {
171            this->status_ = QuestEffectBeaconStatus::active;
172            return true;
173        }
174       
175        this->status_ = QuestEffectBeaconStatus::inactive;
176        return true;
177    }
178   
179    /**
180    @brief
181        Decrement the number of times the QuestEffectBeacon can be executed.
182    @return
183        Returns true if successful.
184    */
185    bool QuestEffectBeacon::decrementTimes(void)
186    {
187        if(!(this->isActive())) //!< The QuestEffectBeacon mus be active to decrement the number of times it can be executed.
188        {
189            return false;
190        }
191        if(this->getTimes() == INFINITE) //!< If times is infinity the QuestEffectBeacon can be executed an infinite number fo times.
192        {
193            return true;
194        }
195       
196        this->times_ = this->times_ - 1; //!< Decrement number of times the QuestEffectBeacon can be executed.
197        if(this->getTimes() == 0) //!< Set the QuestEffectBeacon to inactive when the number of times it can be executed is reduced to 0.
198        {
199            this->status_ = QuestEffectBeaconStatus::inactive;
200        }
201       
202        return true;
203    }
204   
205    /**
206    @brief
207        Set the number of times the QuestEffectBeacon can be executed.
208        The number must be eighter <= 0, or INFINITY which is '-1'.
209    @param n
210        The number of times the QuestEffectBeacon can be executed.
211        The number must be eighter <= 0, or INFINITY which is '-1'.
212    @return
213        Returns true if successful.
214    */
215    bool QuestEffectBeacon::setTimes(const int & n)
216    {
217        if(n < 0 && n != INFINITE)
218        {
219            return false;
220        }
221       
222        this->times_ = n;
223        return true;
224    }
225   
226    /**
227    @brief
228        Adds a QuestEffect to the QuestEffectBeacon.
229    @param effect
230        A pointer to the QuestEffect to be added.
231    @return
232        Returns true if successful.
233    */
234    bool QuestEffectBeacon::addEffect(QuestEffect* effect)
235    {
236        if(effect == NULL) //!< NULL-pointers are not well liked here...
237        {
238            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
239            return false;
240        }
241
242        this->effects_.push_back(effect);
243
244        COUT(3) << "A QuestEffect was added to a QuestEffectBeacon." << std::endl;
245        return true;
246    }
247   
248    /**
249    @brief
250        Returns the QuestEffect at the given index.
251    @param index
252        The index.
253    @return
254        Returns a pointer to the QuestEffect at the given index.
255    */
256    const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const
257    {
258        int i = index;
259        for (std::list<QuestEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
260        {
261            if(i == 0)
262            {
263               return *effect;
264            }
265            i--;
266        }
267        return NULL;
268    }
269
270}
Note: See TracBrowser for help on using the repository browser.