Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Started documenting MetaPcikup, resolved some bugs in PickupCollection and PickupCollectionIdentifier.

  • Property svn:eol-style set to native
File size: 5.8 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
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"
40#include "DroppedPickup.h"
41
42#include "PickupCollectionIdentifier.h"
43
44namespace orxonox
45{
46 
47    CreateFactory(PickupCollection);
48
49    /**
50    @brief
51        Default Constructor.
52    */
53    PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator)
54    {
55        RegisterObject(PickupCollection);
56       
57        this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(this);
58    }
59   
60    /**
61    @brief
62        Destructor.
63    */
64    PickupCollection::~PickupCollection()
65    {
66        //! Destroy all Pickupables constructing this PickupCollection.
67        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
68        {
69            (*it)->destroy();
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       
81        XMLPortObject(PickupCollection, Pickupable, "pickupables", addPickupable, getPickupable, xmlelement, mode);
82       
83        this->initializeIdentifier();
84    }
85   
86    void PickupCollection::initializeIdentifier(void)
87    {
88        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
89        {
90            this->pickupCollectionIdentifier_->addPickup((*it)->getPickupIdentifier());
91        }
92    }
93   
94    /**
95    @brief
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    {
106        new DroppedPickup(this, this, position);
107        return true;
108    }
109   
110    /**
111    @brief
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       
123        this->pickups_.push_back(pickup);
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    */
135    const Pickupable* PickupCollection::getPickupable(unsigned int index)
136    {
137        return this->pickups_[index]; //TODO. Does this work?
138    }
139   
140    //TODO: Steal description from Pickupable.
141    void PickupCollection::changedUsed(void)
142    {
143        SUPER(PickupCollection, changedUsed);
144       
145        //! Change used for all Pickupables this PickupCollection consists of.
146        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
147        {
148            (*it)->setUsed(this->isUsed());
149        }
150    }
151   
152    void PickupCollection::changedCarrier()
153    {
154        SUPER(PickupCollection, changedCarrier);
155       
156        //! Change the carrier for all Pickupables this PickupCollection consists of.
157        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
158        {
159            (*it)->setCarrier(this->getCarrier());
160        }
161    }
162   
163    //TODO: Steal description from Pickupable.
164    void PickupCollection::clone(OrxonoxClass*& item)
165    {
166        if(item == NULL)
167            item = new PickupCollection(this);
168       
169        SUPER(PickupCollection, clone, item);
170       
171        PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);
172        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
173        {
174            Pickupable* newPickup = (*it)->clone();
175            pickup->addPickupable(newPickup);
176        }
177
178        pickup->initializeIdentifier();
179    }
180   
181    bool PickupCollection::isTarget(Identifier* identifier) const
182    {
183        for(std::vector<Pickupable*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
184        {
185            if(!(*it)->isTarget(identifier))
186                return false;
187        }
188       
189        return true;
190    }
191   
192    const PickupIdentifier* PickupCollection::getPickupIdentifier(void)
193    {
194        return this->pickupCollectionIdentifier_;
195    }
196   
197}
Note: See TracBrowser for help on using the repository browser.