Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10038 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

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