Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/pickup/ModifierPickup.cc @ 3181

Last change on this file since 3181 was 3180, checked in by rgrieder, 16 years ago

Cleanup in pickup and quest (almost only cleaned the header file sections).

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