Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8535 was 8535, checked in by ssgier, 13 years ago

corrected comments

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