Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6715 was 6710, checked in by dafrick, 15 years ago

Merged ppspickups2 branch into trunk.

File size: 5.0 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            this->startPickupTimer(this->getDuration());
116            this->setInvisible(true);
117        }
118        else
119        {
120            this->setInvisible(false);
121            this->destroy();
122        }
123       
124    }
125   
126    /**
127    @brief
128        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
129    @return
130        A pointer to the Pawn, or NULL if the conversion failed.
131    */
132    Pawn* InvisiblePickup::carrierToPawnHelper(void)
133    {
134        PickupCarrier* carrier = this->getCarrier();
135        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
136       
137        if(pawn == NULL)
138        {
139            COUT(1) << "Invalid PickupCarrier in InvisiblePickup." << std::endl;
140        }
141        return pawn;
142    }
143   
144    /**
145    @brief
146        Creates a duplicate of the input OrxonoxClass.
147    @param item
148        A pointer to the Orxonox class.
149    */
150    void InvisiblePickup::clone(OrxonoxClass*& item)
151    {
152        if(item == NULL)
153            item = new InvisiblePickup(this);
154       
155        SUPER(InvisiblePickup, clone, item);
156       
157        InvisiblePickup* pickup = dynamic_cast<InvisiblePickup*>(item);
158        pickup->setDuration(this->getDuration());
159        pickup->initializeIdentifier();
160    }
161   
162    /**
163    @brief
164        Sets the invisibility.
165    @param invisibility
166        The invisibility.
167    */
168    bool InvisiblePickup::setInvisible(bool invisibility)
169    {
170        Pawn* pawn = this->carrierToPawnHelper();
171        if(pawn == NULL)
172            return false;
173       
174        pawn->setVisible(!invisibility);
175        return true;
176    }
177   
178    /**
179    @brief
180        Sets the duration.
181    @param duration
182        The duration
183    */
184    void InvisiblePickup::setDuration(float duration)
185    {
186        if(duration >= 0.0f)
187        {
188            this->duration_ = duration;
189        }
190        else
191        {
192            COUT(1) << "Invalid duration in InvisiblePickup." << std::endl;
193            this->duration_ = 0.0f;
194        }
195    }
196   
197    void InvisiblePickup::pickupTimerCallback(void)
198    {
199        this->setUsed(false);
200    }
201
202}
Note: See TracBrowser for help on using the repository browser.