Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8493 was 8305, checked in by dafrick, 14 years ago

Fixing another potential memory leak.

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