Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/items/ShrinkPickup.cc @ 11481

Last change on this file since 11481 was 11083, checked in by muemart, 9 years ago

Fix some clang-tidy warnings.
Also, Serialise.h was doing some C-style casts that ended up being const casts. I moved those const casts as close to the source as possible and changed the loadAndIncrease functions to not do that.

  • Property svn:eol-style set to native
File size: 12.1 KB
RevLine 
[8489]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:
[8713]23 *      Sandro Sgier
[8489]24 *   Co-authors:
25 *      ...
26 *
27 */
28
29
30/**
31    @file ShrinkPickup.cc
32    @brief Implementation of the HealthPickup class.
33*/
34
35
[8375]36#include "ShrinkPickup.h"
37
38#include <sstream>
39#include "core/CoreIncludes.h"
40#include "core/XMLPort.h"
41
42#include "worldentities/pawns/Pawn.h"
43
[8489]44#include "worldentities/CameraPosition.h"
[8381]45
[8375]46namespace orxonox
47{
[9667]48    RegisterClass(ShrinkPickup);
[8375]49
[8713]50    /**
[8534]51    @brief
52        Constructor: Initializes the Pickup.
53    */
[9667]54    ShrinkPickup::ShrinkPickup(Context* context) : Pickup(context)
[8375]55    {
56        RegisterObject(ShrinkPickup);
57
58        this->initialize();
[8713]59    }
60
61    ShrinkPickup::~ShrinkPickup()
62    {
63
64    }
65
66    void ShrinkPickup::initialize(void)
67    {
68        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
69
[8556]70        this->shrinkFactor_ = 5.0f;
[8713]71        this->shrinkDuration_ = 5.0f;
[8556]72        this->duration_ = 5.0f;
[8713]73
[8556]74        this->isActive_ = false;
[8713]75        this->isShrinking_ = false;
[8556]76        this->isTerminating_ = false;
77
[8713]78        this->timeRemainig_ = 0.0f;
79        this->currentFactor_ = 1.0f;
[8375]80    }
81
[8534]82   /**
83    @brief
[8713]84        Method for creating a ShrinkPickup object through XML.
[8534]85    */
86    void ShrinkPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
87    {
88        SUPER(ShrinkPickup, XMLPort, xmlelement, mode);
89
90        XMLPortParam(ShrinkPickup, "shrinkFactor", setShrinkFactor, getShrinkFactor, xmlelement, mode);
91        XMLPortParam(ShrinkPickup, "duration", setDuration, getDuration, xmlelement, mode);
[8713]92        XMLPortParam(ShrinkPickup, "shrinkDuration", setShrinkDuration, getShrinkDuration, xmlelement, mode);
[9348]93    }
[8534]94
[9348]95    /**
96    @brief Sets the shrinking factor.
97    @param factor The factor, needs to greater than 1.
98    */
99    void ShrinkPickup::setShrinkFactor(float factor)
100    {
101        if(factor <= 1.0f)
102        {
103            orxout(internal_warning, context::pickups) << "Invalid shrinking factor in ShrinkPickup. Ignoring.." << endl;
104            return;
105        }
106        this->shrinkFactor_ = factor;
[8534]107    }
108
109    /**
[9348]110    @brief Set the duration for which the ship remains shrunken.
111    @param duration The duration, needs to be non-negative.
112    */
113    void ShrinkPickup::setDuration(float duration)
114    {
115        if(duration < 0.0f)
116        {
117            orxout(internal_warning, context::pickups) << "Invalid duration in ShrinkPickup. Ignoring.." << endl;
118            return;
119        }
120        this->duration_ = duration;
121    }
122
123    /**
124    @brief Set the shrink duration.
125    @param speed The shrink duration, needs to be positive.
126    */
127    void ShrinkPickup::setShrinkDuration(float speed)
128    {
129        if(speed <= 0.0f)
130        {
131            orxout(internal_warning, context::pickups) << "Invalid shrink duration in ShrinkPickup. Ignoring.." << endl;
132            return;
133        }
134        this->shrinkDuration_ = speed;
135    }
136
137    /**
[8534]138    @brief
[8713]139        Prepares for shrinking.
[8534]140    */
[8713]141    void ShrinkPickup::changedUsed(void)
[8534]142    {
[8713]143        SUPER(ShrinkPickup, changedUsed);
[8534]144
[8713]145        if(this->isUsed())
146        {
147            Pawn* pawn = this->carrierToPawnHelper();
[11071]148            if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[8713]149            {
150                this->Pickupable::destroy();
151                return;
152            }
[8534]153
[8713]154            this->currentFactor_ = 1.0f;
155            this->timeRemainig_ = this->shrinkDuration_;
[8534]156
[8713]157            this->isActive_ = true; // Start shrinking now.
158            this->isShrinking_ = true;
159            this->durationTimer_.setTimer(this->duration_, false, createExecutor(createFunctor(&ShrinkPickup::terminate, this))); //Set timer for termination.
160        }
161        if(!this->isUsed() && this->isActive_)
162             this->isTerminating_ = true;
[8489]163    }
[8375]164
[8713]165    void ShrinkPickup::changedPickedUp(void)
[8489]166    {
[8713]167        SUPER(ShrinkPickup, changedPickedUp);
[9348]168
[8713]169        if(!this->isPickedUp() && this->isActive_)
[8375]170        {
[8713]171            if(this->isShrinking_ || this->isTerminating_)
172            {
173                //TODO: Deploy particle effect.
174                Pawn* pawn = this->carrierToPawnHelper();
[11071]175                if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[8713]176                    return;
[8486]177
[8713]178                float factor = 1.0f/this->currentFactor_;
[8489]179
[8713]180                pawn->setScale3D(pawn->getScale3D()*factor);
181                pawn->setMass(pawn->getMass()*factor);
[8489]182
[8713]183                // Iterate over all camera positions and inversely move the camera to create a shrinking sensation.
[11071]184                const std::list<StrongPtr<CameraPosition>>& cameraPositions = pawn->getCameraPositions();
[8713]185                int size = cameraPositions.size();
186                for(int index = 0; index < size; index++)
187                {
188                    CameraPosition* cameraPos = pawn->getCameraPosition(index);
[11071]189                    if(cameraPos == nullptr)
[8713]190                        continue;
191                    cameraPos->setPosition(cameraPos->getPosition()/factor);
192                }
193                this->currentFactor_ = 1.0f;
194                this->timeRemainig_ = this->shrinkDuration_;
195                this->isActive_ = false;
196                this->isShrinking_ = false;
197                this->isTerminating_ = false;
198            }
199            else
200            {
201                //TODO: Deploy particle effect.
202                Pawn* pawn = this->carrierToPawnHelper();
[11071]203                if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[8713]204                    return;
[8489]205
[8713]206                pawn->setScale3D(pawn->getScale3D()*this->shrinkFactor_);
207                pawn->setMass(pawn->getMass()*this->shrinkFactor_);
208
209                // Iterate over all camera positions and inversely move the camera to create a shrinking sensation.
[11071]210                const std::list<StrongPtr<CameraPosition>>& cameraPositions = pawn->getCameraPositions();
[8713]211                int size = cameraPositions.size();
212                for(int index = 0; index < size; index++)
213                {
214                    CameraPosition* cameraPos = pawn->getCameraPosition(index);
[11071]215                    if(cameraPos == nullptr)
[8713]216                        continue;
217                    cameraPos->setPosition(cameraPos->getPosition()/this->shrinkFactor_);
218                }
219                this->currentFactor_ = 1.0f;
220                this->timeRemainig_ = this->shrinkDuration_;
221                this->isActive_ = false;
222            }
[8375]223        }
[8489]224    }
[8375]225
[8554]226    /**
[8534]227    @brief
228        Updates the scales of the ship.
229    @param dt
230        Time since last call.
231    */
[8489]232    void ShrinkPickup::tick(float dt)
233    {
[8713]234        if(this->isActive_)
[8489]235        {
[8713]236            if(this->isShrinking_)    // If the ship has not reached the target scale, continue shrinking
237            {
238                Pawn* pawn = this->carrierToPawnHelper();
[11071]239                if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[8713]240                {
241                    this->Pickupable::destroy();
242                    return;
243                }
[8433]244
[8713]245                this->timeRemainig_ -= dt;
[8433]246
[8713]247                // Calculate the scaling factor by which the initial size would have to be scaled to have the current scale.
248                float currentFactor = std::max(1 - (1-std::max(this->timeRemainig_, 0.0f)/this->shrinkDuration_)*(1-1/this->shrinkFactor_), 1/this->shrinkFactor_);
249                // Calculate the factor by which the previous size has to be scaled to be the current scale.
250                float factor = currentFactor/this->currentFactor_;
251                this->currentFactor_ = currentFactor;
[8433]252
[8713]253                // Stop shrinking if the desired size is reached.
254                if(this->timeRemainig_ <= 0.0f)
255                {
256                    this->timeRemainig_ = this->shrinkDuration_; // Reset the time remaining for when we start to grow the ship again.
257                    this->currentFactor_ = 1/this->shrinkFactor_;
258                    this->isShrinking_ = false;
259                }
260
261                pawn->setScale3D(pawn->getScale3D()*factor);
262                pawn->setMass(pawn->getMass()*factor);
263
264                // Iterate over all camera positions and inversely move the camera to create a shrinking sensation.
[11071]265                const std::list<StrongPtr<CameraPosition>>& cameraPositions = pawn->getCameraPositions();
[8713]266                int size = cameraPositions.size();
267                for(int index = 0; index < size; index++)
268                {
269                    CameraPosition* cameraPos = pawn->getCameraPosition(index);
[11071]270                    if(cameraPos == nullptr)
[8713]271                        continue;
272                    cameraPos->setPosition(cameraPos->getPosition()/factor);
273                }
274
275            }
276            else if(this->isTerminating_)    // Grow until the ship reaches its default scale.
[8489]277            {
[8713]278                Pawn* pawn = this->carrierToPawnHelper();
[11083]279                if (pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
280                {
[8713]281                    this->Pickupable::destroy();
[11083]282                    return;
283                }
[8381]284
[8713]285                this->timeRemainig_ -= dt;
[8375]286
[8713]287                // Calculate the scaling factor by which the initial size would have to be scaled to have the current scale.
288                float currentFactor = std::min(1/this->shrinkFactor_ + (1-std::max(this->timeRemainig_, 0.0f)/this->shrinkDuration_)*(1-1/this->shrinkFactor_), 1.0f);
289                // Calculate the factor by which the previous size has to be scaled to be the current scale.
290                float factor = currentFactor/this->currentFactor_;
291                this->currentFactor_ = currentFactor;
[8489]292
[8713]293                bool destroy = false;
[9348]294
[8713]295                // Stop shrinking if the desired size is reached.
296                if(this->timeRemainig_ <= 0.0f)
297                {
298                    this->timeRemainig_ = shrinkDuration_; // Reset the time remaining for when we start to grow the ship again.
299                    this->currentFactor_ = 1.0f;
300                    this->isTerminating_ = false;
301                    this->isActive_ = false;
302                    destroy = true;
303                }
[8489]304
[8713]305                pawn->setScale3D(pawn->getScale3D()*factor);
306                pawn->setMass(pawn->getMass()*factor);
307
308                // Iterate over all camera positions and inversely move the camera to create a shrinking sensation.
[11071]309                const std::list<StrongPtr<CameraPosition>>& cameraPositions = pawn->getCameraPositions();
[8713]310                int size = cameraPositions.size();
311                for(int index = 0; index < size; index++)
312                {
313                    CameraPosition* cameraPos = pawn->getCameraPosition(index);
[11071]314                    if(cameraPos == nullptr)
[8713]315                        continue;
316                    cameraPos->setPosition(cameraPos->getPosition()/factor);
317                }
318
319                if(destroy)
320                    this->Pickupable::destroy();
[8489]321            }
322        }
323    }
324
[8554]325    /**
[8534]326    @brief
327        Initializes the termination.
328    */
[8489]329    void ShrinkPickup::terminate(void)
330    {
[8713]331        this->setUsed(false);
[8489]332    }
333
[8375]334    Pawn* ShrinkPickup::carrierToPawnHelper(void)
335    {
336        PickupCarrier* carrier = this->getCarrier();
[9348]337        Pawn* pawn = orxonox_cast<Pawn*>(carrier);
[8375]338
339        return pawn;
340    }
341}
Note: See TracBrowser for help on using the repository browser.