Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 27, 2011, 10:45:15 AM (13 years ago)
Author:
dafrick
Message:

Cleaning up ShrinkPickup.

Location:
code/trunk/src/modules/pickup/items
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/pickup/items/ShrinkPickup.cc

    r8706 r8713  
    2121 *
    2222 *   Author:
    23  *      Damian 'Mozork' Frick
     23 *      Sandro Sgier
    2424 *   Co-authors:
    2525 *      ...
     
    4343#include "worldentities/pawns/Pawn.h"
    4444
    45 #include "weaponsystem/WeaponSlot.h"
    46 #include "weaponsystem/Weapon.h"
    4745#include "worldentities/CameraPosition.h"
    4846
     
    5149    CreateFactory(ShrinkPickup);
    5250
    53         /**
     51    /**
    5452    @brief
    5553        Constructor: Initializes the Pickup.
     
    6058
    6159        this->initialize();
     60    }
     61
     62    ShrinkPickup::~ShrinkPickup()
     63    {
     64
     65    }
     66
     67    void ShrinkPickup::initialize(void)
     68    {
     69        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
     70
    6271        this->shrinkFactor_ = 5.0f;
    63         this->shrinkSpeed_ = 5.0f;
     72        this->shrinkDuration_ = 5.0f;
    6473        this->duration_ = 5.0f;
     74
    6575        this->isActive_ = false;
     76        this->isShrinking_ = false;
    6677        this->isTerminating_ = false;
    6778
    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 
     79        this->timeRemainig_ = 0.0f;
     80        this->currentFactor_ = 1.0f;
    8281    }
    8382
     
    9796
    9897        stream.clear();
    99         stream << this->getShrinkSpeed();
     98        stream << this->getShrinkDuration();
    10099        std::string val3 = stream.str();
    101         std::string type3 = "shrinkSpeed";
     100        std::string type3 = "shrinkDuration";
    102101        this->pickupIdentifier_->addParameter(type3, val3);
    103102    }
     
    105104   /**
    106105    @brief
    107         Method for creating a ShrinkhPickup object through XML.
     106        Method for creating a ShrinkPickup object through XML.
    108107    */
    109108    void ShrinkPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
     
    113112        XMLPortParam(ShrinkPickup, "shrinkFactor", setShrinkFactor, getShrinkFactor, xmlelement, mode);
    114113        XMLPortParam(ShrinkPickup, "duration", setDuration, getDuration, xmlelement, mode);
    115         XMLPortParam(ShrinkPickup, "shrinkSpeed", setShrinkSpeed, getShrinkSpeed, xmlelement, mode);
     114        XMLPortParam(ShrinkPickup, "shrinkDuration", setShrinkDuration, getShrinkDuration, xmlelement, mode);
    116115
    117116        this->initializeIdentifier();
     
    120119    /**
    121120    @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).
     121        Prepares for shrinking.
    161122    */
    162123    void ShrinkPickup::changedUsed(void)
     
    166127        if(this->isUsed())
    167128        {
    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.
     129            Pawn* pawn = this->carrierToPawnHelper();
     130            if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
     131            {
    170132                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.
     133                return;
     134            }
     135
     136            this->currentFactor_ = 1.0f;
     137            this->timeRemainig_ = this->shrinkDuration_;
     138
     139            this->isActive_ = true; // Start shrinking now.
     140            this->isShrinking_ = true;
     141            this->durationTimer_.setTimer(this->duration_, false, createExecutor(createFunctor(&ShrinkPickup::terminate, this))); //Set timer for termination.
     142        }
     143        if(!this->isUsed() && this->isActive_)
     144             this->isTerminating_ = true;
     145    }
     146
     147    void ShrinkPickup::changedPickedUp(void)
     148    {
     149        SUPER(ShrinkPickup, changedPickedUp);
     150       
     151        if(!this->isPickedUp() && this->isActive_)
     152        {
     153            if(this->isShrinking_ || this->isTerminating_)
     154            {
     155                //TODO: Deploy particle effect.
     156                Pawn* pawn = this->carrierToPawnHelper();
     157                if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
     158                    return;
     159
     160                float factor = 1.0f/this->currentFactor_;
     161
     162                pawn->setScale3D(pawn->getScale3D()*factor);
     163                pawn->setMass(pawn->getMass()*factor);
     164
     165                // Iterate over all camera positions and inversely move the camera to create a shrinking sensation.
     166                const std::list< SmartPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions();
     167                int size = cameraPositions.size();
     168                for(int index = 0; index < size; index++)
     169                {
     170                    CameraPosition* cameraPos = pawn->getCameraPosition(index);
     171                    if(cameraPos == NULL)
     172                        continue;
     173                    cameraPos->setPosition(cameraPos->getPosition()/factor);
     174                }
     175                this->currentFactor_ = 1.0f;
     176                this->timeRemainig_ = this->shrinkDuration_;
     177                this->isActive_ = false;
     178                this->isShrinking_ = false;
     179                this->isTerminating_ = false;
     180            }
     181            else
     182            {
     183                //TODO: Deploy particle effect.
     184                Pawn* pawn = this->carrierToPawnHelper();
     185                if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
     186                    return;
     187
     188                pawn->setScale3D(pawn->getScale3D()*this->shrinkFactor_);
     189                pawn->setMass(pawn->getMass()*this->shrinkFactor_);
     190
     191                // Iterate over all camera positions and inversely move the camera to create a shrinking sensation.
     192                const std::list< SmartPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions();
     193                int size = cameraPositions.size();
     194                for(int index = 0; index < size; index++)
     195                {
     196                    CameraPosition* cameraPos = pawn->getCameraPosition(index);
     197                    if(cameraPos == NULL)
     198                        continue;
     199                    cameraPos->setPosition(cameraPos->getPosition()/this->shrinkFactor_);
     200                }
     201                this->currentFactor_ = 1.0f;
     202                this->timeRemainig_ = this->shrinkDuration_;
     203                this->isActive_ = false;
     204            }
    186205        }
    187206    }
     
    195214    void ShrinkPickup::tick(float dt)
    196215    {
    197         if(this->isActive_ == true && this->actualScale_ > this->smallScale_)    //if the ship has not reached the target scale, continue shrinking
     216        if(this->isActive_)
    198217        {
    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);
     218            if(this->isShrinking_)    // If the ship has not reached the target scale, continue shrinking
     219            {
     220                Pawn* pawn = this->carrierToPawnHelper();
     221                if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
     222                {
     223                    this->Pickupable::destroy();
     224                    return;
     225                }
     226
     227                this->timeRemainig_ -= dt;
     228
     229                // Calculate the scaling factor by which the initial size would have to be scaled to have the current scale.
     230                float currentFactor = std::max(1 - (1-std::max(this->timeRemainig_, 0.0f)/this->shrinkDuration_)*(1-1/this->shrinkFactor_), 1/this->shrinkFactor_);
     231                // Calculate the factor by which the previous size has to be scaled to be the current scale.
     232                float factor = currentFactor/this->currentFactor_;
     233                this->currentFactor_ = currentFactor;
     234
     235                // Stop shrinking if the desired size is reached.
     236                if(this->timeRemainig_ <= 0.0f)
     237                {
     238                    this->timeRemainig_ = this->shrinkDuration_; // Reset the time remaining for when we start to grow the ship again.
     239                    this->currentFactor_ = 1/this->shrinkFactor_;
     240                    this->isShrinking_ = false;
     241                }
     242
     243                pawn->setScale3D(pawn->getScale3D()*factor);
     244                pawn->setMass(pawn->getMass()*factor);
     245
     246                // Iterate over all camera positions and inversely move the camera to create a shrinking sensation.
     247                const std::list< SmartPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions();
     248                int size = cameraPositions.size();
     249                for(int index = 0; index < size; index++)
     250                {
     251                    CameraPosition* cameraPos = pawn->getCameraPosition(index);
     252                    if(cameraPos == NULL)
     253                        continue;
     254                    cameraPos->setPosition(cameraPos->getPosition()/factor);
     255                }
     256
     257            }
     258            else if(this->isTerminating_)    // Grow until the ship reaches its default scale.
     259            {
     260                Pawn* pawn = this->carrierToPawnHelper();
     261                if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
     262                    this->Pickupable::destroy();
     263
     264                this->timeRemainig_ -= dt;
     265
     266                // Calculate the scaling factor by which the initial size would have to be scaled to have the current scale.
     267                float currentFactor = std::min(1/this->shrinkFactor_ + (1-std::max(this->timeRemainig_, 0.0f)/this->shrinkDuration_)*(1-1/this->shrinkFactor_), 1.0f);
     268                // Calculate the factor by which the previous size has to be scaled to be the current scale.
     269                float factor = currentFactor/this->currentFactor_;
     270                this->currentFactor_ = currentFactor;
     271
     272                bool destroy = false;
     273               
     274                // Stop shrinking if the desired size is reached.
     275                if(this->timeRemainig_ <= 0.0f)
     276                {
     277                    this->timeRemainig_ = shrinkDuration_; // Reset the time remaining for when we start to grow the ship again.
     278                    this->currentFactor_ = 1.0f;
     279                    this->isTerminating_ = false;
     280                    this->isActive_ = false;
     281                    destroy = true;
     282                }
     283
     284                pawn->setScale3D(pawn->getScale3D()*factor);
     285                pawn->setMass(pawn->getMass()*factor);
     286
     287                // Iterate over all camera positions and inversely move the camera to create a shrinking sensation.
     288                const std::list< SmartPtr<CameraPosition> >& cameraPositions = pawn->getCameraPositions();
     289                int size = cameraPositions.size();
     290                for(int index = 0; index < size; index++)
     291                {
     292                    CameraPosition* cameraPos = pawn->getCameraPosition(index);
     293                    if(cameraPos == NULL)
     294                        continue;
     295                    cameraPos->setPosition(cameraPos->getPosition()/factor);
     296                }
     297
     298                if(destroy)
     299                    this->Pickupable::destroy();
    213300            }
    214301        }
    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 
    238302    }
    239303
     
    244308    void ShrinkPickup::terminate(void)
    245309    {
    246         this->isActive_ = false;
    247         this->isTerminating_ = true;
    248         setUsed(false);
     310        this->setUsed(false);
    249311    }
    250312
     
    272334        pickup->setShrinkFactor(this->getShrinkFactor());
    273335        pickup->setDuration(this->getDuration());
    274         pickup->setShrinkSpeed(this->getShrinkSpeed());
     336        pickup->setShrinkDuration(this->getShrinkDuration());
    275337
    276338        pickup->initializeIdentifier();
  • code/trunk/src/modules/pickup/items/ShrinkPickup.h

    r8706 r8713  
    2121 *
    2222 *   Author:
    23  *      Damian 'Mozork' Frick
     23 *      Sandro Sgier
    2424 *   Co-authors:
    2525 *      ...
     
    4848
    4949    /**
    50     @author
    51         Sandro Sgier
    52 
    53     @ingroup PickupItems
    54     */
    55 
    56     /**
    5750    @brief
    5851        The ShrinkPickup is a Pickupable that causes the pawn to shrink to a certain size for a certain time with a certain speed, all of them specified in the following variables:
    5952        - The @b shrinkFactor It determines how much the ship is going to shrink (e.g. the factor 2 would make the ship shrinking to half its size).
    60         - The @b duration Specifies how long the ship will keep small.
    61         - The @b shrinkSpeed Defines how fast the ship shrinks and grows.
     53        - The @b duration Specifies how long the ship will stay small.
     54        - The @b shrinkDuration Defines how fast the ship shrinks and grows in seconds.
    6255
    63 
    64         An example of a XML implementation of a HealthPickup would be:
     56        An example of a XML implementation of a ShrinkPickup would be:
    6557        @code
    66         <HealthPickup
     58        <ShrinkPickup
    6759            shrinkFactor = "5.0"
    6860            duration = "5.0"
     
    8072    {
    8173        public:
    82             ShrinkPickup(BaseObject* creator); //!< Constructor.
    83             virtual ~ShrinkPickup(); //!< Destructor.
    84             virtual void changedUsed(void); //!< Is called when the pickup has transited from used to unused or the other way around.
    85             virtual void clone(OrxonoxClass*& item); //!< Creates a duplicate of the input OrxonoxClass.
     74            ShrinkPickup(BaseObject* creator); // Constructor.
     75            virtual ~ShrinkPickup(); // Destructor.
     76
    8677            virtual void XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode);
     78            virtual void tick(float dt);
     79
     80            virtual void changedUsed(void); // Is called when the pickup has transited from used to unused or the other way around.
     81            virtual void changedPickedUp(void);
     82            virtual void clone(OrxonoxClass*& item); // Creates a duplicate of the input OrxonoxClass.
     83
     84            /**
     85            @brief Get the shrinking factor.
     86            @return Returns the shrinking factor,
     87            */
    8788            inline float getShrinkFactor(void) const
    8889                { return this->shrinkFactor_; }
     90            /**
     91            @brief Sets the shrinking factor.
     92            @param factor The factor, needs to greater than 1.
     93            */
     94            inline void setShrinkFactor(float factor)
     95                { if(factor <= 1.0f) { COUT(2) << "Invalid shrinking factor in ShrinkPickup. Ignoring.." << endl; return; } this->shrinkFactor_ = factor; }
     96            /**
     97            @brief Get the duration for which the ship remains shrunken.
     98            @return Returns the duration.
     99            */
    89100            inline float getDuration(void) const
    90101                { return this->duration_; }
    91             inline float getShrinkSpeed(void) const
    92                 { return this->shrinkSpeed_; }
    93             void setShrinkFactor(float factor);
    94             void setDuration(float duration);
    95             void setShrinkSpeed(float speed);
    96             void tick(float dt);
     102            /**
     103            @brief Set the duration for which the ship remains shrunken.
     104            @param duration The duration, needs to be non-negative.
     105            */
     106            inline void setDuration(float duration)
     107                { if(duration < 0.0f) { COUT(2) << "Invalid duration in ShrinkPickup. Ignoring.." << endl; return; } this->duration_ = duration; }
     108            /**
     109            @brief Get the shrink speed.
     110            @return Returns the shrink speed.
     111            */
     112            inline float getShrinkDuration(void) const
     113                { return this->shrinkDuration_; }
     114            /**
     115            @brief Set the shrink duration.
     116            @param speed The shrink duration, needs to be positive.
     117            */
     118            inline void setShrinkDuration(float speed)
     119                { if(speed <= 0.0f) { COUT(2) << "Invalid shrink duration in ShrinkPickup. Ignoring.." << endl; return; } this->shrinkDuration_ = speed; }
    97120
    98121        protected:
     
    101124        private:
    102125            void initialize(void);
    103             float duration_;            //!< determines how long the pickup will be active
    104             float shrinkFactor_;        //shrink factor of the space ship
    105             float shrinkSpeed_;         //speed of shrinking
    106             bool isActive_;             //true if ship is shrinking or small
    107             bool isTerminating_;        //true if ship is growing
    108             int size_;                  //number of camera positions
    109             std::list<SmartPtr<CameraPosition> > cameraPositions_;
    110             float defaultCameraPos_;    //all default, actual and small values...
    111             Ogre::Vector3 defaultScale_;
    112             Ogre::Vector3 actualScale_;
    113             Ogre::Vector3 smallScale_;
    114             float defaultMass_;
    115             float actualMass_;
    116             float smallMass_;           
     126
     127            float duration_;            //!< Determines how long the pickup will be active
     128            float shrinkFactor_;        //!< Shrink factor of the space ship
     129            float shrinkDuration_;      //!< Duration of shrinking
     130
     131            bool isActive_;             //!< True if ship is shrinking, small, or growing back.
     132            bool isShrinking_;          //!< True if ship is shrinking
     133            bool isTerminating_;        //!< True if ship is growing back to the original size
     134
     135            float currentFactor_;       //!< The shrink factor that is currently applied.
     136            float timeRemainig_;        //!< The remaining shrink time.
     137           
    117138            Pawn* carrierToPawnHelper(void);
    118             Pawn* pawn_;
    119139            Timer durationTimer_;
    120140            void terminate(void);
Note: See TracChangeset for help on using the changeset viewer.