Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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


File size: 4.4 KB
Line 
1#include "ShrinkPickup.h"
2
3#include <sstream>
4#include "core/CoreIncludes.h"
5#include "core/XMLPort.h"
6
7#include "pickup/PickupIdentifier.h"
8#include "worldentities/pawns/Pawn.h"
9
10#include "weaponsystem/WeaponSlot.h"
11#include "weaponsystem/Weapon.h"
12
13namespace orxonox
14{
15    CreateFactory(ShrinkPickup);
16
17        ShrinkPickup::ShrinkPickup(BaseObject* creator) : Pickup(creator)
18    {
19        RegisterObject(ShrinkPickup);
20
21        this->initialize();
22                shrinkFactor_ = 5.0;
23                duration_ = 5.0;
24                shrinkDelay_ = 1.0;
25                isActive_ = false;
26    }
27
28    ShrinkPickup::~ShrinkPickup()
29    {
30
31    }
32
33        void ShrinkPickup::initialize(void)
34        {
35                this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
36        }
37
38    void ShrinkPickup::changedUsed(void)
39        {
40                int i; 
41
42                SUPER(ShrinkPickup, changedUsed);
43
44        if(this->isUsed())
45        {
46            this->pawn = this->carrierToPawnHelper();
47            if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
48                this->Pickupable::destroy();
49       
50                        COUT(0) << "shrinking..." << endl;
51                        //this->pawn->setScale3D(this->pawn->getScale3D() / 2.0);
52                        std::set<WorldEntity*> set = this->pawn->getAttachedObjects();
53                       
54                        /*this->pawn->setScale3D(this->pawn->getScale3D() / 5.0);
55            this->pawn->setMass(this->pawn->getMass() / 5.0);
56
57            const std::list<SmartPtr<CameraPosition> > cameraPositions = this->pawn->getCameraPositions();
58            unsigned int size = cameraPositions.size();
59            for(unsigned int index = 0; index < size; index++)
60            {
61                CameraPosition* cameraPos = this->pawn->getCameraPosition(index);
62                if(cameraPos == NULL)
63                    continue;
64                cameraPos->setPosition(cameraPos->getPosition()*5.0);
65            }*/
66                       
67                        i = 0;
68                        for(std::set<WorldEntity*>::iterator it = set.begin(); it != set.end(); it++)
69                        {
70                                defaultScales_.push_back((*it)->getScale());
71                                actualScales_.push_back((*it)->getScale());
72                                defaultPositions_.push_back((*it)->getPosition());
73                                actualPositions_.push_back((*it)->getPosition());
74                                //(*it)->setScale((*it)->getScale() / 5.0);
75                                //(*it)->setPosition((*it)->getPosition() / 5.0);
76                        }
77                        size_ = defaultScales_.size();
78                        for(i = 0; i < size_; i++)
79                        {
80                                smallScales_.push_back(defaultScales_.at(i) / shrinkFactor_);
81                        }
82                        isActive_ = true;
83                        durationTimer.setTimer(10, false, createExecutor(createFunctor(&ShrinkPickup::terminate, this)));
84        }
85                else
86        {
87                        this->Pickupable::destroy();
88        }
89        }
90
91        void ShrinkPickup::tick(float dt)
92        {
93                double temp;
94                int i;
95                double k = dt / shrinkDelay_;
96                if(isActive_)
97                {
98                        for(i = 0; i < size_; i++)
99                        {
100                                temp = actualScales_.at(i);
101                                if(temp > smallScales_.at(i))
102                                {
103                                        actualScales_.erase(i)
104                                        actualScales_.insert(i, temp - (temp - smallScales_.at(i)) * k);
105                                }
106                                /*temp = actual;
107                                if(temp > smallScales[i])
108                                {
109                                        actualScales[i] -= (actualScales[i] - smallScales[i]) * k;
110                                }*/
111
112                        }
113
114                        i = 0;
115                       
116                        std::set<WorldEntity*> set = this->pawn->getAttachedObjects();
117                        for(std::set<WorldEntity*>::iterator it = set.begin(); it != set.end(); it++)
118                        {
119                                //defaultScales.push_back((*it)->getScale());
120                                //actualScales.push_back((*it)->getScale());
121                                //defaultPositions.push_back((*it)->getPosition());
122                                //actualPositions.push_back((*it)->getPosition());
123                                //(*it)->setScale((*it)->getScale() *0.99);
124                                (*it)->setScale(actualScales_.at(i));
125                                //(*it)->setPosition((*it)->getPosition() / 5.0);
126                        }
127                }
128        }
129
130        void ShrinkPickup::terminate(void)
131        {
132                //this->pawn->setScale3D(this->pawn->getScale3D() * 5.0);
133
134                std::set<WorldEntity*> set = this->pawn->getAttachedObjects();
135                for(std::set<WorldEntity*>::iterator it = set.begin(); it != set.end(); it++)
136                {
137                        (*it)->setScale((*it)->getScale() * 5.0);
138                }               
139                setUsed(false);
140        }
141
142    Pawn* ShrinkPickup::carrierToPawnHelper(void)
143    {
144        PickupCarrier* carrier = this->getCarrier();
145        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
146
147        if(pawn == NULL)
148        {
149            COUT(1) << "Invalid PickupCarrier in ShrinkPickup." << std::endl;
150        }
151
152        return pawn;
153    }
154
155        /**
156    @brief
157        Creates a duplicate of the input OrxonoxClass.
158    @param item
159        A pointer to the Orxonox class.
160    */
161    void ShrinkPickup::clone(OrxonoxClass*& item)
162    {
163        if(item == NULL)
164            item = new ShrinkPickup(this);
165
166        SUPER(ShrinkPickup, clone, item);
167    }
168}
Note: See TracBrowser for help on using the repository browser.