Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Improvements in InvisiblePickup.

  • Property svn:eol-style set to native
File size: 6.3 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    }
102
103    /**
104    @brief
105        Is called when the pickup has transited from used to unused or the other way around.
106    */
107    void InvisiblePickup::changedUsed(void)
108    {
109        SUPER(InvisiblePickup, changedUsed);
110
111        // If the pickup is not picked up nothing must be done.
112        if(!this->isPickedUp())
113            return;
114
115        if (this->isUsed())
116        {
117            if(this->isContinuous())
118            {
119                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
120                {
121                    this->durationTimer_.unpauseTimer();
122                }
123                else
124                {
125                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&InvisiblePickup::pickupTimerCallback, this)));
126                }
127            }
128
129            this->setInvisible(true);
130
131        }
132        else
133        {
134            this->setInvisible(false);
135
136            if((!this->isContinuous() && this->isImmediate()) || (!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
137            {
138                this->Pickupable::destroy();
139            }
140            else if(this->durationTimer_.isActive())
141            {
142                this->durationTimer_.pauseTimer();
143            }
144        }
145
146    }
147
148    /**
149    @brief
150        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
151    @return
152        A pointer to the Pawn, or NULL if the conversion failed.
153    */
154    Pawn* InvisiblePickup::carrierToPawnHelper(void)
155    {
156        PickupCarrier* carrier = this->getCarrier();
157        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
158
159        if(pawn == NULL)
160        {
161            COUT(1) << "Invalid PickupCarrier in InvisiblePickup." << std::endl;
162        }
163        return pawn;
164    }
165
166    /**
167    @brief
168        Creates a duplicate of the input OrxonoxClass.
169    @param item
170        A pointer to the Orxonox class.
171    */
172    void InvisiblePickup::clone(OrxonoxClass*& item)
173    {
174        if(item == NULL)
175            item = new InvisiblePickup(this);
176
177        SUPER(InvisiblePickup, clone, item);
178
179        InvisiblePickup* pickup = dynamic_cast<InvisiblePickup*>(item);
180        pickup->setDuration(this->getDuration());
181        pickup->initializeIdentifier();
182    }
183
184    /**
185    @brief
186        Sets the invisibility.
187    @param invisibility
188        The invisibility.
189    */
190    bool InvisiblePickup::setInvisible(bool invisibility)
191    {
192        Pawn* pawn = this->carrierToPawnHelper();
193        if(pawn == NULL)
194            return false;
195
196        pawn->setVisible(!invisibility);
197        pawn->setRadarVisibility(!invisibility);
198
199// Test to change Material at runtime!
200
201//      Ogre::MaterialPtr mat = this->mesh_.getEntity()->getSubEntity(0)->getMaterial();
202//      mat->setDiffuse(0.4, 0.3, 0.1, 0.1);
203//      mat->setAmbient(0.3, 0.7, 0.8);
204//      mat->setSpecular(0.5, 0.5, 0.5, 0.1);
205//      Ogre::SceneBlendType sbt = Ogre::SBT_ADD;
206//
207//      mat->setSceneBlending(sbt);
208
209        return true;
210    }
211
212    /**
213    @brief
214        Sets the time the InvisibilityPickup will last.
215    @param duration
216        The duration in seconds.
217    */
218    void InvisiblePickup::setDuration(float duration)
219    {
220        if(duration >= 0.0f)
221        {
222            this->duration_ = duration;
223        }
224        else
225        {
226            COUT(1) << "Invalid duration in InvisiblePickup." << std::endl;
227            this->duration_ = 0.0f;
228        }
229    }
230
231    /**
232    @brief
233        Helper method. Is called by the Timer as soon as it expires.
234    */
235    void InvisiblePickup::pickupTimerCallback(void)
236    {
237        this->setUsed(false);
238    }
239
240}
Note: See TracBrowser for help on using the repository browser.