Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/PickupCollection.cc @ 11103

Last change on this file since 11103 was 11099, checked in by muemart, 9 years ago

Fix loads of doxygen warnings and other documentation issues

  • Property svn:eol-style set to native
File size: 10.2 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 PickupCollection.cc
31    @brief Implementation of PickupCollection.
32*/
33
34#include "core/CoreIncludes.h"
35#include "core/XMLPort.h"
36
37#include "interfaces/PickupCarrier.h"
38
39#include "CollectiblePickup.h"
40#include "PickupSpawner.h"
41
42#include "PickupCollection.h"
43
44namespace orxonox
45{
46
47    RegisterClass(PickupCollection);
48
49    /**
50    @brief
51        Default Constructor.
52    */
53    PickupCollection::PickupCollection(Context* context) : BaseObject(context)
54    {
55        RegisterObject(PickupCollection);
56
57        this->processingUsed_ = false;
58        this->processingPickedUp_ = false;
59    }
60
61    /**
62    @brief
63        Destructor. Iterates through all Pickupables this PickupCollection consists of and destroys them if they haven't been already.
64    */
65    PickupCollection::~PickupCollection()
66    {
67        // Destroy all Pickupables constructing this PickupCollection.
68        for(CollectiblePickup* pickup : this->pickups_)
69        {
70            pickup->wasRemovedFromCollection();
71            pickup->destroy();
72        }
73        this->pickups_.clear();
74    }
75
76    /**
77    @brief
78        Creates an instance of this Class through XML.
79    */
80    void PickupCollection::XMLPort(Element& xmlelement, XMLPort::Mode mode)
81    {
82        SUPER(PickupCollection, XMLPort, xmlelement, mode);
83
84        XMLPortParam(PickupCollection, "representation", setRepresentationName, getRepresentationName, xmlelement, mode);
85        XMLPortObject(PickupCollection, CollectiblePickup, "pickupables", addPickupable, getPickupable, xmlelement, mode);
86    }
87
88    /**
89    @brief
90        Is called when the pickup has transited from used to unused or the other way around.
91        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedUsed); to their changdeUsed method.
92    */
93    void PickupCollection::changedUsed(void)
94    {
95        SUPER(PickupCollection, changedUsed);
96
97        this->processingUsed_ = true;
98        // Change used for all Pickupables this PickupCollection consists of.
99        for(CollectiblePickup* pickup : this->pickups_)
100            pickup->setUsed(this->isUsed());
101
102        this->processingUsed_ = false;
103
104        this->changedUsedAction();
105    }
106
107    /**
108    @brief
109        Helper method.
110        Checks whether due to changes in the used status of the pickups of this PickupCollection the used status of this PickupCollection has to change as well.
111    */
112    void PickupCollection::changedUsedAction(void)
113    {
114        if(this->processingUsed_)
115            return;
116
117        size_t numPickupsEnabled = 0;
118        size_t numPickupsInUse = 0;
119        for(CollectiblePickup* pickup : this->pickups_)
120        {
121            if (pickup->isEnabled())
122                ++numPickupsEnabled;
123            if (pickup->isUsed())
124                ++numPickupsInUse;
125        }
126
127        // If all the pickups are not in use but the PickupCollection is.
128        if(numPickupsInUse == 0 && this->isUsed())
129            this->setUsed(false);
130
131        // If all the enabled pickups are in use but the PickupCollection is not.
132        if(numPickupsInUse > 0 && numPickupsInUse == numPickupsEnabled && !this->isUsed())
133            this->setUsed(true);
134    }
135
136    /**
137    @brief
138        Is called when the pickup has changed its PickupCarrier.
139        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedCarrier); to their changedCarrier method.
140    */
141    void PickupCollection::changedCarrier(void)
142    {
143        SUPER(PickupCollection, changedCarrier);
144
145        // Change the PickupCarrier for all Pickupables this PickupCollection consists of.
146        for(CollectiblePickup* pickup : this->pickups_)
147        {
148            if(this->getCarrier() == nullptr)
149                pickup->setCarrier(nullptr);
150            else
151                pickup->setCarrier(this->getCarrier()->getTarget(pickup));
152        }
153    }
154
155    /**
156    @brief
157        Is called when the pickup has transited from picked up to dropped or the other way around.
158        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedPickedUp); to their changedPickedUp method.
159    */
160    void PickupCollection::changedPickedUp()
161    {
162        SUPER(PickupCollection, changedPickedUp);
163
164        this->processingPickedUp_ = true;
165        // Change the pickedUp status for all Pickupables this PickupCollection consists of.
166        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); )
167            (*(it++))->setPickedUp(this->isPickedUp());
168
169        this->processingPickedUp_ = false;
170
171        this->changedPickedUpAction();
172    }
173
174    /**
175    @brief
176        Helper method.
177        Checks whether due to changes in the picked up status of the pickups of this PickupCollection the picked up status of this PickupCollection has to change as well.
178    */
179    void PickupCollection::changedPickedUpAction(void)
180    {
181        if(this->processingPickedUp_)
182            return;
183
184        // If at least all the enabled pickups of this PickupCollection are no longer picked up.
185        bool isOnePickupEnabledAndPickedUp = false;
186        for(CollectiblePickup* pickup : this->pickups_)
187        {
188            if (pickup->isEnabled() && pickup->isPickedUp())
189            {
190                isOnePickupEnabledAndPickedUp = true;
191                break;
192            }
193        }
194        if(!isOnePickupEnabledAndPickedUp && this->isPickedUp())
195            this->Pickupable::destroy();
196    }
197
198    /**
199    @brief
200        Get whether a given class, represented by the input Identifier, is a target of this PickupCollection.
201    @param carrier
202        A pointer to the PickupCarrier we want to know of, whether it is a target of this PickupCollection.
203    @return
204        Returns true if the PickupCarrier identified by the input PickupIdentififer it is a target of this PickupCollection, false if not.
205    */
206    bool PickupCollection::isTarget(const PickupCarrier* carrier) const
207    {
208        for(CollectiblePickup* pickup : this->pickups_)
209        {
210            if(!carrier->isTarget(pickup))
211                return false;
212        }
213
214        return true;
215    }
216
217    /**
218    @brief
219        Add the input Pickupable to list of Pickupables combined by this PickupCollection.
220    @param pickup
221        The Pickupable to be added.
222    @return
223        Returns true if successful,
224    */
225    bool PickupCollection::addPickupable(CollectiblePickup* pickup)
226    {
227        if(pickup == nullptr)
228            return false;
229
230        this->pickups_.push_back(pickup);
231        pickup->wasAddedToCollection(this);
232        this->pickupsChanged();
233        return true;
234    }
235
236    /**
237    @brief
238        Get the Pickupable at the given index.
239    @param index
240        The index the Pickupable is fetched from.
241    @return
242        Returns a pointer to the Pickupable at the index given by index.
243    */
244    const Pickupable* PickupCollection::getPickupable(unsigned int index) const
245    {
246        if(this->pickups_.size() >= index)
247            return nullptr;
248
249        std::list<CollectiblePickup*>::const_iterator it = this->pickups_.begin();
250        std::advance(it, index);
251        return *it;
252    }
253
254    /**
255    @brief
256        Removes the Pickup from the Collection.
257    @param pickup
258        The Pickup to be removed.
259    @return
260        Returns true if the pickup was in the collection.
261    */
262    bool PickupCollection::removePickupable(CollectiblePickup* pickup)
263    {
264        for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it)
265        {
266            if (*it == pickup)
267            {
268                this->pickups_.erase(it);
269                pickup->wasRemovedFromCollection();
270                this->pickupsChanged();
271                return true;
272            }
273        }
274        return false;
275    }
276
277    /**
278    @brief
279        Informs the PickupCollection, that one of its pickups has changed its used status to the input value.
280        This is used internally by the CollectiblePickup class.
281    @param changed
282        The value the used status has changed to.
283    */
284    void PickupCollection::pickupChangedUsed(bool changed)
285    {
286        this->changedUsedAction();
287    }
288
289    /**
290    @brief
291        Informs the PickupCollection, that one of its pickups has changed its picked up status to the input value.
292        This is used internally by the CollectiblePickup class.
293    @param changed
294        The value the picked up status has changed to.
295    */
296    void PickupCollection::pickupChangedPickedUp(bool changed)
297    {
298        this->changedPickedUpAction();
299    }
300
301    /**
302    @brief
303        Informs the PickupCollection, that one of its pickups has been disabled.
304        This is used internally by the CollectiblePickup class.
305    */
306    void PickupCollection::pickupDisabled(void)
307    {
308    }
309
310    /**
311    @brief
312        Helpfer function if the number of pickups in this collection has changed.
313    */
314    void PickupCollection::pickupsChanged(void)
315    {
316        this->changedUsedAction();
317        this->changedPickedUpAction();
318    }
319
320    /**
321    @brief
322        Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
323    @return
324        Returns true if a spawner was created, false if not.
325    */
326    bool PickupCollection::createSpawner(void)
327    {
328        PickupSpawner::createDroppedPickup(this->getContext(), this, this->getCarrier());
329        return true;
330    }
331
332}
Note: See TracBrowser for help on using the repository browser.