Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10871 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

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