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