Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Untangled Pickupable and PickupCarrier a little bit.

  • Property svn:eol-style set to native
File size: 9.2 KB
RevLine 
[6474]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/**
[6538]30    @file Pickupable.cc
[6474]31    @brief Implementation of the Pickupable class.
32*/
33
34#include "Pickupable.h"
35
[6996]36#include "core/LuaState.h"
37#include "core/GUIManager.h"
[6474]38#include "core/Identifier.h"
39#include "core/CoreIncludes.h"
[6996]40#include "util/Convert.h"
41#include "infos/PlayerInfo.h"
[6475]42#include "pickup/PickupIdentifier.h"
[6996]43#include "worldentities/pawns/Pawn.h"
[6474]44#include "PickupCarrier.h"
45
46namespace orxonox
47{
[7127]48
[6474]49    /**
50    @brief
51        Constructor. Registers the objects and initializes its member variables.
52    */
[6725]53    Pickupable::Pickupable() : pickupIdentifier_(NULL), used_(false), pickedUp_(false)
[7127]54    {
[6478]55        RegisterRootObject(Pickupable);
[7127]56
[6474]57        this->carrier_ = NULL;
[7127]58
[6480]59        this->pickupIdentifier_ = new PickupIdentifier(this);
[6474]60    }
[7127]61
[6474]62    /**
63    @brief
[7127]64        Destructor.
[6474]65    */
66    Pickupable::~Pickupable()
67    {
[6477]68        if(this->isUsed())
69            this->setUsed(false);
[7127]70
[7150]71        if(this->isPickedUp())
[6477]72        {
[7150]73            this->drop(false);
[6477]74        }
[7127]75
[6725]76        if(this->pickupIdentifier_ != NULL)
77            this->pickupIdentifier_->destroy();
[6474]78    }
[7127]79
[6474]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;
[7127]92
[6474]93        COUT(4) << "Pickupable (&" << this << ") set to used " << used << "." << std::endl;
[7127]94
[6474]95        this->used_ = used;
96        this->changedUsed();
[6996]97
98        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
[6474]99        return true;
100    }
[7127]101
[6474]102    /**
103    @brief
[6538]104        Get whether the given PickupCarrier is a target of this Pickupable.
[6474]105    @param carrier
[6538]106        The PickupCarrier of which it has to be determinde whether it is a target of this Pickupable.
[6474]107    @return
108        Returns true if the given PickupCarrier is a target.
109    */
[6901]110    bool Pickupable::isTarget(PickupCarrier* carrier) const
[6474]111    {
[6731]112        if(carrier == NULL)
113            return false;
[6490]114        return this->isTarget(carrier->getIdentifier());
115    }
[7127]116
[6490]117    /**
118    @brief
[6731]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.
[6490]122    @return
[6731]123        Returns true if the given PickupCarrier is a target.
[6490]124    */
[6731]125    bool Pickupable::isTarget(const Identifier* identifier) const
[6490]126    {
[6474]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        {
[6731]130            if(identifier->isA(*it))
[6474]131                return true;
132        }
133        return false;
134    }
[7127]135
[6474]136    /**
137    @brief
[6538]138        Add a PickupCarrier as target of this Pickupable.
[6474]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    {
[6490]146        return this->addTarget(target->getIdentifier());
147    }
[7127]148
[6490]149    /**
150    @brief
[6538]151        Add a class, representetd by the input Identifier, as target of this Pickupable.
[6490]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    {
[6474]159        if(this->isTarget(target)) //!< If the input target is already present in the list of targets.
160            return false;
[7127]161
[6490]162        COUT(4) << "Target " << target->getName() << " added to Pickupable (&" << this << ")." << std::endl;
163        this->targets_.push_back(target);
[6474]164        return true;
165    }
[7127]166
[6474]167    /**
[7127]168    @brief
[7150]169        Can be called to pick up a Pickupable.
[6474]170    @param carrier
[7150]171        A pointer to the PickupCarrier that picks up the Pickupable.
[6474]172    @return
[7150]173        Returns true if the Pickupable was picked up, false if not.
[6474]174    */
[7150]175    bool Pickupable::pickup(PickupCarrier* carrier)
[6474]176    {
[7150]177        if(carrier == NULL || this->isPickedUp()) //!< If carrier is NULL or the Pickupable is already picked up.
[6474]178            return false;
[7127]179
[7150]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
[6474]186        COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl;
[6521]187        this->setCarrier(carrier);
[6497]188        this->setPickedUp(true);
[6474]189        return true;
190    }
[7127]191
[6474]192    /**
[6521]193    @brief
[7150]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
[6521]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;
[7127]239
[6521]240        COUT(4) << "Pickupable (&" << this << ") set to pickedUp " << pickedUp << "." << std::endl;
[7127]241
[6521]242        this->pickedUp_ = pickedUp;
243        this->changedPickedUp();
[6996]244        GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()");
[6521]245        return true;
246    }
[7127]247
[6521]248    /**
249    @brief
[6538]250        Sets the carrier of the Pickupable.
[6521]251    @param carrier
252        Sets the input PickupCarrier as the carrier of the pickup.
253    */
[7094]254    inline bool Pickupable::setCarrier(PickupCarrier* carrier, bool tell)
[6521]255    {
256        if(this->carrier_ == carrier)
257            return false;
[7127]258
[6521]259        COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl;
[7127]260
[6521]261        this->carrier_ = carrier;
262        this->changedCarrier();
[7094]263        if(tell && carrier != NULL)
264            this->carrier_->pickups_.insert(this);
[6521]265        return true;
266    }
[7127]267
[6521]268    /**
[7127]269    @brief
[6474]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    {
[6497]276        OrxonoxClass* item = NULL;
277        this->clone(item);
[7127]278
[6497]279        Pickupable* pickup = dynamic_cast<Pickupable*>(item);
[7127]280
[6474]281        COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl;
282        return pickup;
283    }
[7127]284
[6474]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
[6538]290        A reference to a pointer to the OrxonoxClass that is to be duplicated.
[6474]291    */
[6497]292    void Pickupable::clone(OrxonoxClass*& item)
[6474]293    {
294        SUPER(Pickupable, clone, item);
295    }
[6996]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);
[7150]310        return this->pickup(carrier);
[6996]311    }
[7127]312
[6474]313}
Note: See TracBrowser for help on using the repository browser.