Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Documenting and cleanup.

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