Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/PickupCollection.cc @ 6533

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

Resolved segmentation fault, when destroying a PickupCompilation.

  • Property svn:eol-style set to native
File size: 6.3 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<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
68        {
69            if((*it).get() != NULL)
70                (*it).get()->destroy();
71        }
72    }
73   
74    /**
75    @brief
76        Creates an instance of this Class through XML.
77    */
78    void PickupCollection::XMLPort(Element& xmlelement, XMLPort::Mode mode)
79    {
80        SUPER(PickupCollection, XMLPort, xmlelement, mode);
81       
82        XMLPortObject(PickupCollection, Pickupable, "pickupables", addPickupable, getPickupable, xmlelement, mode);
83       
84        this->initializeIdentifier();
85    }
86   
87    void PickupCollection::initializeIdentifier(void)
88    {
89        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
90        {
91            this->pickupCollectionIdentifier_->addPickup((*it).get()->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        new 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        WeakPtr<Pickupable> ptr = pickup;
125        this->pickups_.push_back(ptr);
126        return true;
127    }
128   
129    /**
130    @brief
131        Get the Pickupable at the given index.
132    @param index
133        The index the Pickupable is fetched from.
134    @return
135        Returns a pointer to the Pickupable at the index given by index.
136    */
137    const Pickupable* PickupCollection::getPickupable(unsigned int index)
138    {
139        return this->pickups_[index].get(); //TODO. Does this work?
140    }
141   
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<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
148        {
149            (*it).get()->setUsed(this->isUsed());
150        }
151    }
152   
153    void PickupCollection::changedCarrier(void)
154    {
155        SUPER(PickupCollection, changedCarrier);
156       
157        //! Change used for all Pickupables this PickupCollection consists of.
158        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
159        {
160            (*it).get()->setCarrier(this->getCarrier());
161        }
162    }
163   
164    void PickupCollection::changedPickedUp()
165    {
166        SUPER(PickupCollection, changedPickedUp);
167       
168        //! Change the carrier for all Pickupables this PickupCollection consists of.
169        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
170        {
171            (*it).get()->setPickedUp(this->isPickedUp());
172        }
173    }
174   
175    void PickupCollection::clone(OrxonoxClass*& item)
176    {
177        if(item == NULL)
178            item = new PickupCollection(this);
179       
180        SUPER(PickupCollection, clone, item);
181       
182        PickupCollection* pickup = dynamic_cast<PickupCollection*>(item);
183        for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
184        {
185            Pickupable* newPickup = (*it).get()->clone();
186            pickup->addPickupable(newPickup);
187        }
188
189        pickup->initializeIdentifier();
190    }
191   
192    bool PickupCollection::isTarget(Identifier* identifier) const
193    {
194        for(std::vector<WeakPtr<Pickupable> >::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
195        {
196            if(!(*it).get()->isTarget(identifier))
197                return false;
198        }
199       
200        return true;
201    }
202   
203    const PickupIdentifier* PickupCollection::getPickupIdentifier(void)
204    {
205        return this->pickupCollectionIdentifier_;
206    }
207   
208}
Note: See TracBrowser for help on using the repository browser.