Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7613 was 7552, checked in by dafrick, 14 years ago

Resolving some TODOs and doing some additional cleanup. Almost done now…

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