Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Some Documenting and bug fixes.

  • Property svn:eol-style set to native
File size: 6.5 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
41namespace orxonox
42{
43 
44    /**
45    @brief
46        Default Constructor.
47    */
48    PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator)
49    {
50        RegisterObject(PickupCollection);
51    }
52   
53    /**
54    @brief
55        Destructor.
56    */
57    PickupCollection::~PickupCollection()
58    {
59        //! Destroy all Pickupables constructing this PickupCollection.
60        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
61        {
62            delete *it;
63        }
64    }
65   
66    /**
67    @brief
68        Creates an instance of this Class through XML.
69    */
70    void PickupCollection::XMLPort(Element& xmlelement, XMLPort::Mode mode)
71    {
72        SUPER(PickupCollection, XMLPort, xmlelement, mode);
73       
74        //TODO: Does this work? Problem could be, that Pickupable itself cannot be instantiated through XML...
75        XMLPortObject(PickupCollection, PickupCollection, "pickupables", addPickupable, getPickupable, xmlelement, mode);
76    }
77   
78    /**
79    @brief
80        Add the input Pickupable to list of Pickupables combined by this PickupCollection.
81    @param pickup
82        The Pickupable to be added.
83    @return
84        Returns true if successful,
85    */
86    bool PickupCollection::addPickupable(Pickupable* pickup)
87    {
88        if(pickup == NULL)
89            return false;
90       
91        this->pickups_.push_back(pickup);
92        return true;
93    }
94   
95    /**
96    @brief
97        Get the Pickupable at the given index.
98    @param index
99        The index the Pickupable is fetched from.
100    @return
101        Returns a pointer to the Pickupable at the index given by index.
102    */
103    const Pickupable* PickupCollection::getPickupable(unsigned int index)
104    {
105        return this->pickups_[index]; //TODO. Does this work?
106    }
107   
108    //TODO: Steal description from Pickupable.
109    void PickupCollection::changedUsed(void)
110    {
111        SUPER(PickupCollection, changedUsed);
112       
113        //! Change used for all Pickupables this PickupCollection consists of.
114        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
115        {
116            (*it)->changedUsed();
117        }
118    }
119   
120    /**
121    @brief
122        Puts the PickupCollection in use.
123    @return
124        Returns true if successful.
125    */
126    //TODO: Revert if one fails? (same for unused)
127    bool PickupCollection::use(void)
128    {
129        if(this->isUsed())
130            return false;
131       
132        bool success = true;
133        //! Set all Pickupables to used.
134        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
135        {
136            if(!(*it)->use())
137            {
138                success = false;
139            }
140        }
141       
142        this->changedUsed();
143       
144        return success;
145    }
146   
147    /**
148    @brief
149        Puts the PickupCollection out of use.
150    @return
151        Returns true if successful.
152    */
153    bool PickupCollection::unuse(void)
154    {
155        if(!this->isUsed())
156            return false;
157       
158        bool success = true;
159        //! Set all Pickupables to unused.
160        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
161        {
162            if(!(*it)->unuse())
163            {
164                success = false;
165            }
166        }
167       
168        this->changedUsed();
169       
170        return success;
171    }
172
173    /**
174    @brief
175        Is invoked when the pickup is picked up.
176    @param carrier
177        The PickupCarrier that is picking up this pickup.
178    @return
179        Returns true if successful.
180    */
181    //TODO: Something should happen in the carrier as well, maybe just in the carrier. Owner might not be correct if the carrier hands the pickup down or up. Maybe even a Pawn as input instead fo a carrier. Or do this in Spawner?
182    bool PickupCollection::pickup(PickupCarrier* carrier)
183    {
184        if(this->getOwner() != NULL)
185        {
186            COUT(2) << "Pickup wanted to get picked up by a new carrier, but it already has a carrier." << std::endl;
187            return false;
188        }
189        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
190        {
191            (*it)->setOwner(carrier);
192        }
193       
194        this->setOwner(carrier);
195       
196        return true;
197    }
198   
199    /**
200    @brief
201        Drop the pickup.
202    @return
203        Return true if successful.
204    */
205    bool PickupCollection::drop(void)
206    {
207        this->unuse();
208        this->setOwner(NULL);
209       
210        //TODO: Create new Pickupspawner/DroppedPickup
211        return true;
212    }
213   
214    //TODO: Steal description from Pickupable.
215    Pickupable* PickupCollection::clone()
216    {
217        Template* collectionTemplate = Template::getTemplate(this->getIdentifier()->getName());
218        BaseObject* newObject = collectionTemplate->getBaseclassIdentifier()->fabricate(this);
219       
220        PickupCollection* newCollection = dynamic_cast<PickupCollection*>(newObject);
221        for(std::vector<Pickupable*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++)
222        {
223            Pickupable* newPickup = (*it)->clone();
224            newCollection->pickups_.push_back(newPickup);
225        }
226       
227        Pickupable* pickup = dynamic_cast<Pickupable*>(newCollection);
228        return pickup;
229    }
230   
231}
Note: See TracBrowser for help on using the repository browser.