Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6490 was 6480, checked in by dafrick, 15 years ago

Simplification in creation of PickupIdentifier.

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