Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/chat/src/modules/pickup/PickupCollection.cc @ 6632

Last change on this file since 6632 was 6524, checked in by dafrick, 15 years ago

Merged pickup branch into trunk. Yay. Persisting bugs will be fixed, very soon.

  • Property svn:eol-style set to native
File size: 6.1 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    void PickupCollection::changedUsed(void)
141    {
142        SUPER(PickupCollection, changedUsed);
143       
144        //! Change used for all Pickupables this PickupCollection consists of.
145        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
146        {
147            (*it)->setUsed(this->isUsed());
148        }
149    }
150   
151    void PickupCollection::changedCarrier(void)
152    {
153        SUPER(PickupCollection, changedCarrier);
154       
155        //! Change used for all Pickupables this PickupCollection consists of.
156        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
157        {
158            (*it)->setCarrier(this->getCarrier());
159        }
160    }
161   
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   
173    void PickupCollection::clone(OrxonoxClass*& item)
174    {
175        if(item == NULL)
176            item = new PickupCollection(this);
177       
178        SUPER(PickupCollection, clone, item);
179       
180        PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);
181        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
182        {
183            Pickupable* newPickup = (*it)->clone();
184            pickup->addPickupable(newPickup);
185        }
186
187        pickup->initializeIdentifier();
188    }
189   
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   
201    const PickupIdentifier* PickupCollection::getPickupIdentifier(void)
202    {
203        return this->pickupCollectionIdentifier_;
204    }
205   
206}
Note: See TracBrowser for help on using the repository browser.