Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5910 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

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