Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Untangled Pickupable and PickupCarrier a little bit.

  • Property svn:eol-style set to native
File size: 9.2 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())
72        {
73            this->drop(false);
74        }
75
76        if(this->pickupIdentifier_ != NULL)
77            this->pickupIdentifier_->destroy();
78    }
79
80    /**
81    @brief
82        Sets the Pickupable to used or unused, depending on the input.
83    @param used
84        If used is true the Pickupable is set to used, it is set to unused, otherwise.
85    @return
86        Returns true if the used state was changed, false if not.
87    */
88    bool Pickupable::setUsed(bool used)
89    {
90        if(this->used_ == used)
91            return false;
92
93        COUT(4) << "Pickupable (&" << this << ") set to used " << used << "." << std::endl;
94
95        this->used_ = used;
96        this->changedUsed();
97
98        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
99        return true;
100    }
101
102    /**
103    @brief
104        Get whether the given PickupCarrier is a target of this Pickupable.
105    @param carrier
106        The PickupCarrier of which it has to be determinde whether it is a target of this Pickupable.
107    @return
108        Returns true if the given PickupCarrier is a target.
109    */
110    bool Pickupable::isTarget(PickupCarrier* carrier) const
111    {
112        if(carrier == NULL)
113            return false;
114        return this->isTarget(carrier->getIdentifier());
115    }
116
117    /**
118    @brief
119        Get whether the given Identififer is a target of this Pickupable.
120    @param identifier
121        The PickupCarrier of which it has to be determinde whether it is a target of this Pickupable.
122    @return
123        Returns true if the given PickupCarrier is a target.
124    */
125    bool Pickupable::isTarget(const Identifier* identifier) const
126    {
127        //! Iterate through all targets of this Pickupable.
128        for(std::list<Identifier*>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++)
129        {
130            if(identifier->isA(*it))
131                return true;
132        }
133        return false;
134    }
135
136    /**
137    @brief
138        Add a PickupCarrier as target of this Pickupable.
139    @param target
140        The PickupCarrier to be added.
141    @return
142        Returns true if the target was added, false if not.
143    */
144    bool Pickupable::addTarget(PickupCarrier* target)
145    {
146        return this->addTarget(target->getIdentifier());
147    }
148
149    /**
150    @brief
151        Add a class, representetd by the input Identifier, as target of this Pickupable.
152    @param target
153        The Identifier to be added.
154    @return
155        Returns true if the target was added, false if not.
156    */
157    bool Pickupable::addTarget(Identifier* target)
158    {
159        if(this->isTarget(target)) //!< If the input target is already present in the list of targets.
160            return false;
161
162        COUT(4) << "Target " << target->getName() << " added to Pickupable (&" << this << ")." << std::endl;
163        this->targets_.push_back(target);
164        return true;
165    }
166
167    /**
168    @brief
169        Can be called to pick up a Pickupable.
170    @param carrier
171        A pointer to the PickupCarrier that picks up the Pickupable.
172    @return
173        Returns true if the Pickupable was picked up, false if not.
174    */
175    bool Pickupable::pickup(PickupCarrier* carrier)
176    {
177        if(carrier == NULL || this->isPickedUp()) //!< If carrier is NULL or the Pickupable is already picked up.
178            return false;
179
180        if(!carrier->addPickup(this))
181        {
182            COUT(3) << "A Pickupable (&" << this << ") was trying to be added to a PickupCarrier, but was already present." << std::endl;
183            return false;
184        }
185
186        COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl;
187        this->setCarrier(carrier);
188        this->setPickedUp(true);
189        return true;
190    }
191
192    /**
193    @brief
194        Can be called to drop a Pickupable.
195    @param createSpawner
196        If true a spawner is be created for the dropped Pickupable. True is default.
197    @return
198        Returns true if the Pickupable has been dropped, false if not.
199    */
200    bool Pickupable::drop(bool createSpawner)
201    {
202        if(!this->isPickedUp()) //!< If the Pickupable is not picked up.
203            return false;
204
205        assert(this->getCarrier()); //!> The Carrier cannot be NULL at this point. //TODO: Too conservative?
206        if(!this->getCarrier()->removePickup(this)) //TODO Shouldn't this be a little later?
207            COUT(2) << "Pickupable (&" << this << ") is being dropped, but it was not present in the PickupCarriers list of pickups." << std::endl;
208       
209        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
210        this->setUsed(false);
211        this->setPickedUp(false);
212
213        bool created = false;
214        if(createSpawner)
215            created = this->createSpawner();
216
217        this->setCarrier(NULL);
218
219        if(!created && createSpawner)
220        {
221            this->destroy();
222        }
223
224        return true;
225    }
226
227    /**
228    @brief
229        Helper method to set the Pickupable to either picked up or not picked up.
230    @param pickedUp
231        The value this->pickedUp_ should be set to.
232    @return
233        Returns true if the pickedUp status was changed, false if not.
234    */
235    bool Pickupable::setPickedUp(bool pickedUp)
236    {
237        if(this->pickedUp_ == pickedUp)
238            return false;
239
240        COUT(4) << "Pickupable (&" << this << ") set to pickedUp " << pickedUp << "." << std::endl;
241
242        this->pickedUp_ = pickedUp;
243        this->changedPickedUp();
244        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
245        return true;
246    }
247
248    /**
249    @brief
250        Sets the carrier of the Pickupable.
251    @param carrier
252        Sets the input PickupCarrier as the carrier of the pickup.
253    */
254    inline bool Pickupable::setCarrier(PickupCarrier* carrier, bool tell)
255    {
256        if(this->carrier_ == carrier)
257            return false;
258
259        COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl;
260
261        this->carrier_ = carrier;
262        this->changedCarrier();
263        if(tell && carrier != NULL)
264            this->carrier_->pickups_.insert(this);
265        return true;
266    }
267
268    /**
269    @brief
270        Creates a duplicate of the Pickupable.
271    @return
272        Returns the clone of this pickup as a pointer to a Pickupable.
273    */
274    Pickupable* Pickupable::clone(void)
275    {
276        OrxonoxClass* item = NULL;
277        this->clone(item);
278
279        Pickupable* pickup = dynamic_cast<Pickupable*>(item);
280
281        COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl;
282        return pickup;
283    }
284
285    /**
286    @brief
287        Creates a duplicate of the input OrxonoxClass.
288        This method needs to be implemented by any Class inheriting from Pickupable.
289    @param item
290        A reference to a pointer to the OrxonoxClass that is to be duplicated.
291    */
292    void Pickupable::clone(OrxonoxClass*& item)
293    {
294        SUPER(Pickupable, clone, item);
295    }
296
297    /**
298    @brief
299        Method to transcribe a Pickupable as a Rewardable to the player.
300    @param player
301        A pointer to the PlayerInfo, do whatever you want with it.
302    @return
303        Return true if successful.
304    */
305    bool Pickupable::reward(PlayerInfo* player)
306    {
307        ControllableEntity* entity = player->getControllableEntity();
308        Pawn* pawn = static_cast<Pawn*>(entity);
309        PickupCarrier* carrier = static_cast<PickupCarrier*>(pawn);
310        return this->pickup(carrier);
311    }
312
313}
Note: See TracBrowser for help on using the repository browser.