Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Removed old msvc specific support for precompiled header files.

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