Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/orxonox/interfaces/Pickupable.cc @ 7096

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

(Hopefully) fix in pickups.

File size: 8.9 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 Pickupable.cc
31    @brief Implementation of the Pickupable class.
32*/
33
34#include "Pickupable.h"
35
36#include "core/LuaState.h"
37#include "core/GUIManager.h"
38#include "core/Identifier.h"
39#include "core/CoreIncludes.h"
40#include "util/Convert.h"
41#include "infos/PlayerInfo.h"
42#include "pickup/PickupIdentifier.h"
43#include "worldentities/pawns/Pawn.h"
44#include "PickupCarrier.h"
45
46namespace orxonox
47{
48   
49    /**
50    @brief
51        Constructor. Registers the objects and initializes its member variables.
52    */
53    Pickupable::Pickupable() : pickupIdentifier_(NULL), used_(false), pickedUp_(false)
54    {       
55        RegisterRootObject(Pickupable);
56       
57        this->carrier_ = NULL;
58       
59        this->pickupIdentifier_ = new PickupIdentifier(this);
60    }
61   
62    /**
63    @brief
64        Destructor.
65    */
66    Pickupable::~Pickupable()
67    {
68        if(this->isUsed())
69            this->setUsed(false);
70       
71        if(this->isPickedUp() && this->getCarrier() != NULL)
72        {
73            this->getCarrier()->drop(this, false);
74            this->setCarrier(NULL);
75        }
76       
77        if(this->pickupIdentifier_ != NULL)
78            this->pickupIdentifier_->destroy();
79    }
80   
81    /**
82    @brief
83        Sets the Pickupable to used or unused, depending on the input.
84    @param used
85        If used is true the Pickupable is set to used, it is set to unused, otherwise.
86    @return
87        Returns true if the used state was changed, false if not.
88    */
89    bool Pickupable::setUsed(bool used)
90    {
91        if(this->used_ == used)
92            return false;
93       
94        COUT(4) << "Pickupable (&" << this << ") set to used " << used << "." << std::endl;
95       
96        this->used_ = used;
97        this->changedUsed();
98
99        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
100        return true;
101    }
102   
103    /**
104    @brief
105        Get whether the given PickupCarrier is a target of this Pickupable.
106    @param carrier
107        The PickupCarrier of which it has to be determinde whether it is a target of this Pickupable.
108    @return
109        Returns true if the given PickupCarrier is a target.
110    */
111    bool Pickupable::isTarget(PickupCarrier* carrier) const
112    {
113        if(carrier == NULL)
114            return false;
115        return this->isTarget(carrier->getIdentifier());
116    }
117   
118    /**
119    @brief
120        Get whether the given Identififer is a target of this Pickupable.
121    @param identifier
122        The PickupCarrier of which it has to be determinde whether it is a target of this Pickupable.
123    @return
124        Returns true if the given PickupCarrier is a target.
125    */
126    bool Pickupable::isTarget(const Identifier* identifier) const
127    {
128        //! Iterate through all targets of this Pickupable.
129        for(std::list<Identifier*>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++)
130        {
131            if(identifier->isA(*it))
132                return true;
133        }
134        return false;
135    }
136       
137    /**
138    @brief
139        Add a PickupCarrier as target of this Pickupable.
140    @param target
141        The PickupCarrier to be added.
142    @return
143        Returns true if the target was added, false if not.
144    */
145    bool Pickupable::addTarget(PickupCarrier* target)
146    {
147        return this->addTarget(target->getIdentifier());
148    }
149   
150    /**
151    @brief
152        Add a class, representetd by the input Identifier, as target of this Pickupable.
153    @param target
154        The Identifier to be added.
155    @return
156        Returns true if the target was added, false if not.
157    */
158    bool Pickupable::addTarget(Identifier* target)
159    {
160        if(this->isTarget(target)) //!< If the input target is already present in the list of targets.
161            return false;
162       
163        COUT(4) << "Target " << target->getName() << " added to Pickupable (&" << this << ")." << std::endl;
164        this->targets_.push_back(target);
165        return true;
166    }
167   
168    /**
169    @brief 
170        Sets the Pickupable to picked up.
171        This method will be called by the PickupCarrier picking the Pickupable up.
172    @param carrier
173        The PickupCarrier that picked the Pickupable up.
174    @return
175        Returns false if, for some reason, the pickup could not be picked up, e.g. it was picked up already.
176    */
177    bool Pickupable::pickedUp(PickupCarrier* carrier)
178    {
179        if(this->isPickedUp()) //!< If the Pickupable is already picked up.
180            return false;
181       
182        COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl;
183        this->setCarrier(carrier);
184        this->setPickedUp(true);
185        return true;
186    }
187   
188    /**
189    @brief
190        Helper method to set the Pickupable to either picked up or not picked up.
191    @param pickedUp
192        The value this->pickedUp_ should be set to.
193    @return
194        Returns true if the pickedUp status was changed, false if not.
195    */
196    bool Pickupable::setPickedUp(bool pickedUp)
197    {
198        if(this->pickedUp_ == pickedUp)
199            return false;
200       
201        COUT(4) << "Pickupable (&" << this << ") set to pickedUp " << pickedUp << "." << std::endl;
202       
203        this->pickedUp_ = pickedUp;
204        this->changedPickedUp();
205        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
206        return true;
207    }
208       
209    /**
210    @brief
211        Sets the carrier of the Pickupable.
212    @param carrier
213        Sets the input PickupCarrier as the carrier of the pickup.
214    */
215    inline bool Pickupable::setCarrier(PickupCarrier* carrier, bool tell)
216    {
217        if(this->carrier_ == carrier)
218            return false;
219       
220        COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl;
221       
222        this->carrier_ = carrier;
223        this->changedCarrier();
224        if(tell && carrier != NULL)
225            this->carrier_->pickups_.insert(this);
226        return true;
227    }
228   
229    /**
230    @brief
231        Sets the Pickupable to not picked up or dropped.
232        This method will be called by the PickupCarrier dropping the Pickupable.
233    @return
234        Returns false if the pickup could not be dropped.
235    */
236    bool Pickupable::dropped(void)
237    {
238        if(!this->isPickedUp()) //!< If the Pickupable is not picked up.
239            return false;
240       
241        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
242        this->setUsed(false);
243        this->setPickedUp(false);
244       
245        bool created = this->createSpawner();
246       
247        this->setCarrier(NULL);
248       
249        if(!created)
250        {
251            this->destroy();
252        }
253       
254        return true;
255    }
256   
257    /**
258    @brief
259        Creates a duplicate of the Pickupable.
260    @return
261        Returns the clone of this pickup as a pointer to a Pickupable.
262    */
263    Pickupable* Pickupable::clone(void)
264    {
265        OrxonoxClass* item = NULL;
266        this->clone(item);
267       
268        Pickupable* pickup = dynamic_cast<Pickupable*>(item);
269       
270        COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl;
271        return pickup;
272    }
273   
274    /**
275    @brief
276        Creates a duplicate of the input OrxonoxClass.
277        This method needs to be implemented by any Class inheriting from Pickupable.
278    @param item
279        A reference to a pointer to the OrxonoxClass that is to be duplicated.
280    */
281    void Pickupable::clone(OrxonoxClass*& item)
282    {
283        SUPER(Pickupable, clone, item);
284    }
285
286    /**
287    @brief
288        Method to transcribe a Pickupable as a Rewardable to the player.
289    @param player
290        A pointer to the PlayerInfo, do whatever you want with it.
291    @return
292        Return true if successful.
293    */
294    bool Pickupable::reward(PlayerInfo* player)
295    {
296        ControllableEntity* entity = player->getControllableEntity();
297        Pawn* pawn = static_cast<Pawn*>(entity);
298        PickupCarrier* carrier = static_cast<PickupCarrier*>(pawn);
299        return carrier->pickup(this);
300    }
301   
302}
Note: See TracBrowser for help on using the repository browser.