Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/modules/pickup/items/InvisiblePickup.cc @ 7090

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

Merged ppspickups4 branch.

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