Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

finished code for shrinking and growing

File size: 4.7 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    ShrinkPickup::ShrinkPickup(BaseObject* creator) : Pickup(creator)
54    {
55        RegisterObject(ShrinkPickup);
56
57        this->initialize();
58        shrinkFactor_ = 5.0;
59        duration_ = 5.0;
60        shrinkSpeed_ = 5.0;
61        isActive_ = false;
62        isTerminating_ = false;
63    }
64
65    ShrinkPickup::~ShrinkPickup()
66    {
67
68    }
69
70    void ShrinkPickup::initialize(void)
71    {
72        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
73    }
74
75    void ShrinkPickup::changedUsed(void)
76    {
77        SUPER(ShrinkPickup, changedUsed);
78
79        if(this->isUsed())
80        {
81            this->pawn = this->carrierToPawnHelper();
82            if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
83                this->Pickupable::destroy();
84
85            defaultScale_ = this->pawn->getScale3D();
86            defaultMass_ = this->pawn->getMass();
87
88            smallScale_ = defaultScale_ / shrinkFactor_;
89            smallMass_ = defaultMass_ / shrinkFactor_;
90
91            actualScale_ = defaultScale_;
92            actualMass_ = defaultMass_;
93
94            cameraPositions_ = this->pawn->getCameraPositions();
95            size_ = cameraPositions_.size();
96            isActive_ = true;
97            durationTimer.setTimer(duration_, false, createExecutor(createFunctor(&ShrinkPickup::terminate, this)));
98        }
99        else
100        {
101            this->Pickupable::destroy();
102        }
103    }
104
105    void ShrinkPickup::tick(float dt)
106    {
107        if(isActive_ == true && actualScale_ > smallScale_)
108        {
109            float factor_ = 1 + dt*shrinkSpeed_;
110
111            actualScale_ /= factor_;
112            actualMass_ /= factor_;
113
114            this->pawn->setScale3D(actualScale_);
115            this->pawn->setMass(actualMass_);
116
117            for(int index = 0; index < size_; index++)
118            {
119                CameraPosition* cameraPos_ = this->pawn->getCameraPosition(index);
120                if(cameraPos_ == NULL)
121                continue;
122                cameraPos_->setPosition(cameraPos_->getPosition()*factor_);
123            }
124        }
125        else isActive_ = false;
126
127        if(isTerminating_ == true && actualScale_ < defaultScale_)
128        {
129            float factor_ = 1 + dt*shrinkSpeed_;
130
131            actualScale_ *= factor_;
132            actualMass_ *= factor_;
133
134            this->pawn->setScale3D(actualScale_);
135            this->pawn->setMass(actualMass_);
136
137            for(int index = 0; index < size_; index++)
138            {
139                CameraPosition* cameraPos_ = this->pawn->getCameraPosition(index);
140                if(cameraPos_ == NULL)
141                continue;
142                cameraPos_->setPosition(cameraPos_->getPosition()/factor_);
143            }
144        }
145        else if(isTerminating_ == true)
146        setUsed(false);
147    }
148
149    void ShrinkPickup::terminate(void)
150    {
151        isActive_ = false;
152        isTerminating_ = true;
153    }
154
155    Pawn* ShrinkPickup::carrierToPawnHelper(void)
156    {
157        PickupCarrier* carrier = this->getCarrier();
158        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
159
160        return pawn;
161    }
162
163        /**
164    @brief
165        Creates a duplicate of the input OrxonoxClass.
166    @param item
167        A pointer to the Orxonox class.
168    */
169    void ShrinkPickup::clone(OrxonoxClass*& item)
170    {
171        if(item == NULL)
172            item = new ShrinkPickup(this);
173
174        SUPER(ShrinkPickup, clone, item);
175    }
176}
Note: See TracBrowser for help on using the repository browser.