Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8554 was 8554, checked in by dafrick, 13 years ago

Some cleanup. Ready for merge.

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