Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/QuestEffectBeacon.cc @ 6587

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

Some more updated documentation, this time in QuestEffectBeacon.

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