Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7080 was 6996, checked in by dafrick, 15 years ago

Loads of changes.
1) PickupInventory should now be working even for extreme cases.
2) Added support for inactive Spawnpoints in Gametype.
3) Made Pickupable rewardble. meaning from now on any Pickupable can be given as a reward for completing Quests.
4) Added some keybinds to KeybindMenu, such as PickupInventory, QuestGUI and Chat.

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)
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        return true;
225    }
226   
227    /**
228    @brief
229        Sets the Pickupable to not picked up or dropped.
230        This method will be called by the PickupCarrier dropping the Pickupable.
231    @return
232        Returns false if the pickup could not be dropped.
233    */
234    bool Pickupable::dropped(void)
235    {
236        if(!this->isPickedUp()) //!< If the Pickupable is not picked up.
237            return false;
238       
239        COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl;
240        this->setUsed(false);
241        this->setPickedUp(false);
242       
243        bool created = this->createSpawner();
244       
245        this->setCarrier(NULL);
246       
247        if(!created)
248        {
249            this->destroy();
250        }
251       
252        return true;
253    }
254   
255    /**
256    @brief
257        Creates a duplicate of the Pickupable.
258    @return
259        Returns the clone of this pickup as a pointer to a Pickupable.
260    */
261    Pickupable* Pickupable::clone(void)
262    {
263        OrxonoxClass* item = NULL;
264        this->clone(item);
265       
266        Pickupable* pickup = dynamic_cast<Pickupable*>(item);
267       
268        COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl;
269        return pickup;
270    }
271   
272    /**
273    @brief
274        Creates a duplicate of the input OrxonoxClass.
275        This method needs to be implemented by any Class inheriting from Pickupable.
276    @param item
277        A reference to a pointer to the OrxonoxClass that is to be duplicated.
278    */
279    void Pickupable::clone(OrxonoxClass*& item)
280    {
281        SUPER(Pickupable, clone, item);
282    }
283
284    /**
285    @brief
286        Method to transcribe a Pickupable as a Rewardable to the player.
287    @param player
288        A pointer to the PlayerInfo, do whatever you want with it.
289    @return
290        Return true if successful.
291    */
292    bool Pickupable::reward(PlayerInfo* player)
293    {
294        ControllableEntity* entity = player->getControllableEntity();
295        Pawn* pawn = static_cast<Pawn*>(entity);
296        PickupCarrier* carrier = static_cast<PickupCarrier*>(pawn);
297        return carrier->pickup(this);
298    }
299   
300}
Note: See TracBrowser for help on using the repository browser.