Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem2/src/orxonox/objects/quest/QuestEffectBeacon.cc @ 2658

Last change on this file since 2658 was 2251, checked in by dafrick, 16 years ago

Some cleanup…

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