Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9476 was 9348, checked in by landauf, 12 years ago

merged branch presentation2012merge back to trunk

  • Property svn:eol-style set to native
File size: 5.9 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
42#include "worldentities/pawns/Pawn.h"
43
44namespace orxonox
45{
[6684]46
[6641]47    CreateFactory(InvisiblePickup);
[7163]48
[6641]49    /**
50    @brief
51        Constructor. Registers the object and initializes the member variables.
52    */
53    InvisiblePickup::InvisiblePickup(BaseObject* creator) : Pickup(creator)
54    {
55        RegisterObject(InvisiblePickup);
[7163]56        this->initialize();
[6641]57    }
[7163]58
[6641]59    /**
60    @brief
61        Destructor.
62    */
63    InvisiblePickup::~InvisiblePickup()
[7163]64    {
[6641]65    }
[7163]66
[6641]67    /**
68    @brief
69    Initializes the member variables.
70    */
71    void InvisiblePickup::initialize(void)
72    {
[6708]73        this->duration_ = 0.0f;
[7541]74        // Defines who is allowed to pick up the pickup.
[6708]75        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
[6641]76    }
77
78    /**
79    @brief
80        Method for creating a HealthPickup object through XML.
81    */
82    void InvisiblePickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
83    {
[7163]84        SUPER(InvisiblePickup, XMLPort, xmlelement, mode);
[6708]85        XMLPortParam(InvisiblePickup, "duration", setDuration, getDuration, xmlelement, mode);
[6641]86    }
[7163]87
[6641]88    /**
89    @brief
90        Is called when the pickup has transited from used to unused or the other way around.
91    */
92    void InvisiblePickup::changedUsed(void)
93    {
94        SUPER(InvisiblePickup, changedUsed);
[7163]95
[7547]96        // If the pickup has transited to used.
[6708]97        if (this->isUsed())
98        {
[7547]99            // If its durationType is continuous, we set a Timer to be reminded, when the time has run out.
[7544]100            if(this->isContinuous())
[6755]101            {
[7541]102                if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
103                {
104                    this->durationTimer_.unpauseTimer();
105                }
106                else
107                {
108                    this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&InvisiblePickup::pickupTimerCallback, this)));
109                }
[6755]110            }
[7163]111
[6708]112            this->setInvisible(true);
[7163]113
[6708]114        }
115        else
116        {
117            this->setInvisible(false);
[7163]118
[7547]119            // 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]120            if((!this->isContinuous() && this->isImmediate()) || (this->isContinuous() && !this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration()))
[6755]121            {
[7163]122                this->Pickupable::destroy();
[6755]123            }
[7547]124            // We pause the Timer if the pickup is continuous and the duration is not yet exceeded,
[7546]125            else if(this->isContinuous() && this->durationTimer_.isActive())
[6755]126            {
[7208]127                this->durationTimer_.pauseTimer();
[6755]128            }
[6708]129        }
[6641]130    }
[7163]131
[6641]132    /**
133    @brief
134        Sets the invisibility.
[6708]135    @param invisibility
[6641]136        The invisibility.
137    */
138    bool InvisiblePickup::setInvisible(bool invisibility)
139    {
[6708]140        Pawn* pawn = this->carrierToPawnHelper();
141        if(pawn == NULL)
142            return false;
[7163]143
[6708]144        pawn->setVisible(!invisibility);
[8220]145        //TODO: Invisibility should imply radar invisibility as well, thus this should be solved in Pawn.
[7163]146        pawn->setRadarVisibility(!invisibility);
147
148// Test to change Material at runtime!
149
150//      Ogre::MaterialPtr mat = this->mesh_.getEntity()->getSubEntity(0)->getMaterial();
151//      mat->setDiffuse(0.4, 0.3, 0.1, 0.1);
152//      mat->setAmbient(0.3, 0.7, 0.8);
153//      mat->setSpecular(0.5, 0.5, 0.5, 0.1);
154//      Ogre::SceneBlendType sbt = Ogre::SBT_ADD;
155//
156//      mat->setSceneBlending(sbt);
157
[6708]158        return true;
[6641]159    }
[7163]160
[6641]161    /**
162    @brief
[7547]163        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
164    @return
165        A pointer to the Pawn, or NULL if the conversion failed.
166    */
167    Pawn* InvisiblePickup::carrierToPawnHelper(void)
168    {
169        PickupCarrier* carrier = this->getCarrier();
[9348]170        Pawn* pawn = orxonox_cast<Pawn*>(carrier);
[7547]171
172        if(pawn == NULL)
173        {
[8858]174            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in InvisiblePickup." << endl;
[7547]175        }
176        return pawn;
177    }
178
179    /**
180    @brief
[7544]181        Sets the time the InvisibilityPickup will last.
[6641]182    @param duration
[7544]183        The duration in seconds.
[6641]184    */
185    void InvisiblePickup::setDuration(float duration)
186    {
[6708]187        if(duration >= 0.0f)
188        {
189            this->duration_ = duration;
190        }
191        else
192        {
[8858]193            orxout(internal_error, context::pickups) << "Invalid duration in InvisiblePickup." << endl;
[6708]194            this->duration_ = 0.0f;
195        }
[6641]196    }
[7163]197
[7544]198    /**
199    @brief
200        Helper method. Is called by the Timer as soon as it expires.
201    */
[6708]202    void InvisiblePickup::pickupTimerCallback(void)
[6684]203    {
[6708]204        this->setUsed(false);
[6641]205    }
206
207}
Note: See TracBrowser for help on using the repository browser.