Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/HUD_HS16/src/orxonox/interfaces/Pickupable.cc @ 11592

Last change on this file since 11592 was 11510, checked in by patricwi, 7 years ago

looking for SEGFault

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