Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Some more documentation.

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