Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp4/src/orxonox/objects/pickup/ModifierPickup.cc @ 3113

Last change on this file since 3113 was 3079, checked in by landauf, 16 years ago

Added forward declarations to OrxonoxPrereqs.h.

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