Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11240 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • 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/**
[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{
[9667]47    RegisterClass(QuestEffectBeacon);
[2146]48
[2221]49    /**
50    @brief
51        Constructor. Registers the object and initializes defaults.
52    */
[9667]53    QuestEffectBeacon::QuestEffectBeacon(Context* context) : StaticEntity(context)
[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
[8858]81        orxout(verbose, context::quests) << "New QuestEffectBeacon created." << 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        {
[8858]110            orxout(verbose, context::quests) << "The QuestEffectBeacon is inactive." << endl;
[2146]111            return false;
112        }
[5720]113
[6800]114        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
[11071]115        PlayerInfo* player = nullptr;
[7163]116
117        // If the trigger is a PlayerTrigger.
[11071]118        if(pTrigger != nullptr)
[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
[8706]123                player = pTrigger->getTriggeringPlayer();
[2221]124        }
[7552]125        else
126            return false;
[7163]127
[11071]128        if(player == nullptr)
[2146]129        {
[8858]130            orxout(verbose, context::quests) << "The QuestEffectBeacon was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << endl;
[2146]131            return false;
132        }
[5720]133
[8858]134        orxout(verbose, context::quests) << "QuestEffectBeacon executed on player: " << player << " ." << endl;
[2209]135
[8351]136        bool temp = QuestEffect::invokeEffects(player, this->effects_); // Invoke the QuestEffects on the PlayerInfo.
137        if(temp)
[2146]138        {
[7456]139            this->decrementTimes(); // Decrement the number of times the beacon can be used.
[2146]140            return true;
[2662]141        }
[2221]142
[2662]143        return false;
[2146]144    }
[5720]145
[2221]146    /**
147    @brief
148        Set the status of the QuestEffectBeacon.
149    @param activate
150        If true the QuestEffectBeacon is activated, if false it is deactivated.
151    @return
152        Returns whether the activation/deactivation was successful.
153    */
154    bool QuestEffectBeacon::setActive(bool activate)
[2146]155    {
[7456]156        if(this->getTimes() == 0 && activate) // A QuestEffectBeacon that can be executed only 0 times is always inactive.
[2221]157            return false;
[5720]158
[2221]159        if(activate)
160        {
[7456]161            this->status_ = QuestEffectBeaconStatus::Active;
162            return true;
[2221]163        }
[5720]164
[3280]165        this->status_ = QuestEffectBeaconStatus::Inactive;
[2221]166        return true;
[2146]167    }
[5720]168
[2221]169    /**
170    @brief
171        Decrement the number of times the QuestEffectBeacon can be executed.
172    @return
173        Returns true if successful.
174    */
[2146]175    bool QuestEffectBeacon::decrementTimes(void)
176    {
[7456]177        if(!(this->isActive())) // The QuestEffectBeacon mus be active to decrement the number of times it can be executed.
[2146]178            return false;
[7456]179
180        if(this->getTimes() == INFINITE_TIME) // If times is infinity the QuestEffectBeacon can be executed an infinite number fo times.
[2146]181            return true;
[5720]182
[7456]183        this->times_ = this->times_ - 1; // Decrement number of times the QuestEffectBeacon can be executed.
184        if(this->getTimes() == 0) // Set the QuestEffectBeacon to inactive when the number of times it can be executed is reduced to 0.
[3280]185            this->status_ = QuestEffectBeaconStatus::Inactive;
[5720]186
[2146]187        return true;
188    }
[5720]189
[2221]190    /**
191    @brief
192        Set the number of times the QuestEffectBeacon can be executed.
193        The number must be eighter <= 0, or INFINITY which is '-1'.
194    @param n
195        The number of times the QuestEffectBeacon can be executed.
196        The number must be eighter <= 0, or INFINITY which is '-1'.
197    @return
198        Returns true if successful.
199    */
[2146]200    bool QuestEffectBeacon::setTimes(const int & n)
201    {
[2662]202        if(n < 0 && n != INFINITE_TIME)
[2146]203            return false;
[5720]204
[2146]205        this->times_ = n;
206        return true;
207    }
[5720]208
[2146]209    /**
210    @brief
[2221]211        Adds a QuestEffect to the QuestEffectBeacon.
212    @param effect
213        A pointer to the QuestEffect to be added.
214    @return
215        Returns true if successful.
[2146]216    */
217    bool QuestEffectBeacon::addEffect(QuestEffect* effect)
218    {
[7552]219        assert(effect);
[2146]220
221        this->effects_.push_back(effect);
222
[8858]223        orxout(verbose, context::quests) << "A QuestEffect was added to a QuestEffectBeacon." << endl;
[2146]224        return true;
225    }
[5720]226
[2221]227    /**
[2146]228    @brief
[2221]229        Returns the QuestEffect at the given index.
230    @param index
231        The index.
232    @return
233        Returns a pointer to the QuestEffect at the given index.
[2146]234    */
[2191]235    const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const
[2146]236    {
237        int i = index;
[11071]238        for (QuestEffect* effect : this->effects_)
[2146]239        {
240            if(i == 0)
[11071]241               return effect;
[7456]242
[2146]243            i--;
244        }
[11071]245        return nullptr;
[2146]246    }
247
248}
Note: See TracBrowser for help on using the repository browser.