Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Additional documentation, code niceifying and potential bug fixing. Also: Renamed DroppedItem to DroppedPickup.

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