Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/items/InvisiblePickup.cc @ 6755

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

Fixed Segfault upon dropping of InvisiblePickup, while it was being used.

File size: 5.5 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 *      Benedict Simlinger
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file InvisiblePickup.cc
31    @brief Implementation of the InvisiblePickup class.
32*/
33
34#include "InvisiblePickup.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "util/StringUtils.h"
39
40#include "worldentities/pawns/Pawn.h"
41#include "pickup/PickupIdentifier.h"
42
43#include <sstream>
44
45namespace orxonox
46{
47
48    CreateFactory(InvisiblePickup);
49   
50    /**
51    @brief
52        Constructor. Registers the object and initializes the member variables.
53    */
54    InvisiblePickup::InvisiblePickup(BaseObject* creator) : Pickup(creator)
55    {
56        RegisterObject(InvisiblePickup);
57        //! Defines who is allowed to pick up the pickup.
58        this->initialize(); 
59    }
60   
61    /**
62    @brief
63        Destructor.
64    */
65    InvisiblePickup::~InvisiblePickup()
66    {       
67    }
68   
69   
70    void InvisiblePickup::initializeIdentifier(void)
71    {
72        std::stringstream stream;
73        stream << this->getDuration();
74        std::string type1 = "duration";
75        std::string val1 = stream.str();
76        this->pickupIdentifier_->addParameter(type1, val1);
77    }
78   
79    /**
80    @brief
81    Initializes the member variables.
82    */
83    void InvisiblePickup::initialize(void)
84    {
85        this->duration_ = 0.0f;
86        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
87    }
88
89    /**
90    @brief
91        Method for creating a HealthPickup object through XML.
92    */
93    void InvisiblePickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
94    {
95        SUPER(InvisiblePickup, XMLPort, xmlelement, mode);   
96        XMLPortParam(InvisiblePickup, "duration", setDuration, getDuration, xmlelement, mode);
97       
98        this->initializeIdentifier();
99    }
100   
101    /**
102    @brief
103        Is called when the pickup has transited from used to unused or the other way around.
104    */
105    void InvisiblePickup::changedUsed(void)
106    {
107        SUPER(InvisiblePickup, changedUsed);
108       
109        //! If the pickup is not picked up nothing must be done.
110        if(!this->isPickedUp())
111            return;
112       
113        if (this->isUsed())
114        {
115            if(!this->getTimer()->isActive() && this->getTimer()->getRemainingTime() > 0.0f)
116            {
117                this->getTimer()->unpauseTimer();
118            }
119            else
120            {
121                this->startPickupTimer(this->getDuration());
122            }
123            this->setInvisible(true);
124        }
125        else
126        {
127            this->setInvisible(false);
128       
129            if(!this->getTimer()->isActive() && this->getTimer()->getRemainingTime() == this->getDuration())
130            {
131                this->destroy();
132            }
133            else
134            {
135                this->getTimer()->pauseTimer();
136            }
137        }
138       
139    }
140   
141    /**
142    @brief
143        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
144    @return
145        A pointer to the Pawn, or NULL if the conversion failed.
146    */
147    Pawn* InvisiblePickup::carrierToPawnHelper(void)
148    {
149        PickupCarrier* carrier = this->getCarrier();
150        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
151       
152        if(pawn == NULL)
153        {
154            COUT(1) << "Invalid PickupCarrier in InvisiblePickup." << std::endl;
155        }
156        return pawn;
157    }
158   
159    /**
160    @brief
161        Creates a duplicate of the input OrxonoxClass.
162    @param item
163        A pointer to the Orxonox class.
164    */
165    void InvisiblePickup::clone(OrxonoxClass*& item)
166    {
167        if(item == NULL)
168            item = new InvisiblePickup(this);
169       
170        SUPER(InvisiblePickup, clone, item);
171       
172        InvisiblePickup* pickup = dynamic_cast<InvisiblePickup*>(item);
173        pickup->setDuration(this->getDuration());
174        pickup->initializeIdentifier();
175    }
176   
177    /**
178    @brief
179        Sets the invisibility.
180    @param invisibility
181        The invisibility.
182    */
183    bool InvisiblePickup::setInvisible(bool invisibility)
184    {
185        Pawn* pawn = this->carrierToPawnHelper();
186        if(pawn == NULL)
187            return false;
188       
189        pawn->setVisible(!invisibility);
190        return true;
191    }
192   
193    /**
194    @brief
195        Sets the duration.
196    @param duration
197        The duration
198    */
199    void InvisiblePickup::setDuration(float duration)
200    {
201        if(duration >= 0.0f)
202        {
203            this->duration_ = duration;
204        }
205        else
206        {
207            COUT(1) << "Invalid duration in InvisiblePickup." << std::endl;
208            this->duration_ = 0.0f;
209        }
210    }
211   
212    void InvisiblePickup::pickupTimerCallback(void)
213    {
214        this->setUsed(false);
215    }
216
217}
Note: See TracBrowser for help on using the repository browser.