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, 15 years ago

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

  • Property svn:eol-style set to native
File size: 5.8 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    //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.
[6421]146        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]147        {
[6466]148            (*it)->setUsed(this->isUsed());
[6405]149        }
150    }
151   
[6466]152    void PickupCollection::changedCarrier()
[6405]153    {
[6466]154        SUPER(PickupCollection, changedCarrier);
[6405]155       
[6466]156        //! Change the carrier for all Pickupables this PickupCollection consists of.
[6421]157        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]158        {
[6466]159            (*it)->setCarrier(this->getCarrier());
[6405]160        }
161    }
162   
[6466]163    //TODO: Steal description from Pickupable.
[6497]164    void PickupCollection::clone(OrxonoxClass*& item)
[6405]165    {
[6466]166        if(item == NULL)
167            item = new PickupCollection(this);
[6405]168       
[6466]169        SUPER(PickupCollection, clone, item);
[6405]170       
[6466]171        PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);
[6421]172        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
[6405]173        {
174            Pickupable* newPickup = (*it)->clone();
[6466]175            pickup->addPickupable(newPickup);
[6405]176        }
[6466]177
178        pickup->initializeIdentifier();
[6405]179    }
180   
[6519]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   
[6475]192    const PickupIdentifier* PickupCollection::getPickupIdentifier(void)
193    {
194        return this->pickupCollectionIdentifier_;
195    }
196   
[6405]197}
Note: See TracBrowser for help on using the repository browser.