Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup3/src/orxonox/interfaces/Pickupable.cc @ 6477

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

Some more documenting. Completed HealthPickup.

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 the Pickupable class.
32*/
33
34#include "Pickupable.h"
35
36#include "core/Identifier.h"
37#include "core/CoreIncludes.h"
38#include "pickup/PickupIdentifier.h"
39#include "PickupCarrier.h"
40
41namespace orxonox
42{
43   
44    /**
45    @brief
46        Constructor. Registers the objects and initializes its member variables.
47    */
48    Pickupable::Pickupable()
49    {
50        RegisterRootObject(Pickupable);
51
52        this->used_ = false;
53        this->pickedUp_ = false;
54        this->carrier_ = NULL;
55       
56        this->pickupIdentifier_ = new PickupIdentifier();
57    }
58   
59    /**
60    @brief
61        Destructor.
62    */
63    Pickupable::~Pickupable()
64    {
65        if(this->isUsed())
66            this->setUsed(false);
67       
68        if(this->isPickedUp())
69        {
70            this->getCarrier()->drop(this, false);
71            this->setCarrier(NULL);
72        }
73    }
74   
75    /**
76    @brief
77        Sets the Pickupable to used or unused, depending on the input.
78    @param used
79        If used is true the Pickupable is set to used, it is set to unused, otherwise.
80    @return
81        Returns true if the used state was changed, false if not.
82    */
83    bool Pickupable::setUsed(bool used)
84    {
85        if(this->used_ == used)
86            return false;
87       
88        COUT(4) << "Pickupable (&" << this << ") set to used " << used << "." << std::endl;
89       
90        this->used_ = used;
91        this->changedUsed();
92        return true;
93    }
94   
95    /**
96    @brief
97        Get whether the given PickupCarrier is a target of this pickup.
98    @param carrier
99        The PickupCarrier of which it has to be determinde whether it is a target of this pickup.
100    @return
101        Returns true if the given PickupCarrier is a target.
102    */
103    bool Pickupable::isTarget(const PickupCarrier* carrier) const
104    {
105        Identifier* identifier = carrier->getIdentifier();
106        //! Iterate through all targets of this Pickupable.
107        for(std::list<Identifier*>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++)
108        {
109            if(identifier->isA(*it))
110                return true;
111        }
112        return false;
113    }
114   
115    /**
116    @brief
117        Add a PickupCarrier as target of this pickup.
118    @param target
119        The PickupCarrier to be added.
120    @return
121        Returns true if the target was added, false if not.
122    */
123    bool Pickupable::addTarget(PickupCarrier* target)
124    {
125        if(this->isTarget(target)) //!< If the input target is already present in the list of targets.
126            return false;
127       
128        COUT(4) << "Target (&" << target << ") added to Pickupable (&" << this << ")." << std::endl;
129        this->targets_.push_back(target->getIdentifier());
130        return true;
131    }
132   
133    /**
134    @brief 
135        Sets the Pickupable to picked up.
136        This method will be called by the PickupCarrier picking the Pickupable up.
137    @param carrier
138        The PickupCarrier that picked the Pickupable up.
139    @return
140        Returns false if, for some reason, the pickup could not be picked up, e.g. it was picked up already.
141    */
142    bool Pickupable::pickedUp(PickupCarrier* carrier)
143    {
144        if(this->isPickedUp()) //!< If the Pickupable is already picked up.
145            return false;
146       
147        COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl;
148        this->setCarrier(carrier);
149        this->setPickedUp(true);
150        return true;
151    }
152   
153    /**
154    @brief
155        Sets the Pickupable to not picked up or dropped.
156        This method will be called by the PickupCarrier dropping the Pickupable.
157    @return
158        Returns false if the pickup could not be dropped.
159    */
160    bool Pickupable::dropped(void)
161    {
162        if(!this->isPickedUp()) //!< If the Pickupable is not picked up.
163            return false;
164       
165        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
166        this->setUsed(false);
167        this->setPickedUp(false);
168       
169        bool created = this->createSpawner(this->getCarrier()->getCarrierPosition());
170       
171        this->setCarrier(NULL);
172        if(!created)
173            this->destroy();
174       
175        return true;
176    }
177   
178       
179    /**
180    @brief
181        Sets the carrier of the pickup.
182    @param carrier
183        Sets the input PickupCarrier as the carrier of the pickup.
184    @return
185        Returns true if the carrier was changed, false if not.
186    */
187    bool Pickupable::setCarrier(PickupCarrier* carrier)
188    {
189        if(this->getCarrier() == carrier)
190            return false;
191       
192        this->carrier_ = carrier;
193        this->changedCarrier();
194        return true;
195    }
196   
197    /**
198    @brief
199        Creates a duplicate of the Pickupable.
200    @return
201        Returns the clone of this pickup as a pointer to a Pickupable.
202    */
203    //TODO: Does this work?
204    Pickupable* Pickupable::clone(void)
205    {
206        Pickupable* pickup = NULL;
207        this->clone(pickup);
208       
209        COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl;
210        return pickup;
211    }
212   
213    /**
214    @brief
215        Creates a duplicate of the input OrxonoxClass.
216        This method needs to be implemented by any Class inheriting from Pickupable.
217    @param item
218        A pointer to the OrxonoxClass that is to be duplicated.
219    */
220    //TODO: Specify how the implementation must be done in detail.
221    void Pickupable::clone(OrxonoxClass* item)
222    {
223        SUPER(Pickupable, clone, item);
224    }
225   
226}
Note: See TracBrowser for help on using the repository browser.