Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Simplification in creation of PickupIdentifier.

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