Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/quest/QuestEffectBeacon.cc @ 3302

Last change on this file since 3302 was 3280, checked in by rgrieder, 15 years ago

Merged most of the core4 revisions back to the trunk except for:

  • orxonox_cast
  • all the radical changes in the input library
  • Property svn:eol-style set to native
File size: 7.9 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
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#include "orxonox/objects/worldentities/pawns/Pawn.h"
40#include "orxonox/objects/worldentities/triggers/PlayerTrigger.h"
41#include "QuestEffect.h"
42
43namespace orxonox
44{
45    CreateFactory(QuestEffectBeacon);
46
47    /**
48    @brief
49        Constructor. Registers the object and initializes defaults.
50    */
51    QuestEffectBeacon::QuestEffectBeacon(BaseObject* creator) : StaticEntity(creator)
52    {
53        RegisterObject(QuestEffectBeacon);
54       
55        this->status_ = QuestEffectBeaconStatus::Active;
56        this->times_ = INFINITE_TIME;
57    }
58
59    /**
60        Destructor.
61    */
62    QuestEffectBeacon::~QuestEffectBeacon()
63    {
64    }
65   
66    /**
67    @brief
68        Method for creating a QuestEffectBeacon object through XML.
69    */
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);
75        XMLPortObject(QuestEffectBeacon, QuestEffect, "effects", addEffect, getEffect, xmlelement, mode);
76       
77        COUT(3) << "New QuestEffectBeacon created." << std::endl;
78    }
79   
80    /**
81    @brief
82        Processes an event for this QuestEffectBeacon.
83    */
84    void QuestEffectBeacon::processEvent(Event& event)
85    {
86        SUPER(QuestEffectBeacon, processEvent, event);
87   
88        ORXONOX_SET_SUBCLASS_EVENT(QuestEffectBeacon, "execute", execute, event, PlayerTrigger);
89    }
90   
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    */
100    bool QuestEffectBeacon::execute(bool b, PlayerTrigger* trigger)
101    {
102        if(!b)
103        {
104            return false;
105        }
106        if(!(this->isActive())) //!< If the QuestEffectBeacon is inactive it cannot be executed.
107        {
108            COUT(3) << "The QuestEffectBeacon is inactive." << std::endl;
109            return false;
110        }
111       
112        if(!trigger->isForPlayer()) //!< The PlayerTrigger is not exclusively for ControllableEntities which means we cannot extract one.
113        {
114            return false;
115        }
116
117        //! Extracting the ControllableEntity form the PlayerTrigger.
118        Pawn* pawn = trigger->getTriggeringPlayer();
119
120        if(pawn == NULL)
121        {
122            COUT(2) << "The QuestEffectBeacon was triggered by an entity other than a Pawn." << std::endl;
123            return false;
124        }
125       
126        //! Extract the PlayerInfo from the ControllableEntity.
127        PlayerInfo* player = pawn->getPlayer();
128       
129        if(player == NULL)
130        {
131            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
132            return false;
133        }
134
135        COUT(3) << "QuestEffectBeacon executed on player: " << player << " ." << std::endl;
136
137        bool check = QuestEffect::invokeEffects(player, this->effects_); //!< Invoke the QuestEffects on the PlayerInfo.
138        if(check)
139        {
140            this->decrementTimes(); //!< Decrement the number of times the beacon can be used.
141            return true;
142        }
143
144        return false;
145    }
146   
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)
156    {
157        if(this->getTimes() == 0 && activate) //!< A QuestEffectBeacon that can be executed only 0 times is always inactive.
158        {
159            return false;
160        }
161       
162        if(activate)
163        {
164        this->status_ = QuestEffectBeaconStatus::Active;
165        return true;
166        }
167       
168        this->status_ = QuestEffectBeaconStatus::Inactive;
169        return true;
170    }
171   
172    /**
173    @brief
174        Decrement the number of times the QuestEffectBeacon can be executed.
175    @return
176        Returns true if successful.
177    */
178    bool QuestEffectBeacon::decrementTimes(void)
179    {
180        if(!(this->isActive())) //!< The QuestEffectBeacon mus be active to decrement the number of times it can be executed.
181        {
182            return false;
183        }
184        if(this->getTimes() == INFINITE_TIME) //!< If times is infinity the QuestEffectBeacon can be executed an infinite number fo times.
185        {
186            return true;
187        }
188       
189        this->times_ = this->times_ - 1; //!< Decrement number of times the QuestEffectBeacon can be executed.
190        if(this->getTimes() == 0) //!< Set the QuestEffectBeacon to inactive when the number of times it can be executed is reduced to 0.
191        {
192            this->status_ = QuestEffectBeaconStatus::Inactive;
193        }
194       
195        return true;
196    }
197   
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    */
208    bool QuestEffectBeacon::setTimes(const int & n)
209    {
210        if(n < 0 && n != INFINITE_TIME)
211        {
212            return false;
213        }
214       
215        this->times_ = n;
216        return true;
217    }
218   
219    /**
220    @brief
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.
226    */
227    bool QuestEffectBeacon::addEffect(QuestEffect* effect)
228    {
229        if(effect == NULL) //!< NULL-pointers are not well liked here...
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
237        COUT(3) << "A QuestEffect was added to a QuestEffectBeacon." << std::endl;
238        return true;
239    }
240   
241    /**
242    @brief
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.
248    */
249    const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const
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.