Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Removed Timer from Pickup, as this is no his core functionality.

  • 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>
37#include <OgreEntity.h>
38#include <OgreAnimationState.h>
39
40#include "util/StringUtils.h"
[6641]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{
[6684]49
[6641]50    CreateFactory(InvisiblePickup);
[7163]51
[6641]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);
[6708]59        //! Defines who is allowed to pick up the pickup.
[7163]60        this->initialize();
[6641]61    }
[7163]62
[6641]63    /**
64    @brief
65        Destructor.
66    */
67    InvisiblePickup::~InvisiblePickup()
[7163]68    {
[6641]69    }
[7163]70
71
[6641]72    void InvisiblePickup::initializeIdentifier(void)
73    {
[6708]74        std::stringstream stream;
75        stream << this->getDuration();
76        std::string type1 = "duration";
77        std::string val1 = stream.str();
78        this->pickupIdentifier_->addParameter(type1, val1);
[6641]79    }
[7163]80
[6641]81    /**
82    @brief
83    Initializes the member variables.
84    */
85    void InvisiblePickup::initialize(void)
86    {
[6708]87        this->duration_ = 0.0f;
88        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
[6641]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    {
[7163]97        SUPER(InvisiblePickup, XMLPort, xmlelement, mode);
[6708]98        XMLPortParam(InvisiblePickup, "duration", setDuration, getDuration, xmlelement, mode);
[7163]99
[6708]100        this->initializeIdentifier();
[6641]101    }
[7163]102
[6641]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);
[7163]110
[6641]111        //! If the pickup is not picked up nothing must be done.
112        if(!this->isPickedUp())
113            return;
[7163]114
[6708]115        if (this->isUsed())
116        {
[7208]117            if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() > 0.0f)
[6755]118            {
[7208]119                this->durationTimer_.unpauseTimer();
[6755]120            }
121            else
122            {
[7208]123                this->durationTimer_.setTimer(this->getDuration(), false, createExecutor(createFunctor(&InvisiblePickup::pickupTimerCallback, this)));
[6755]124            }
[7163]125
[6708]126            this->setInvisible(true);
[7163]127
[6708]128        }
129        else
130        {
131            this->setInvisible(false);
[7163]132
[7208]133            if(!this->durationTimer_.isActive() && this->durationTimer_.getRemainingTime() == this->getDuration())
[6755]134            {
[7163]135                this->Pickupable::destroy();
[6755]136            }
137            else
138            {
[7208]139                this->durationTimer_.pauseTimer();
[6755]140            }
[6708]141        }
[7163]142
[6641]143    }
[7163]144
[6641]145    /**
146    @brief
147        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
148    @return
149        A pointer to the Pawn, or NULL if the conversion failed.
150    */
151    Pawn* InvisiblePickup::carrierToPawnHelper(void)
152    {
153        PickupCarrier* carrier = this->getCarrier();
154        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
[7163]155
[6641]156        if(pawn == NULL)
157        {
158            COUT(1) << "Invalid PickupCarrier in InvisiblePickup." << std::endl;
159        }
160        return pawn;
161    }
[7163]162
[6641]163    /**
164    @brief
165        Creates a duplicate of the input OrxonoxClass.
166    @param item
167        A pointer to the Orxonox class.
168    */
169    void InvisiblePickup::clone(OrxonoxClass*& item)
170    {
171        if(item == NULL)
172            item = new InvisiblePickup(this);
[7163]173
[6641]174        SUPER(InvisiblePickup, clone, item);
[7163]175
[6708]176        InvisiblePickup* pickup = dynamic_cast<InvisiblePickup*>(item);
177        pickup->setDuration(this->getDuration());
178        pickup->initializeIdentifier();
[6641]179    }
[7163]180
[6641]181    /**
182    @brief
183        Sets the invisibility.
[6708]184    @param invisibility
[6641]185        The invisibility.
186    */
187    bool InvisiblePickup::setInvisible(bool invisibility)
188    {
[6708]189        Pawn* pawn = this->carrierToPawnHelper();
190        if(pawn == NULL)
191            return false;
[7163]192
[6708]193        pawn->setVisible(!invisibility);
[7163]194        pawn->setRadarVisibility(!invisibility);
195
196// Test to change Material at runtime!
197
198//      Ogre::MaterialPtr mat = this->mesh_.getEntity()->getSubEntity(0)->getMaterial();
199//      mat->setDiffuse(0.4, 0.3, 0.1, 0.1);
200//      mat->setAmbient(0.3, 0.7, 0.8);
201//      mat->setSpecular(0.5, 0.5, 0.5, 0.1);
202//      Ogre::SceneBlendType sbt = Ogre::SBT_ADD;
203//
204//      mat->setSceneBlending(sbt);
205
[6708]206        return true;
[6641]207    }
[7163]208
[6641]209    /**
210    @brief
[6708]211        Sets the duration.
[6641]212    @param duration
[6708]213        The duration
[6641]214    */
215    void InvisiblePickup::setDuration(float duration)
216    {
[6708]217        if(duration >= 0.0f)
218        {
219            this->duration_ = duration;
220        }
221        else
222        {
223            COUT(1) << "Invalid duration in InvisiblePickup." << std::endl;
224            this->duration_ = 0.0f;
225        }
[6641]226    }
[7163]227
[6708]228    void InvisiblePickup::pickupTimerCallback(void)
[6684]229    {
[6708]230        this->setUsed(false);
[6641]231    }
232
233}
Note: See TracBrowser for help on using the repository browser.