Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Simplification in creation of PickupIdentifier.

  • Property svn:eol-style set to native
File size: 5.6 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(this);
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        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
88        {
89            this->pickupCollectionIdentifier_->addPickup((*it)->getPickupIdentifier());
90        }
91    }
92   
93    /**
94    @brief
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
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       
122        this->pickups_.push_back(pickup);
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    */
134    const Pickupable* PickupCollection::getPickupable(unsigned int index)
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.
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()
152    {
153        SUPER(PickupCollection, changedCarrier);
154       
155        //! Change the carrier 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    //TODO: Steal description from Pickupable.
163    void PickupCollection::clone(OrxonoxClass* item)
164    {
165        if(item == NULL)
166            item = new PickupCollection(this);
167       
168        SUPER(PickupCollection, clone, item);
169       
170        PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);
171        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
172        {
173            Pickupable* newPickup = (*it)->clone();
174            pickup->addPickupable(newPickup);
175        }
176
177        pickup->initializeIdentifier();
178    }
179   
180    const PickupIdentifier* PickupCollection::getPickupIdentifier(void)
181    {
182        return this->pickupCollectionIdentifier_;
183    }
184   
185}
Note: See TracBrowser for help on using the repository browser.