Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/interfaces/Pickupable.cc @ 7501

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

Some documenting and cleaning up/re-organization in pickups module.

  • Property svn:eol-style set to native
File size: 11.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 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
42#include "infos/PlayerInfo.h"
43#include "pickup/PickupIdentifier.h"
44#include "worldentities/pawns/Pawn.h"
45
46#include "PickupCarrier.h"
47
48namespace orxonox
49{
50
51    /**
52    @brief
53        Constructor. Registers the objects and initializes its member variables.
54    */
55    Pickupable::Pickupable() : pickupIdentifier_(NULL), used_(false), pickedUp_(false)
56    {
57        RegisterRootObject(Pickupable);
58
59        this->carrier_ = NULL;
60
61        this->pickupIdentifier_ = new PickupIdentifier(this);
62        this->beingDestroyed_ = false;
63        this->enabled_ = true;
64    }
65
66    /**
67    @brief
68        Destructor.
69    */
70    Pickupable::~Pickupable()
71    {
72        if(this->pickupIdentifier_ != NULL)
73        {
74            COUT(4) << "Pickupable (" << this->getIdentifier()->getName() << ") (&" << this << ") destroyed." << std::endl;
75            this->pickupIdentifier_->destroy();
76        }
77    }
78
79    /**
80    @brief
81        A method that is called by OrxonoxClass::destroy() before the object is actually destroyed.
82    */
83    void Pickupable::preDestroy(void)
84    {
85        this->beingDestroyed_ = true;
86
87        if(this->isPickedUp())
88            this->drop(false); // Drops the pickup without creating a PickupSpawner.
89    }
90
91    /**
92    @brief
93        Is called internally within the pickup module to destroy pickups.
94    */
95    void Pickupable::destroy(void)
96    {
97        this->destroyPickup();
98    }
99
100    /**
101    @brief
102        Destroys a Pickupable.
103        If the Pickupable is already in the process of being destroyed a warning is displayed and this method is skipped.
104    */
105    void Pickupable::destroyPickup(void)
106    {
107        if(!this->beingDestroyed_)
108            this->OrxonoxClass::destroy();
109        else
110            COUT(2) << this->getIdentifier()->getName() << " may be unsafe. " << std::endl;
111    }
112
113    /**
114    @brief
115        Sets the Pickupable to used or unused, depending on the input.
116    @param used
117        If used is true the Pickupable is set to used, it is set to unused, otherwise.
118    @return
119        Returns true if the used state was changed, false if not.
120    */
121    bool Pickupable::setUsed(bool used)
122    {
123        if(this->used_ == used || !this->isPickedUp()) // If either the used status of the Pickupable doesn't change or it isn't picked up.
124            return false;
125
126        if((!this->isUsable() && used) || (!this->isUnusable() && !used)) // If either the Pickupable is requested to be used but it is not usable or the Pickupable is requested to be unused, while it is not unusable.
127            return false;
128
129        COUT(4) << "Pickupable (&" << this << ") set to used " << used << "." << std::endl;
130
131        this->used_ = used;
132        this->changedUsed();
133
134        //TODO: Synchronize & make safe for dedicated server.
135        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
136        return true;
137    }
138
139    /**
140    @brief
141        Get whether the given PickupCarrier is a target of this Pickupable.
142    @param carrier
143        The PickupCarrier of which it has to be determinde whether it is a target of this Pickupable.
144    @return
145        Returns true if the given PickupCarrier is a target.
146    */
147    bool Pickupable::isTarget(PickupCarrier* carrier) const
148    {
149        if(carrier == NULL)
150            return false;
151
152        return this->isTarget(carrier->getIdentifier());
153    }
154
155    /**
156    @brief
157        Get whether the given Identififer is a target of this Pickupable.
158    @param identifier
159        The PickupCarrier of which it has to be determinde whether it is a target of this Pickupable.
160    @return
161        Returns true if the given PickupCarrier is a target.
162    */
163    bool Pickupable::isTarget(const Identifier* identifier) const
164    {
165        //! Iterate through all targets of this Pickupable.
166        for(std::list<Identifier*>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++)
167        {
168            if(identifier->isA(*it))
169                return true;
170        }
171
172        return false;
173    }
174
175    /**
176    @brief
177        Add a PickupCarrier as target of this Pickupable.
178    @param target
179        The PickupCarrier to be added.
180    @return
181        Returns true if the target was added, false if not.
182    */
183    bool Pickupable::addTarget(PickupCarrier* target)
184    {
185        return this->addTarget(target->getIdentifier());
186    }
187
188    /**
189    @brief
190        Add a class, representetd by the input Identifier, as target of this Pickupable.
191    @param target
192        The Identifier to be added.
193    @return
194        Returns true if the target was added, false if not.
195    */
196    bool Pickupable::addTarget(Identifier* target)
197    {
198        if(this->isTarget(target)) //!< If the input target is already present in the list of targets.
199            return false;
200
201        COUT(4) << "Target " << target->getName() << " added to Pickupable (&" << this << ")." << std::endl;
202        this->targets_.push_back(target);
203        return true;
204    }
205
206    /**
207    @brief
208        Can be called to pick up a Pickupable.
209    @param carrier
210        A pointer to the PickupCarrier that picks up the Pickupable.
211    @return
212        Returns true if the Pickupable was picked up, false if not.
213    */
214    bool Pickupable::pickup(PickupCarrier* carrier)
215    {
216        if(carrier == NULL || this->isPickedUp()) //!< If carrier is NULL or the Pickupable is already picked up.
217            return false;
218
219        if(!this->setCarrier(carrier))
220        {
221            COUT(3) << "A Pickupable (&" << this << ") was trying to be added to a PickupCarrier, but was already present." << std::endl;
222            return false;
223        }
224       
225        this->setPickedUp(true);
226        COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl;
227        return true;
228    }
229
230    /**
231    @brief
232        Can be called to drop a Pickupable.
233    @param createSpawner
234        If true a spawner is to be created for the dropped Pickupable. True is default.
235    @return
236        Returns true if the Pickupable has been dropped, false if not.
237    */
238    bool Pickupable::drop(bool createSpawner)
239    {
240        if(!this->isPickedUp()) // If the Pickupable is not picked up.
241            return false;
242
243        assert(this->getCarrier()); // The Carrier cannot be NULL at this point.
244        if(!this->getCarrier()->removePickup(this)) //TODO Shouldn't this be a little later?
245            COUT(2) << "Pickupable (&" << this << ", " << this->getIdentifier()->getName() << ") is being dropped, but it was not present in the PickupCarriers list of pickups." << std::endl;
246
247        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
248        this->setUsed(false);
249        this->setPickedUp(false);
250
251        bool created = false;
252        if(createSpawner)
253            created = this->createSpawner();
254
255        this->setCarrier(NULL);
256
257        if(!created && createSpawner) // If a PickupSpawner should have been created but wasn't.
258            this->destroy();
259
260        return true;
261    }
262
263    /**
264    @brief
265        Helper method to set the Pickupable to either picked up or not picked up.
266    @param pickedUp
267        The value this->pickedUp_ should be set to.
268    @return
269        Returns true if the pickedUp status was changed, false if not.
270    */
271    bool Pickupable::setPickedUp(bool pickedUp)
272    {
273        if(this->pickedUp_ == pickedUp) // If the picked up status has not changed.
274            return false;
275
276        COUT(4) << "Pickupable (&" << this << ") set to pickedUp " << pickedUp << "." << std::endl;
277
278        this->pickedUp_ = pickedUp;
279        if(!pickedUp) // if the Pickupable has been dropped it unregisters itself with its PickupCarrier.
280            this->getCarrier()->removePickup(this);
281        this->changedPickedUp();
282
283        //TODO: Synchronize & make safe for dedicated server.
284        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
285        return true;
286    }
287
288    /**
289    @brief
290        Sets the carrier of the Pickupable.
291    @param carrier
292        Sets the input PickupCarrier as the carrier of the pickup.
293    @param tell
294        If true (default) the pickup is added to the list of pickups in the PickupCarrier.
295    @return
296        Returns true if successful, false if not.
297    */
298    bool Pickupable::setCarrier(orxonox::PickupCarrier* carrier, bool tell)
299    {
300        if(this->carrier_ == carrier) // If the PickupCarrier doesn't change.
301            return false;
302
303        COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl;
304
305        if(carrier != NULL && tell)
306        {
307            if(!carrier->addPickup(this))
308                return false;
309        }
310
311        this->carrier_ = carrier;
312        this->changedCarrier();
313        return true;
314    }
315
316    /**
317    @brief
318        Is called by the PickupCarrier when it is being destroyed.
319    */
320    void Pickupable::carrierDestroyed(void)
321    {
322        this->destroy();
323    }
324
325    /**
326    @brief
327        Creates a duplicate of the Pickupable.
328    @return
329        Returns the clone of this pickup as a pointer to a Pickupable.
330    */
331    Pickupable* Pickupable::clone(void)
332    {
333        OrxonoxClass* item = NULL;
334        this->clone(item);
335
336        Pickupable* pickup = dynamic_cast<Pickupable*>(item);
337
338        COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl;
339        return pickup;
340    }
341
342    /**
343    @brief
344        Creates a duplicate of the input OrxonoxClass.
345        This method needs to be implemented by any Class inheriting from Pickupable.
346    @param item
347        A reference to a pointer to the OrxonoxClass that is to be duplicated.
348    */
349    void Pickupable::clone(OrxonoxClass*& item)
350    {
351        SUPER(Pickupable, clone, item);
352    }
353
354    /**
355    @brief
356        Method to transcribe a Pickupable as a Rewardable to the player.
357    @param player
358        A pointer to the PlayerInfo, do whatever you want with it.
359    @return
360        Return true if successful.
361    */
362    bool Pickupable::reward(PlayerInfo* player)
363    {
364        ControllableEntity* entity = player->getControllableEntity();
365        Pawn* pawn = static_cast<Pawn*>(entity);
366        PickupCarrier* carrier = static_cast<PickupCarrier*>(pawn);
367        return this->pickup(carrier);
368    }
369
370}
Note: See TracBrowser for help on using the repository browser.