Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6477 was 6475, checked in by dafrick, 15 years ago

Additional documentation, code niceifying and potential bug fixing. Also: Renamed DroppedItem to DroppedPickup.

  • Property svn:eol-style set to native
File size: 5.7 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    /**
48    @brief
49        Default Constructor.
50    */
51    PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator)
52    {
53        RegisterObject(PickupCollection);
54       
55        this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier();
56    }
57   
58    /**
59    @brief
60        Destructor.
61    */
62    PickupCollection::~PickupCollection()
63    {
64        //! Destroy all Pickupables constructing this PickupCollection.
65        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
66        {
67            (*it)->destroy();
68        }
69    }
70   
71    /**
72    @brief
73        Creates an instance of this Class through XML.
74    */
75    void PickupCollection::XMLPort(Element& xmlelement, XMLPort::Mode mode)
76    {
77        SUPER(PickupCollection, XMLPort, xmlelement, mode);
78       
79        //TODO: Does this work? Problem could be, that Pickupable itself cannot be instantiated through XML, doubt that, though.
80        XMLPortObject(PickupCollection, PickupCollection, "pickupables", addPickupable, getPickupable, xmlelement, mode);
81       
82        this->initializeIdentifier();
83    }
84   
85    void PickupCollection::initializeIdentifier(void)
86    {
87        this->pickupCollectionIdentifier_->addClass(this->getIdentifier());
88       
89        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
90        {
91            this->pickupCollectionIdentifier_->addPickup((*it)->getPickupIdentifier());
92        }
93    }
94   
95    /**
96    @brief
97        Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
98        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.:
99        DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position);
100    @param position
101        The position at which the PickupSpawner should be placed.
102    @return
103        Returns true if a spawner was created, false if not.
104    */
105    bool PickupCollection::createSpawner(const Vector3& position)
106    {
107        DroppedPickup::DroppedPickup(this, this, position);
108        return true;
109    }
110   
111    /**
112    @brief
113        Add the input Pickupable to list of Pickupables combined by this PickupCollection.
114    @param pickup
115        The Pickupable to be added.
116    @return
117        Returns true if successful,
118    */
119    bool PickupCollection::addPickupable(Pickupable* pickup)
120    {
121        if(pickup == NULL)
122            return false;
123       
124        this->pickups_.push_back(pickup);
125        return true;
126    }
127   
128    /**
129    @brief
130        Get the Pickupable at the given index.
131    @param index
132        The index the Pickupable is fetched from.
133    @return
134        Returns a pointer to the Pickupable at the index given by index.
135    */
136    const Pickupable* PickupCollection::getPickupable(unsigned int index)
137    {
138        return this->pickups_[index]; //TODO. Does this work?
139    }
140   
141    //TODO: Steal description from Pickupable.
142    void PickupCollection::changedUsed(void)
143    {
144        SUPER(PickupCollection, changedUsed);
145       
146        //! Change used for all Pickupables this PickupCollection consists of.
147        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
148        {
149            (*it)->setUsed(this->isUsed());
150        }
151    }
152   
153    void PickupCollection::changedCarrier()
154    {
155        SUPER(PickupCollection, changedCarrier);
156       
157        //! Change the carrier for all Pickupables this PickupCollection consists of.
158        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
159        {
160            (*it)->setCarrier(this->getCarrier());
161        }
162    }
163   
164    //TODO: Steal description from Pickupable.
165    void PickupCollection::clone(OrxonoxClass* item)
166    {
167        if(item == NULL)
168            item = new PickupCollection(this);
169       
170        SUPER(PickupCollection, clone, item);
171       
172        PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);
173        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
174        {
175            Pickupable* newPickup = (*it)->clone();
176            pickup->addPickupable(newPickup);
177        }
178
179        pickup->initializeIdentifier();
180    }
181   
182    const PickupIdentifier* PickupCollection::getPickupIdentifier(void)
183    {
184        return this->pickupCollectionIdentifier_;
185    }
186   
187}
Note: See TracBrowser for help on using the repository browser.