Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8993 was 8645, checked in by landauf, 14 years ago

set svn:eol-style to native, removed svn:executable property. no code changes.

  • Property svn:eol-style set to native
File size: 7.9 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:
[8535]23 *      Damian 'Mozork' Frick
[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 "pickup/PickupIdentifier.h"
43#include "worldentities/pawns/Pawn.h"
44
[8381]45#include "weaponsystem/WeaponSlot.h"
46#include "weaponsystem/Weapon.h"
[8489]47#include "worldentities/CameraPosition.h"
[8381]48
[8375]49namespace orxonox
50{
51    CreateFactory(ShrinkPickup);
52
[8554]53        /**
[8534]54    @brief
55        Constructor: Initializes the Pickup.
56    */
[8489]57    ShrinkPickup::ShrinkPickup(BaseObject* creator) : Pickup(creator)
[8375]58    {
59        RegisterObject(ShrinkPickup);
60
61        this->initialize();
[8556]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;
[8375]77    }
78
79    ShrinkPickup::~ShrinkPickup()
80    {
81
82    }
83
[8534]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
[8554]107        Method for creating a ShrinkhPickup object through XML.
[8534]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
[8489]153    void ShrinkPickup::initialize(void)
154    {
155        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
156    }
[8375]157
[8554]158    /**
[8534]159    @brief
160        Prepares for shrinking (collecting several informations).
161    */
[8375]162    void ShrinkPickup::changedUsed(void)
[8489]163    {
164        SUPER(ShrinkPickup, changedUsed);
[8433]165
[8375]166        if(this->isUsed())
167        {
[8554]168            this->pawn_ = this->carrierToPawnHelper();
[8556]169            if(this->pawn_ == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[8375]170                this->Pickupable::destroy();
[8486]171
[8534]172            //Collect scaling information.
[8556]173            this->defaultScale_ = this->pawn_->getScale3D();
174            this->defaultMass_ = this->pawn_->getMass();
[8489]175
[8556]176            this->smallScale_ = this->defaultScale_ / this->shrinkFactor_;
177            this->smallMass_ = this->defaultMass_ / this->shrinkFactor_;
[8489]178
[8556]179            this->actualScale_ = this->defaultScale_;
180            this->actualMass_ = this->defaultMass_;
[8489]181
[8556]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.
[8375]186        }
[8489]187    }
[8375]188
[8554]189    /**
[8534]190    @brief
191        Updates the scales of the ship.
192    @param dt
193        Time since last call.
194    */
[8489]195    void ShrinkPickup::tick(float dt)
196    {
[8556]197        if(this->isActive_ == true && this->actualScale_ > this->smallScale_)    //if the ship has not reached the target scale, continue shrinking
[8489]198        {
[8556]199            float factor = 1 + dt*this->shrinkSpeed_;
[8433]200
[8556]201            this->actualScale_ /= factor;
202            this->actualMass_ /= factor;
[8433]203
[8556]204            this->pawn_->setScale3D(this->actualScale_);
205            this->pawn_->setMass(this->actualMass_);
[8433]206
[8556]207            for(int index = 0; index < this->size_; index++)
[8489]208            {
[8556]209                CameraPosition* cameraPos = this->pawn_->getCameraPosition(index);
210                if(cameraPos == NULL)
[8489]211                continue;
[8556]212                cameraPos->setPosition(cameraPos->getPosition()*factor);
[8489]213            }
214        }
[8556]215        else this->isActive_ = false;
[8381]216
[8556]217        if(this->isTerminating_ == true && this->actualScale_ < this->defaultScale_)    //grow until the ship reaches its default scale.
[8489]218        {
[8556]219            float factor = 1 + dt*this->shrinkSpeed_;
[8375]220
[8556]221            this->actualScale_ *= factor;
222            this->actualMass_ *= factor;
[8489]223
[8556]224            this->pawn_->setScale3D(this->actualScale_);
225            this->pawn_->setMass(this->actualMass_);
[8489]226
[8556]227            for(int index = 0; index < this->size_; index++)
[8489]228            {
[8556]229                CameraPosition* cameraPos = this->pawn_->getCameraPosition(index);
230                if(cameraPos == NULL)
[8489]231                continue;
[8556]232                cameraPos->setPosition(cameraPos->getPosition()/factor);
[8489]233            }
234        }
[8556]235        else if(this->isTerminating_ == true)
[8502]236            this->Pickupable::destroy();
237
[8489]238    }
239
[8554]240    /**
[8534]241    @brief
242        Initializes the termination.
243    */
[8489]244    void ShrinkPickup::terminate(void)
245    {
[8556]246        this->isActive_ = false;
247        this->isTerminating_ = true;
[8502]248        setUsed(false);
[8489]249    }
250
[8375]251    Pawn* ShrinkPickup::carrierToPawnHelper(void)
252    {
253        PickupCarrier* carrier = this->getCarrier();
254        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
255
256        return pawn;
257    }
258
[8554]259    /**
[8375]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);
[8534]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();
[8375]277    }
278}
Note: See TracBrowser for help on using the repository browser.