Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixing small bug in Script (regarding number of executions).
Fixed bug in WorldEntity, that caused the visibility and activity to be synchronized incorrectly (since bVisibleMem and bActiveMem are not synchronized).
Some small changed in documentation.
Started "synchronizing" pickups. Seems to work (except GUI), but haven't done any extensive testing yet.

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