Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/modules/questsystem/QuestEffectBeacon.cc @ 10514

Last change on this file since 10514 was 9638, checked in by landauf, 11 years ago

renamed CreateFactory() as RegisterClass() to be more consistent with the corresponding RegisterObject() macro

  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
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
29/**
30    @file QuestEffectBeacon.cc
31    @brief Implementation of the QuestEffectBeacon class.
32*/
33
34#include "QuestEffectBeacon.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "core/EventIncludes.h"
39
40#include "interfaces/PlayerTrigger.h"
41#include "worldentities/pawns/Pawn.h"
42
43#include "QuestEffect.h"
44
45namespace orxonox
46{
47    RegisterClass(QuestEffectBeacon);
48
49    /**
50    @brief
51        Constructor. Registers the object and initializes defaults.
52    */
53    QuestEffectBeacon::QuestEffectBeacon(Context* context) : StaticEntity(context)
54    {
55        RegisterObject(QuestEffectBeacon);
56
57        this->status_ = QuestEffectBeaconStatus::Active;
58        this->times_ = INFINITE_TIME;
59    }
60
61    /**
62        Destructor.
63    */
64    QuestEffectBeacon::~QuestEffectBeacon()
65    {
66    }
67
68    /**
69    @brief
70        Method for creating a QuestEffectBeacon object through XML.
71    */
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);
77        XMLPortObject(QuestEffectBeacon, QuestEffect, "effects", addEffect, getEffect, xmlelement, mode);
78
79        XMLPortEventSink(QuestEffectBeacon, BaseObject, "execute", execute, xmlelement, mode); //TODO: Change BaseObject to MultiTrigger as soon as MultiTrigger is the base of all triggers.
80
81        orxout(verbose, context::quests) << "New QuestEffectBeacon created." << endl;
82    }
83
84    void QuestEffectBeacon::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
85    {
86        SUPER(QuestEffectBeacon, XMLEventPort, xmlelement, mode);
87
88        XMLPortEventSink(QuestEffectBeacon, BaseObject, "execute", execute, xmlelement, mode);
89    }
90
91    /**
92    @brief
93        Executes the QuestEffectBeacon.
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.
95    @param bTriggered
96        true means the trigger was activated while false means it was deactivated
97    @param trigger
98        A pointer to the PlayerTrigger that threw the Event.
99    @return
100        Returns true if successfully executed, false if not.
101    */
102    bool QuestEffectBeacon::execute(bool bTriggered, BaseObject* trigger)
103    {
104        if(!bTriggered)
105        {
106            return false;
107        }
108        if(!(this->isActive())) // If the QuestEffectBeacon is inactive it cannot be executed.
109        {
110            orxout(verbose, context::quests) << "The QuestEffectBeacon is inactive." << endl;
111            return false;
112        }
113
114        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
115        PlayerInfo* player = NULL;
116
117        // If the trigger is a PlayerTrigger.
118        if(pTrigger != NULL)
119        {
120            if(!pTrigger->isForPlayer())  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
121                return false;
122            else
123                player = pTrigger->getTriggeringPlayer();
124        }
125        else
126            return false;
127
128        if(player == NULL)
129        {
130            orxout(verbose, context::quests) << "The QuestEffectBeacon was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << endl;
131            return false;
132        }
133
134        orxout(verbose, context::quests) << "QuestEffectBeacon executed on player: " << player << " ." << endl;
135
136        bool temp = QuestEffect::invokeEffects(player, this->effects_); // Invoke the QuestEffects on the PlayerInfo.
137        if(temp)
138        {
139            this->decrementTimes(); // Decrement the number of times the beacon can be used.
140            return true;
141        }
142
143        return false;
144    }
145
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)
155    {
156        if(this->getTimes() == 0 && activate) // A QuestEffectBeacon that can be executed only 0 times is always inactive.
157            return false;
158
159        if(activate)
160        {
161            this->status_ = QuestEffectBeaconStatus::Active;
162            return true;
163        }
164
165        this->status_ = QuestEffectBeaconStatus::Inactive;
166        return true;
167    }
168
169    /**
170    @brief
171        Decrement the number of times the QuestEffectBeacon can be executed.
172    @return
173        Returns true if successful.
174    */
175    bool QuestEffectBeacon::decrementTimes(void)
176    {
177        if(!(this->isActive())) // The QuestEffectBeacon mus be active to decrement the number of times it can be executed.
178            return false;
179
180        if(this->getTimes() == INFINITE_TIME) // If times is infinity the QuestEffectBeacon can be executed an infinite number fo times.
181            return true;
182
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.
185            this->status_ = QuestEffectBeaconStatus::Inactive;
186
187        return true;
188    }
189
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    */
200    bool QuestEffectBeacon::setTimes(const int & n)
201    {
202        if(n < 0 && n != INFINITE_TIME)
203            return false;
204
205        this->times_ = n;
206        return true;
207    }
208
209    /**
210    @brief
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.
216    */
217    bool QuestEffectBeacon::addEffect(QuestEffect* effect)
218    {
219        assert(effect);
220
221        this->effects_.push_back(effect);
222
223        orxout(verbose, context::quests) << "A QuestEffect was added to a QuestEffectBeacon." << endl;
224        return true;
225    }
226
227    /**
228    @brief
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.
234    */
235    const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const
236    {
237        int i = index;
238        for (std::list<QuestEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
239        {
240            if(i == 0)
241               return *effect;
242
243            i--;
244        }
245        return NULL;
246    }
247
248}
Note: See TracBrowser for help on using the repository browser.