Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/pickup/ModifierPickup.cc @ 5925

Last change on this file since 5925 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

  • Property svn:eol-style set to native
File size: 7.4 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 *      Daniel 'Huty' Haggenmueller
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file
31    @brief Implementation of ModifierPickup (temporary(?) pickup for testing).
32*/
33
34#include "ModifierPickup.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "worldentities/pawns/Pawn.h"
39
40namespace orxonox
41{
42    CreateFactory(ModifierPickup);
43
44    /**
45        @brief Constructor. Registers the ModifierPickup.
46        @param creator Pointer to the object which created this item.
47    */
48    ModifierPickup::ModifierPickup(BaseObject* creator) : PassiveItem(creator)
49    {
50        RegisterObject(ModifierPickup);
51
52        this->duration_ = 0.0f;
53    }
54    //! Deconstructor.
55    ModifierPickup::~ModifierPickup()
56    {
57    }
58    /**
59        @brief Method for loading information from a level file.
60        @param element XMLElement from which to read the data.
61        @param mode XMLPort mode to use.
62    */
63    void ModifierPickup::XMLPort(Element& element, XMLPort::Mode mode)
64    {
65        SUPER(ModifierPickup, XMLPort, element, mode);
66
67        XMLPortParam(ModifierPickup, "duration", setDuration, getDuration, element, mode);
68
69        XMLPortParamTemplate(ModifierPickup, "damageAdd", setAdditiveDamage, getAdditiveDamage, element, mode, float);
70        XMLPortParamTemplate(ModifierPickup, "damageMulti", setMultiplicativeDamage, getMultiplicativeDamage, element, mode, float);
71
72        XMLPortParamTemplate(ModifierPickup, "accelerationAdd", setAdditiveAcceleration, getAdditiveAcceleration, element, mode, float);
73        XMLPortParamTemplate(ModifierPickup, "accelerationMulti", setMultiplicativeAcceleration, getMultiplicativeAcceleration, element, mode, float);
74    }
75    /**
76        @brief
77            Invoked when a pawn picks up the pickup.
78
79            Adds the modifiers to the pawn and sets a timer (if effect is limited)
80            if the pickup could be added to the pawn's PickupCollection.
81
82        @param pawn Pawn which picked up the pickup.
83        @return Returns whether the pickup was able to be added to the pawn.
84    */
85    bool ModifierPickup::pickedUp(Pawn* pawn)
86    {
87        if (this->addTo(pawn))
88        {
89            std::map<ModifierType::Value, float>::iterator it;
90
91            for (it = this->additiveModifiers_.begin(); it != this->additiveModifiers_.end(); it++)
92            {
93                pawn->getPickups().addAdditiveModifier((*it).first, (*it).second);
94            }
95
96            for (it = this->multiplicativeModifiers_.begin(); it != this->multiplicativeModifiers_.end(); it++)
97            {
98                pawn->getPickups().addMultiplicativeModifier((*it).first, (*it).second);
99            }
100
101            if (this->duration_ > 0.0f)
102            {
103                ExecutorMember<ModifierPickup>* executor = createExecutor(createFunctor(&ModifierPickup::timerCallback));
104                executor->setDefaultValues(pawn);
105                this->timer_.setTimer(this->duration_, false, this, executor);
106            }
107
108            return true;
109        }
110        return false;
111    }
112    /**
113        @brief
114            Invoked when a pawn drops the pickup.
115
116            Removes the modifiers from the pawn if the pickup
117            was successfully removed from it's PickupCollection.
118
119        @param pawn Pawn which dropped the pickup.
120        @return Returns whether the pickup could be removed.
121    */
122    bool ModifierPickup::dropped(Pawn* pawn)
123    {
124        if (this->removeFrom(pawn))
125        {
126            std::map<ModifierType::Value, float>::iterator it;
127
128            for (it = this->additiveModifiers_.begin(); it != this->additiveModifiers_.end(); it++)
129            {
130                pawn->getPickups().removeAdditiveModifier((*it).first, (*it).second);
131            }
132
133            for (it = this->multiplicativeModifiers_.begin(); it != this->multiplicativeModifiers_.end(); it++)
134            {
135                pawn->getPickups().removeMultiplicativeModifier((*it).first, (*it).second);
136            }
137
138            if (this->timer_.getRemainingTime() > 0.0f)
139                this->timer_.stopTimer();
140
141            delete this;
142
143            return true;
144        }
145        return false;
146    }
147    /**
148        @brief Invoked when the timer finished, calls dropped().
149    */
150    void ModifierPickup::timerCallback(Pawn* pawn)
151    {
152        if (!this->dropped(pawn))
153            COUT(2) << "Failed to remove modifier pickup after the timer ran out!" << std::endl;
154    }
155    /**
156        @brief Gets the additive modifier of a given type.
157        @param type ModifierType for which to return the modifier.
158        @return Returns the additive modifier for type (or 0 if not exists).
159    */
160    float ModifierPickup::getAdditiveModifier(ModifierType::Value type) const
161    {
162        std::map<ModifierType::Value, float>::const_iterator it = this->additiveModifiers_.find(type);
163        if (it != this->additiveModifiers_.end())
164            return (*it).second;
165        else
166            return 0.0f;
167    }
168    /**
169        @brief Gets the multiplicative modifier of a given type.
170        @param type ModifierType for which to return the modifier.
171        @return Returns the multiplicative modifier for type (or 1 if not exists).
172    */
173    float ModifierPickup::getMultiplicativeModifier(ModifierType::Value type) const
174    {
175        std::map<ModifierType::Value, float>::const_iterator it = this->multiplicativeModifiers_.find(type);
176        if (it != this->multiplicativeModifiers_.end())
177            return (*it).second;
178        else
179            return 1.0f;
180    }
181    /**
182        @brief Gets the additive modifier of a given type.
183        @param type ModifierType for which to return the modifier.
184        @param value The new additive modifier for type.
185    */
186    void ModifierPickup::setAdditiveModifier(ModifierType::Value type, float value)
187    {
188        if (this->additiveModifiers_.find(type) == this->additiveModifiers_.end())
189            this->additiveModifiers_.insert( std::pair<ModifierType::Value, float>(type, value) );
190        else
191            this->additiveModifiers_[type] = value;
192    }
193    /**
194        @brief Gets the multiplicative modifier of a given type.
195        @param type ModifierType for which to return the modifier.
196        @param value The new multiplicative modifier for type.
197    */
198    void ModifierPickup::setMultiplicativeModifier(ModifierType::Value type, float value)
199    {
200        if (this->multiplicativeModifiers_.find(type) == this->multiplicativeModifiers_.end())
201            this->multiplicativeModifiers_.insert( std::pair<ModifierType::Value, float>(type, value) );
202        else
203            this->multiplicativeModifiers_[type] = value;
204    }
205}
Note: See TracBrowser for help on using the repository browser.