Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/modules/pickup/items/ShrinkPickup.cc @ 8632

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

Merging pickup branch into presentation branch.

File size: 7.9 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 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29
30/**
31    @file ShrinkPickup.cc
32    @brief Implementation of the HealthPickup class.
33*/
34
35
36#include "ShrinkPickup.h"
37
38#include <sstream>
39#include "core/CoreIncludes.h"
40#include "core/XMLPort.h"
41
42#include "pickup/PickupIdentifier.h"
43#include "worldentities/pawns/Pawn.h"
44
45#include "weaponsystem/WeaponSlot.h"
46#include "weaponsystem/Weapon.h"
47#include "worldentities/CameraPosition.h"
48
49namespace orxonox
50{
51    CreateFactory(ShrinkPickup);
52
53        /**
54    @brief
55        Constructor: Initializes the Pickup.
56    */
57    ShrinkPickup::ShrinkPickup(BaseObject* creator) : Pickup(creator)
58    {
59        RegisterObject(ShrinkPickup);
60
61        this->initialize();
62        this->shrinkFactor_ = 5.0f;
63        this->shrinkSpeed_ = 5.0f;
64        this->duration_ = 5.0f;
65        this->isActive_ = false;
66        this->isTerminating_ = false;
67
68        this->size_ = 0;
69        this->defaultCameraPos_ = 0.0f;
70        this->defaultScale_ = Vector3::UNIT_SCALE;
71        this->actualScale_ = Vector3::UNIT_SCALE;
72        this->smallScale_ = Vector3::UNIT_SCALE;
73        this->defaultMass_ = 1.0f;
74        this->actualMass_ = 1.0f;
75        this->smallMass_ = 1.0f;
76        this->pawn_ = NULL;
77    }
78
79    ShrinkPickup::~ShrinkPickup()
80    {
81
82    }
83
84    void ShrinkPickup::initializeIdentifier(void)
85    {
86        std::stringstream stream;
87        stream << this->getShrinkFactor();
88        std::string type1 = "shrinkFactor";
89        std::string val1 = stream.str();
90        this->pickupIdentifier_->addParameter(type1, val1);
91
92        stream.clear();
93        stream << this->getDuration();
94        std::string val2 = stream.str();
95        std::string type2 = "duration";
96        this->pickupIdentifier_->addParameter(type2, val2);
97
98        stream.clear();
99        stream << this->getShrinkSpeed();
100        std::string val3 = stream.str();
101        std::string type3 = "shrinkSpeed";
102        this->pickupIdentifier_->addParameter(type3, val3);
103    }
104
105   /**
106    @brief
107        Method for creating a ShrinkhPickup object through XML.
108    */
109    void ShrinkPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
110    {
111        SUPER(ShrinkPickup, XMLPort, xmlelement, mode);
112
113        XMLPortParam(ShrinkPickup, "shrinkFactor", setShrinkFactor, getShrinkFactor, xmlelement, mode);
114        XMLPortParam(ShrinkPickup, "duration", setDuration, getDuration, xmlelement, mode);
115        XMLPortParam(ShrinkPickup, "shrinkSpeed", setShrinkSpeed, getShrinkSpeed, xmlelement, mode);
116
117        this->initializeIdentifier();
118    }
119
120    /**
121    @brief
122        Sets the shrinking factor.
123    @param factor
124        The factor.
125    */
126    void ShrinkPickup::setShrinkFactor(float factor)
127    {
128        this->shrinkFactor_ = factor;
129    }
130
131    /**
132    @brief
133        Sets the duration.
134    @param duration
135        The duration.
136    */
137    void ShrinkPickup::setDuration(float duration)
138    {
139        this->duration_ = duration;
140    }
141
142    /**
143    @brief
144        Sets the shrinking speed.
145    @param speed
146        The speed.
147    */
148    void ShrinkPickup::setShrinkSpeed(float speed)
149    {
150        this->shrinkSpeed_ = speed;
151    }
152
153    void ShrinkPickup::initialize(void)
154    {
155        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
156    }
157
158    /**
159    @brief
160        Prepares for shrinking (collecting several informations).
161    */
162    void ShrinkPickup::changedUsed(void)
163    {
164        SUPER(ShrinkPickup, changedUsed);
165
166        if(this->isUsed())
167        {
168            this->pawn_ = this->carrierToPawnHelper();
169            if(this->pawn_ == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
170                this->Pickupable::destroy();
171
172            //Collect scaling information.
173            this->defaultScale_ = this->pawn_->getScale3D();
174            this->defaultMass_ = this->pawn_->getMass();
175
176            this->smallScale_ = this->defaultScale_ / this->shrinkFactor_;
177            this->smallMass_ = this->defaultMass_ / this->shrinkFactor_;
178
179            this->actualScale_ = this->defaultScale_;
180            this->actualMass_ = this->defaultMass_;
181
182            this->cameraPositions_ = this->pawn_->getCameraPositions();
183            this->size_ = this->cameraPositions_.size();
184            this->isActive_ = true;    //start shrinking now.
185            this->durationTimer_.setTimer(this->duration_, false, createExecutor(createFunctor(&ShrinkPickup::terminate, this)));    //Set timer for termination.
186        }
187    }
188
189    /**
190    @brief
191        Updates the scales of the ship.
192    @param dt
193        Time since last call.
194    */
195    void ShrinkPickup::tick(float dt)
196    {
197        if(this->isActive_ == true && this->actualScale_ > this->smallScale_)    //if the ship has not reached the target scale, continue shrinking
198        {
199            float factor = 1 + dt*this->shrinkSpeed_;
200
201            this->actualScale_ /= factor;
202            this->actualMass_ /= factor;
203
204            this->pawn_->setScale3D(this->actualScale_);
205            this->pawn_->setMass(this->actualMass_);
206
207            for(int index = 0; index < this->size_; index++)
208            {
209                CameraPosition* cameraPos = this->pawn_->getCameraPosition(index);
210                if(cameraPos == NULL)
211                continue;
212                cameraPos->setPosition(cameraPos->getPosition()*factor);
213            }
214        }
215        else this->isActive_ = false;
216
217        if(this->isTerminating_ == true && this->actualScale_ < this->defaultScale_)    //grow until the ship reaches its default scale.
218        {
219            float factor = 1 + dt*this->shrinkSpeed_;
220
221            this->actualScale_ *= factor;
222            this->actualMass_ *= factor;
223
224            this->pawn_->setScale3D(this->actualScale_);
225            this->pawn_->setMass(this->actualMass_);
226
227            for(int index = 0; index < this->size_; index++)
228            {
229                CameraPosition* cameraPos = this->pawn_->getCameraPosition(index);
230                if(cameraPos == NULL)
231                continue;
232                cameraPos->setPosition(cameraPos->getPosition()/factor);
233            }
234        }
235        else if(this->isTerminating_ == true)
236            this->Pickupable::destroy();
237
238    }
239
240    /**
241    @brief
242        Initializes the termination.
243    */
244    void ShrinkPickup::terminate(void)
245    {
246        this->isActive_ = false;
247        this->isTerminating_ = true;
248        setUsed(false);
249    }
250
251    Pawn* ShrinkPickup::carrierToPawnHelper(void)
252    {
253        PickupCarrier* carrier = this->getCarrier();
254        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
255
256        return pawn;
257    }
258
259    /**
260    @brief
261        Creates a duplicate of the input OrxonoxClass.
262    @param item
263        A pointer to the Orxonox class.
264    */
265    void ShrinkPickup::clone(OrxonoxClass*& item)
266    {
267        if(item == NULL)
268            item = new ShrinkPickup(this);
269
270        SUPER(ShrinkPickup, clone, item);
271        ShrinkPickup* pickup = dynamic_cast<ShrinkPickup*>(item);
272        pickup->setShrinkFactor(this->getShrinkFactor());
273        pickup->setDuration(this->getDuration());
274        pickup->setShrinkSpeed(this->getShrinkSpeed());
275
276        pickup->initializeIdentifier();
277    }
278}
Note: See TracBrowser for help on using the repository browser.