Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem5/src/orxonox/objects/quest/QuestEffectBeacon.cc @ 2961

Last change on this file since 2961 was 2779, checked in by dafrick, 16 years ago

Updated questsystem to current trunk. Synchronized it with my local state of things. The Notifications are not yet working, though.

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