Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup3/src/modules/pickup/PickupCollection.cc @ 6521

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

Added changedPickedUp method to Pickupable to solve a problem in PickupCollection, which, for the most part, works now.

  • Property svn:eol-style set to native
File size: 6.1 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/**
30    @file
31    @brief Implementation of PickupCollection.
32*/
33
34#include "PickupCollection.h"
35
36#include "core/CoreIncludes.h"
37#include "core/Template.h"
38#include "core/XMLPort.h"
39#include "interfaces/PickupCarrier.h"
[6475]40#include "DroppedPickup.h"
[6405]41
[6475]42#include "PickupCollectionIdentifier.h"
43
[6405]44namespace orxonox
45{
46 
[6519]47    CreateFactory(PickupCollection);
48
[6405]49    /**
50    @brief
51        Default Constructor.
52    */
53    PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator)
54    {
55        RegisterObject(PickupCollection);
[6475]56       
[6480]57        this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(this);
[6405]58    }
59   
60    /**
61    @brief
62        Destructor.
63    */
64    PickupCollection::~PickupCollection()
65    {
66        //! Destroy all Pickupables constructing this PickupCollection.
[6421]67        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]68        {
[6475]69            (*it)->destroy();
[6405]70        }
71    }
72   
73    /**
74    @brief
75        Creates an instance of this Class through XML.
76    */
77    void PickupCollection::XMLPort(Element& xmlelement, XMLPort::Mode mode)
78    {
79        SUPER(PickupCollection, XMLPort, xmlelement, mode);
80       
[6519]81        XMLPortObject(PickupCollection, Pickupable, "pickupables", addPickupable, getPickupable, xmlelement, mode);
[6466]82       
83        this->initializeIdentifier();
[6405]84    }
85   
[6466]86    void PickupCollection::initializeIdentifier(void)
87    {
88        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
89        {
[6475]90            this->pickupCollectionIdentifier_->addPickup((*it)->getPickupIdentifier());
[6466]91        }
92    }
93   
[6405]94    /**
95    @brief
[6475]96        Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
97        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.:
98        DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position);
99    @param position
100        The position at which the PickupSpawner should be placed.
101    @return
102        Returns true if a spawner was created, false if not.
103    */
104    bool PickupCollection::createSpawner(const Vector3& position)
105    {
[6519]106        new DroppedPickup(this, this, position);
[6475]107        return true;
108    }
109   
110    /**
111    @brief
[6405]112        Add the input Pickupable to list of Pickupables combined by this PickupCollection.
113    @param pickup
114        The Pickupable to be added.
115    @return
116        Returns true if successful,
117    */
118    bool PickupCollection::addPickupable(Pickupable* pickup)
119    {
120        if(pickup == NULL)
121            return false;
122       
[6421]123        this->pickups_.push_back(pickup);
[6405]124        return true;
125    }
126   
127    /**
128    @brief
129        Get the Pickupable at the given index.
130    @param index
131        The index the Pickupable is fetched from.
132    @return
133        Returns a pointer to the Pickupable at the index given by index.
134    */
[6421]135    const Pickupable* PickupCollection::getPickupable(unsigned int index)
[6405]136    {
137        return this->pickups_[index]; //TODO. Does this work?
138    }
139   
140    void PickupCollection::changedUsed(void)
141    {
142        SUPER(PickupCollection, changedUsed);
143       
144        //! Change used for all Pickupables this PickupCollection consists of.
[6421]145        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]146        {
[6466]147            (*it)->setUsed(this->isUsed());
[6405]148        }
149    }
150   
[6521]151    void PickupCollection::changedCarrier(void)
[6405]152    {
[6466]153        SUPER(PickupCollection, changedCarrier);
[6405]154       
[6521]155        //! Change used for all Pickupables this PickupCollection consists of.
[6421]156        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]157        {
[6466]158            (*it)->setCarrier(this->getCarrier());
[6405]159        }
160    }
161   
[6521]162    void PickupCollection::changedPickedUp()
163    {
164        SUPER(PickupCollection, changedPickedUp);
165       
166        //! Change the carrier for all Pickupables this PickupCollection consists of.
167        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
168        {
169            (*it)->setPickedUp(this->isPickedUp());
170        }
171    }
172   
[6497]173    void PickupCollection::clone(OrxonoxClass*& item)
[6405]174    {
[6466]175        if(item == NULL)
176            item = new PickupCollection(this);
[6405]177       
[6466]178        SUPER(PickupCollection, clone, item);
[6405]179       
[6466]180        PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);
[6421]181        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]182        {
183            Pickupable* newPickup = (*it)->clone();
[6466]184            pickup->addPickupable(newPickup);
[6405]185        }
[6466]186
187        pickup->initializeIdentifier();
[6405]188    }
189   
[6519]190    bool PickupCollection::isTarget(Identifier* identifier) const
191    {
192        for(std::vector<Pickupable*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
193        {
194            if(!(*it)->isTarget(identifier))
195                return false;
196        }
197       
198        return true;
199    }
200   
[6475]201    const PickupIdentifier* PickupCollection::getPickupIdentifier(void)
202    {
203        return this->pickupCollectionIdentifier_;
204    }
205   
[6405]206}
Note: See TracBrowser for help on using the repository browser.