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