Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2501 was 2251, checked in by dafrick, 16 years ago

Some cleanup…

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