Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixed PickupCollection. It works now the way it was intended and no restructuring of the pickupsystem was necessary.

  • Property svn:eol-style set to native
File size: 8.3 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"
36#include "interfaces/PickupCarrier.h"
[6475]37#include "DroppedPickup.h"
38#include "PickupCollectionIdentifier.h"
39
[6538]40#include "PickupCollection.h"
41
[6405]42namespace orxonox
43{
44 
[6519]45    CreateFactory(PickupCollection);
46
[6405]47    /**
48    @brief
49        Default Constructor.
50    */
51    PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator)
52    {
53        RegisterObject(PickupCollection);
[6475]54       
[6480]55        this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(this);
[6405]56    }
57   
58    /**
59    @brief
[6538]60        Destructor. Iterates through all Pickupables this PickupCollection consists of and destroys them if they haven't been already.
[6405]61    */
62    PickupCollection::~PickupCollection()
63    {
64        //! Destroy all Pickupables constructing this PickupCollection.
[6533]65        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]66        {
[6533]67            if((*it).get() != NULL)
68                (*it).get()->destroy();
[6405]69        }
70    }
71   
72    /**
73    @brief
74        Creates an instance of this Class through XML.
75    */
76    void PickupCollection::XMLPort(Element& xmlelement, XMLPort::Mode mode)
77    {
78        SUPER(PickupCollection, XMLPort, xmlelement, mode);
79       
[6519]80        XMLPortObject(PickupCollection, Pickupable, "pickupables", addPickupable, getPickupable, xmlelement, mode);
[6466]81       
82        this->initializeIdentifier();
[6405]83    }
84   
[6538]85    /**
86    @brief
87        Initializes the PickupIdentifier for this pickup.
88    */
[6466]89    void PickupCollection::initializeIdentifier(void)
90    {
[6533]91        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6466]92        {
[6533]93            this->pickupCollectionIdentifier_->addPickup((*it).get()->getPickupIdentifier());
[6466]94        }
95    }
96   
[6405]97    /**
98    @brief
[6538]99        Is called when the pickup has transited from used to unused or the other way around.
100        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedUsed); to their changdeUsed method.
[6475]101    */
[6405]102    void PickupCollection::changedUsed(void)
103    {
104        SUPER(PickupCollection, changedUsed);
105       
106        //! Change used for all Pickupables this PickupCollection consists of.
[6533]107        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]108        {
[6533]109            (*it).get()->setUsed(this->isUsed());
[6405]110        }
111    }
112   
[6538]113    /**
114    @brief
115        Is called when the pickup has changed its PickupCarrier.
116        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedCarrier); to their changedCarrier method.
117    */
[6521]118    void PickupCollection::changedCarrier(void)
[6405]119    {
[6466]120        SUPER(PickupCollection, changedCarrier);
[6405]121       
[6538]122        //! Change the PickupCarrier for all Pickupables this PickupCollection consists of.
[6533]123        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]124        {
[6901]125            (*it).get()->setCarrier(this->getCarrier()->getTarget(*it));
[6405]126        }
127    }
128   
[6538]129    /**
130    @brief
131        Is called when the pickup has transited from picked up to dropped or the other way around.
132        Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedPickedUp); to their changedPickedUp method.
133    */
[6521]134    void PickupCollection::changedPickedUp()
135    {
136        SUPER(PickupCollection, changedPickedUp);
137       
[6538]138        //! Change the pickedUp status for all Pickupables this PickupCollection consists of.
[6533]139        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6521]140        {
[6533]141            (*it).get()->setPickedUp(this->isPickedUp());
[6521]142        }
143    }
144   
[6538]145    /**
146    @brief
147        Creates a duplicate of the input OrxonoxClass.
148        This method needs to be implemented by any Class inheriting from Pickupable.
149    @param item
150        A reference to a pointer to the OrxonoxClass that is to be duplicated.
151    */
[6497]152    void PickupCollection::clone(OrxonoxClass*& item)
[6405]153    {
[6466]154        if(item == NULL)
155            item = new PickupCollection(this);
[6405]156       
[6466]157        SUPER(PickupCollection, clone, item);
[6405]158       
[6466]159        PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);
[6540]160        //! Clone all Pickupables this PickupCollection consist of.
[6533]161        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]162        {
[6533]163            Pickupable* newPickup = (*it).get()->clone();
[6466]164            pickup->addPickupable(newPickup);
[6405]165        }
[6466]166
167        pickup->initializeIdentifier();
[6405]168    }
169   
[6538]170    /**
171    @brief
172        Get whether a given class, represented by the input Identifier, is a target of this PickupCollection.
173    @param identifier
174        A pointer to the PickupIdentifier of the PickupCarrier we want to know of, whether it is a target of this PickupCollection.
175    @return
176        Returns true if the PickupCarrier identified by the input PickupIdentififer it is a target of this PickupCollection, false if not.
177    */
[6731]178    bool PickupCollection::isTarget(PickupCarrier* carrier) const
[6519]179    {
[6533]180        for(std::vector<WeakPtr<Pickupable> >::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6519]181        {
[6731]182            if(!carrier->isTarget((*it).get()))
[6519]183                return false;
184        }
185       
186        return true;
187    }
188   
[6538]189    /**
190    @brief
191        Get the PickupIdentifier of this PickupCollection.
192        This is in fact the PickupCollectionIdentifier.
193    @return
194        Returns a pointer to the PickupIdentifier of this PickupCollection.
195    */
[6475]196    const PickupIdentifier* PickupCollection::getPickupIdentifier(void)
197    {
198        return this->pickupCollectionIdentifier_;
199    }
200   
[6538]201    /**
202    @brief
203        Add the input Pickupable to list of Pickupables combined by this PickupCollection.
204    @param pickup
205        The Pickupable to be added.
206    @return
207        Returns true if successful,
208    */
209    bool PickupCollection::addPickupable(Pickupable* pickup)
210    {
211        if(pickup == NULL)
212            return false;
213       
214        WeakPtr<Pickupable> ptr = pickup; //!< Create a weak pointer to be able to test in the constructor if the Pointer is still valid.
215        this->pickups_.push_back(ptr);
216        return true;
217    }
218   
219    /**
220    @brief
221        Get the Pickupable at the given index.
222    @param index
223        The index the Pickupable is fetched from.
224    @return
225        Returns a pointer to the Pickupable at the index given by index.
226    */
227    const Pickupable* PickupCollection::getPickupable(unsigned int index)
228    {
229        return this->pickups_[index].get();
230    }
231       
232    /**
233    @brief
234        Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
235        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.:
236        DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position);
237    @param position
238        The position at which the PickupSpawner should be placed.
239    @return
240        Returns true if a spawner was created, false if not.
241    */
[6540]242    bool PickupCollection::createSpawner(void)
[6538]243    {
[6540]244        new DroppedPickup(this, this, this->getCarrier());
[6538]245        return true;
246    }
247   
[6405]248}
Note: See TracBrowser for help on using the repository browser.