Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/orxonox/interfaces/Pickupable.cc @ 9571

Last change on this file since 9571 was 9571, checked in by landauf, 11 years ago

using Destroyable instead of OrxonoxClass now wherever the destruction-related members are addressed

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