Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/quest/QuestEffectBeacon.cc @ 2385

Last change on this file since 2385 was 2385, checked in by dafrick, 15 years ago

Merged questsystem3.

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