| [6474] | 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 |  *      Damian 'Mozork' Frick | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 | */ | 
|---|
 | 28 |  | 
|---|
| [6540] | 29 | /** | 
|---|
 | 30 |     @file Pickup.cc | 
|---|
 | 31 |     @brief Implementation of the Pickup class. | 
|---|
 | 32 | */ | 
|---|
 | 33 |  | 
|---|
| [6474] | 34 | #include "Pickup.h" | 
|---|
 | 35 |  | 
|---|
 | 36 | #include "core/CoreIncludes.h" | 
|---|
| [6475] | 37 | #include "util/StringUtils.h" | 
|---|
 | 38 | #include "pickup/PickupIdentifier.h" | 
|---|
 | 39 | #include "DroppedPickup.h" | 
|---|
| [6474] | 40 |  | 
|---|
| [6709] | 41 | #include "tools/Timer.h" | 
|---|
 | 42 |  | 
|---|
| [6474] | 43 | namespace orxonox | 
|---|
 | 44 | { | 
|---|
| [6709] | 45 |  | 
|---|
| [6474] | 46 |     /*static*/ const std::string Pickup::activationTypeImmediate_s = "immediate"; | 
|---|
 | 47 |     /*static*/ const std::string Pickup::activationTypeOnUse_s = "onUse"; | 
|---|
 | 48 |     /*static*/ const std::string Pickup::durationTypeOnce_s = "once"; | 
|---|
 | 49 |     /*static*/ const std::string Pickup::durationTypeContinuous_s = "continuous"; | 
|---|
| [6709] | 50 |  | 
|---|
| [6474] | 51 |     Pickup::Pickup(BaseObject* creator) : BaseObject(creator) | 
|---|
 | 52 |     { | 
|---|
 | 53 |         RegisterObject(Pickup); | 
|---|
| [6709] | 54 |  | 
|---|
| [6475] | 55 |         this->initialize(); | 
|---|
| [6474] | 56 |     } | 
|---|
| [6709] | 57 |  | 
|---|
| [6474] | 58 |     Pickup::~Pickup() | 
|---|
 | 59 |     { | 
|---|
| [6709] | 60 |  | 
|---|
| [6474] | 61 |     } | 
|---|
| [6709] | 62 |  | 
|---|
| [6475] | 63 |     /** | 
|---|
 | 64 |     @brief | 
|---|
 | 65 |         Initializes the member variables. | 
|---|
 | 66 |     */ | 
|---|
 | 67 |     void Pickup::initialize(void) | 
|---|
 | 68 |     { | 
|---|
 | 69 |         this->activationType_ = pickupActivationType::immediate; | 
|---|
 | 70 |         this->durationType_ = pickupDurationType::once; | 
|---|
 | 71 |     } | 
|---|
| [6709] | 72 |  | 
|---|
| [6475] | 73 |     /** | 
|---|
 | 74 |     @brief | 
|---|
 | 75 |         Initializes the PickupIdentififer of this Pickup. | 
|---|
 | 76 |     */ | 
|---|
| [6474] | 77 |     void Pickup::initializeIdentifier(void) | 
|---|
| [6709] | 78 |     { | 
|---|
| [6474] | 79 |         std::string val1 = this->getActivationType(); | 
|---|
 | 80 |         std::string type1 = "activationType"; | 
|---|
| [6475] | 81 |         this->pickupIdentifier_->addParameter(type1, val1); | 
|---|
| [6709] | 82 |  | 
|---|
| [6474] | 83 |         std::string val2 = this->getDurationType(); | 
|---|
 | 84 |         std::string type2 = "durationType"; | 
|---|
| [6475] | 85 |         this->pickupIdentifier_->addParameter(type2, val2); | 
|---|
| [6474] | 86 |     } | 
|---|
| [6709] | 87 |  | 
|---|
| [6475] | 88 |     /** | 
|---|
 | 89 |     @brief | 
|---|
 | 90 |         Method for creating a Pickup object through XML. | 
|---|
 | 91 |     */ | 
|---|
| [6474] | 92 |     void Pickup::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 93 |     { | 
|---|
 | 94 |         SUPER(Pickup, XMLPort, xmlelement, mode); | 
|---|
 | 95 |  | 
|---|
 | 96 |         XMLPortParam(Pickup, "activationType", setActivationType, getActivationType, xmlelement, mode); | 
|---|
 | 97 |         XMLPortParam(Pickup, "durationType", setDurationType, getDurationType, xmlelement, mode); | 
|---|
| [6709] | 98 |  | 
|---|
| [6474] | 99 |         this->initializeIdentifier(); | 
|---|
 | 100 |     } | 
|---|
| [6709] | 101 |  | 
|---|
| [6474] | 102 |     /** | 
|---|
 | 103 |     @brief | 
|---|
 | 104 |         Get the activation type of the pickup. | 
|---|
| [6475] | 105 |     @return | 
|---|
 | 106 |         Returns a string containing the activation type. | 
|---|
| [6474] | 107 |     */ | 
|---|
 | 108 |     const std::string& Pickup::getActivationType(void) | 
|---|
 | 109 |     { | 
|---|
 | 110 |         switch(this->activationType_) | 
|---|
 | 111 |         { | 
|---|
 | 112 |             case pickupActivationType::immediate: | 
|---|
 | 113 |                 return activationTypeImmediate_s; | 
|---|
 | 114 |             case pickupActivationType::onUse: | 
|---|
 | 115 |                 return activationTypeOnUse_s; | 
|---|
 | 116 |             default: | 
|---|
| [6475] | 117 |                 return BLANKSTRING; | 
|---|
| [6474] | 118 |         } | 
|---|
 | 119 |     } | 
|---|
| [6709] | 120 |  | 
|---|
| [6474] | 121 |     /** | 
|---|
 | 122 |     @brief | 
|---|
 | 123 |         Get the duration type of the pickup. | 
|---|
| [6475] | 124 |     @return | 
|---|
 | 125 |         Returns a string containing the duration type. | 
|---|
| [6474] | 126 |     */ | 
|---|
 | 127 |     const std::string& Pickup::getDurationType(void) | 
|---|
 | 128 |     { | 
|---|
 | 129 |         switch(this->durationType_) | 
|---|
 | 130 |         { | 
|---|
 | 131 |             case pickupDurationType::once: | 
|---|
 | 132 |                 return durationTypeOnce_s; | 
|---|
 | 133 |             case pickupDurationType::continuous: | 
|---|
 | 134 |                 return durationTypeContinuous_s; | 
|---|
 | 135 |             default: | 
|---|
| [6475] | 136 |                 return BLANKSTRING; | 
|---|
| [6474] | 137 |         } | 
|---|
 | 138 |     } | 
|---|
| [6709] | 139 |  | 
|---|
| [6474] | 140 |     /** | 
|---|
 | 141 |     @brief | 
|---|
 | 142 |         Set the activation type of the Pickup. | 
|---|
 | 143 |     @param type | 
|---|
 | 144 |         The activation type of the Pickup as a string. | 
|---|
 | 145 |     */ | 
|---|
 | 146 |     void Pickup::setActivationType(const std::string& type) | 
|---|
 | 147 |     { | 
|---|
 | 148 |         if(type == activationTypeImmediate_s) | 
|---|
 | 149 |         { | 
|---|
 | 150 |             this->activationType_ = pickupActivationType::immediate; | 
|---|
 | 151 |         } | 
|---|
 | 152 |         else if(type == activationTypeOnUse_s) | 
|---|
 | 153 |         { | 
|---|
 | 154 |             this->activationType_ = pickupActivationType::onUse; | 
|---|
 | 155 |         } | 
|---|
 | 156 |         else | 
|---|
 | 157 |         { | 
|---|
 | 158 |             COUT(1) << "Invalid activationType in pickup." << std::endl; | 
|---|
 | 159 |         } | 
|---|
 | 160 |     } | 
|---|
| [6709] | 161 |  | 
|---|
| [6474] | 162 |     /** | 
|---|
 | 163 |     @brief | 
|---|
 | 164 |         Set the duration type of the Pickup. | 
|---|
 | 165 |     @param type | 
|---|
 | 166 |         The duration type of the Pickup as a string. | 
|---|
 | 167 |     */ | 
|---|
 | 168 |     void Pickup::setDurationType(const std::string& type) | 
|---|
 | 169 |     { | 
|---|
 | 170 |         if(type == durationTypeOnce_s) | 
|---|
 | 171 |         { | 
|---|
 | 172 |             this->durationType_ = pickupDurationType::once; | 
|---|
 | 173 |         } | 
|---|
 | 174 |         else if(type == durationTypeContinuous_s) | 
|---|
 | 175 |         { | 
|---|
 | 176 |             this->durationType_ = pickupDurationType::continuous; | 
|---|
 | 177 |         } | 
|---|
 | 178 |         else | 
|---|
 | 179 |         { | 
|---|
 | 180 |             COUT(1) << "Invalid durationType in pickup." << std::endl; | 
|---|
 | 181 |         } | 
|---|
 | 182 |     } | 
|---|
| [6709] | 183 |  | 
|---|
| [6474] | 184 |     /** | 
|---|
 | 185 |     @brief | 
|---|
| [6475] | 186 |         Should be called when the pickup has transited from picked up to dropped or the other way around. | 
|---|
| [6521] | 187 |         Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedPickedUp); to their changedPickedUp method. | 
|---|
| [6475] | 188 |     */ | 
|---|
| [6521] | 189 |     void Pickup::changedPickedUp(void) | 
|---|
| [6475] | 190 |     { | 
|---|
| [6521] | 191 |         SUPER(Pickup, changedPickedUp); | 
|---|
| [6709] | 192 |  | 
|---|
| [6475] | 193 |         //! Sets the Pickup to used if the Pickup has activation type 'immediate' and gets picked up. | 
|---|
| [6477] | 194 |         if(this->getCarrier() != NULL && this->isPickedUp() && this->isImmediate()) | 
|---|
| [6475] | 195 |         { | 
|---|
 | 196 |             this->setUsed(true); | 
|---|
 | 197 |         } | 
|---|
 | 198 |     } | 
|---|
| [6709] | 199 |  | 
|---|
| [6475] | 200 |     /** | 
|---|
 | 201 |     @brief | 
|---|
 | 202 |         Creates a duplicate of the Pickup. | 
|---|
| [6474] | 203 |     @return | 
|---|
 | 204 |         Returns the clone of this pickup as a pointer to a Pickupable. | 
|---|
 | 205 |     */ | 
|---|
| [6497] | 206 |     void Pickup::clone(OrxonoxClass*& item) | 
|---|
| [6474] | 207 |     { | 
|---|
 | 208 |         if(item == NULL) | 
|---|
 | 209 |             item = new Pickup(this); | 
|---|
| [6709] | 210 |  | 
|---|
| [6474] | 211 |         SUPER(Pickup, clone, item); | 
|---|
| [6709] | 212 |  | 
|---|
| [6474] | 213 |         Pickup* pickup = dynamic_cast<Pickup*>(item); | 
|---|
 | 214 |         pickup->setActivationTypeDirect(this->getActivationTypeDirect()); | 
|---|
 | 215 |         pickup->setDurationTypeDirect(this->getDurationTypeDirect()); | 
|---|
| [6709] | 216 |  | 
|---|
| [6474] | 217 |         pickup->initializeIdentifier(); | 
|---|
 | 218 |     } | 
|---|
| [6709] | 219 |  | 
|---|
| [6475] | 220 |     /** | 
|---|
 | 221 |     @brief | 
|---|
 | 222 |         Facilitates the creation of a PickupSpawner upon dropping of the Pickupable. | 
|---|
 | 223 |         This method must be implemented by any class directly inheriting from Pickupable. It is most easily done by just creating a new DroppedPickup, e.g.: | 
|---|
 | 224 |         DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position); | 
|---|
 | 225 |     @param position | 
|---|
 | 226 |         The position at which the PickupSpawner should be placed. | 
|---|
 | 227 |     @return | 
|---|
 | 228 |         Returns true if a spawner was created, false if not. | 
|---|
 | 229 |     */ | 
|---|
| [6540] | 230 |     bool Pickup::createSpawner(void) | 
|---|
| [6474] | 231 |     { | 
|---|
| [6540] | 232 |         new DroppedPickup(this, this, this->getCarrier()); | 
|---|
| [6475] | 233 |         return true; | 
|---|
| [6474] | 234 |     } | 
|---|
| [6709] | 235 |  | 
|---|
 | 236 |     /** | 
|---|
 | 237 |     @brief | 
|---|
 | 238 |         Starts the pickup duration timer. | 
|---|
 | 239 |         After the specified durationTime has expired the function pickupTimerCallback is called. | 
|---|
 | 240 |         pickupTimerCallback can be overloaded and thus the desired functionality can be implemented. | 
|---|
 | 241 |     @param durationTime | 
|---|
 | 242 |         The duration after which the expires and the callback function is called. | 
|---|
 | 243 |     @return | 
|---|
 | 244 |         Returns true if the pickup duration timer was started successfully, false if not. | 
|---|
 | 245 |     */ | 
|---|
 | 246 |     bool Pickup::startPickupTimer(float durationTime) | 
|---|
 | 247 |     { | 
|---|
 | 248 |         if (durationTime<=0) | 
|---|
 | 249 |         { | 
|---|
 | 250 |             COUT(1) << "Invalid durationTime in pickup." << std::endl; | 
|---|
 | 251 |             return false; | 
|---|
 | 252 |         } | 
|---|
 | 253 |         if (this->durationTimer_.isActive()) //!< Check if Timer is already running | 
|---|
 | 254 |         { | 
|---|
 | 255 |             COUT(1) << "Pickup durationTimer already in use." << std::endl; | 
|---|
 | 256 |             return false; | 
|---|
 | 257 |         } | 
|---|
 | 258 |         this->durationTimer_.setTimer(durationTime, false, createExecutor(createFunctor(&Pickup::pickupTimerCallback, this))); | 
|---|
 | 259 |         return true; | 
|---|
 | 260 |     } | 
|---|
| [6474] | 261 | } | 
|---|