| 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 MetaPickup.cc | 
|---|
| 31 |     @brief Implementation of the MetaPickup class. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "core/CoreIncludes.h" | 
|---|
| 35 | #include "core/XMLPort.h" | 
|---|
| 36 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 37 | #include "interfaces/PickupCarrier.h" | 
|---|
| 38 | #include "pickup/PickupIdentifier.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "MetaPickup.h" | 
|---|
| 41 |  | 
|---|
| 42 | namespace orxonox { | 
|---|
| 43 |  | 
|---|
| 44 |     CreateFactory(MetaPickup); | 
|---|
| 45 |  | 
|---|
| 46 |     //! Setting the static variables to their values. | 
|---|
| 47 |     /*static*/ const std::string MetaPickup::metaTypeNone_s = "none"; | 
|---|
| 48 |     /*static*/ const std::string MetaPickup::metaTypeUse_s = "use"; | 
|---|
| 49 |     /*static*/ const std::string MetaPickup::metaTypeDrop_s = "drop"; | 
|---|
| 50 |     /*static*/ const std::string MetaPickup::metaTypeDestroy_s = "destroy"; | 
|---|
| 51 |     /*static*/ const std::string MetaPickup::metaTypeDestroyCarrier_s = "destroyCarrier"; | 
|---|
| 52 |  | 
|---|
| 53 |     /** | 
|---|
| 54 |     @brief | 
|---|
| 55 |         Constructor. Registers and initializes the object. | 
|---|
| 56 |     */ | 
|---|
| 57 |     MetaPickup::MetaPickup(BaseObject* creator) : Pickup(creator) | 
|---|
| 58 |     { | 
|---|
| 59 |         RegisterObject(MetaPickup); | 
|---|
| 60 |  | 
|---|
| 61 |         this->initialize(); | 
|---|
| 62 |     } | 
|---|
| 63 |  | 
|---|
| 64 |     /** | 
|---|
| 65 |     @brief | 
|---|
| 66 |         Destructor. | 
|---|
| 67 |     */ | 
|---|
| 68 |     MetaPickup::~MetaPickup() | 
|---|
| 69 |     { | 
|---|
| 70 |  | 
|---|
| 71 |     } | 
|---|
| 72 |  | 
|---|
| 73 |     /** | 
|---|
| 74 |     @brief | 
|---|
| 75 |         Initializes the object. | 
|---|
| 76 |     */ | 
|---|
| 77 |     void MetaPickup::initialize(void) | 
|---|
| 78 |     { | 
|---|
| 79 |         this->addTarget(ClassIdentifier<PickupCarrier>::getIdentifier()); | 
|---|
| 80 |  | 
|---|
| 81 |         this->setActivationTypeDirect(pickupActivationType::immediate); | 
|---|
| 82 |         this->setDurationTypeDirect(pickupDurationType::once); | 
|---|
| 83 |         this->metaType_ = pickupMetaType::none; | 
|---|
| 84 |     } | 
|---|
| 85 |  | 
|---|
| 86 |     /** | 
|---|
| 87 |     @brief | 
|---|
| 88 |         Helper method to initialize the PickupIdentifier. | 
|---|
| 89 |     */ | 
|---|
| 90 |     void MetaPickup::initializeIdentifier(void) | 
|---|
| 91 |     { | 
|---|
| 92 |         std::string val = this->getMetaType(); | 
|---|
| 93 |         std::string type = "metaType"; | 
|---|
| 94 |         this->pickupIdentifier_->addParameter(type, val); | 
|---|
| 95 |     } | 
|---|
| 96 |  | 
|---|
| 97 |     /** | 
|---|
| 98 |     @brief | 
|---|
| 99 |         Method for creating a MetaPickup object through XML. | 
|---|
| 100 |     */ | 
|---|
| 101 |     void MetaPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) | 
|---|
| 102 |     { | 
|---|
| 103 |         SUPER(MetaPickup, XMLPort, xmlelement, mode); | 
|---|
| 104 |  | 
|---|
| 105 |         XMLPortParam(MetaPickup, "metaType", setMetaType, getMetaType, xmlelement, mode); | 
|---|
| 106 |  | 
|---|
| 107 |         this->initializeIdentifier(); | 
|---|
| 108 |     } | 
|---|
| 109 |  | 
|---|
| 110 |     /** | 
|---|
| 111 |     @brief | 
|---|
| 112 |         Is called when the pickup has transited from used to unused or the other way around. | 
|---|
| 113 |         Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedUsed); to their changdeUsed method. | 
|---|
| 114 |     */ | 
|---|
| 115 |     void MetaPickup::changedUsed(void) | 
|---|
| 116 |     { | 
|---|
| 117 |         SUPER(MetaPickup, changedUsed); | 
|---|
| 118 |  | 
|---|
| 119 |         //! If the MetaPickup transited to used. | 
|---|
| 120 |         if(this->isUsed()) | 
|---|
| 121 |         { | 
|---|
| 122 |             PickupCarrier* carrier = this->getCarrier(); | 
|---|
| 123 |             if(this->getMetaTypeDirect() != pickupMetaType::none && carrier != NULL) | 
|---|
| 124 |             { | 
|---|
| 125 |                 if(this->getMetaTypeDirect() == pickupMetaType::destroyCarrier) | 
|---|
| 126 |                 { | 
|---|
| 127 |                     Pawn* pawn = orxonox_cast<Pawn*>(carrier); | 
|---|
| 128 |                     pawn->kill(); | 
|---|
| 129 |                     return; | 
|---|
| 130 |                 } | 
|---|
| 131 |                 std::set<Pickupable*> pickups = carrier->getPickups(); | 
|---|
| 132 |                 //! Set all Pickupables carried by the PickupCarrier either to used or drop them, depending on the meta type. | 
|---|
| 133 |                 for(std::set<Pickupable*>::iterator it = pickups.begin(); it != pickups.end(); it++) | 
|---|
| 134 |                 { | 
|---|
| 135 |                     Pickup* pickup = dynamic_cast<Pickup*>(*it); | 
|---|
| 136 |                     if(this->getMetaTypeDirect() == pickupMetaType::use) | 
|---|
| 137 |                     { | 
|---|
| 138 |                         if(pickup != NULL && pickup != this && pickup->isOnUse() && !pickup->isUsed()) | 
|---|
| 139 |                         { | 
|---|
| 140 |                             pickup->setUsed(true); | 
|---|
| 141 |                         } | 
|---|
| 142 |                     } | 
|---|
| 143 |                     if(this->getMetaTypeDirect() == pickupMetaType::drop) | 
|---|
| 144 |                     { | 
|---|
| 145 |                         if(pickup != NULL && pickup != this) | 
|---|
| 146 |                         { | 
|---|
| 147 |                             pickup->drop(); | 
|---|
| 148 |                         } | 
|---|
| 149 |                     } | 
|---|
| 150 |                     if(this->getMetaTypeDirect() == pickupMetaType::destroy) | 
|---|
| 151 |                     { | 
|---|
| 152 |                         if(pickup != NULL && pickup != this) | 
|---|
| 153 |                         { | 
|---|
| 154 |                             pickup->Pickupable::destroy(); | 
|---|
| 155 |                         } | 
|---|
| 156 |                     } | 
|---|
| 157 |                 } | 
|---|
| 158 |             } | 
|---|
| 159 |             this->Pickupable::destroy(); | 
|---|
| 160 |         } | 
|---|
| 161 |     } | 
|---|
| 162 |  | 
|---|
| 163 |     /** | 
|---|
| 164 |     @brief | 
|---|
| 165 |         Creates a duplicate of the input OrxonoxClass. | 
|---|
| 166 |     @param item | 
|---|
| 167 |         A pointer to the Orxonox class. | 
|---|
| 168 |     */ | 
|---|
| 169 |     void MetaPickup::clone(OrxonoxClass*& item) | 
|---|
| 170 |     { | 
|---|
| 171 |         if(item == NULL) | 
|---|
| 172 |             item = new MetaPickup(this); | 
|---|
| 173 |  | 
|---|
| 174 |         SUPER(MetaPickup, clone, item); | 
|---|
| 175 |  | 
|---|
| 176 |         MetaPickup* pickup = dynamic_cast<MetaPickup*>(item); | 
|---|
| 177 |         pickup->setMetaTypeDirect(this->getMetaTypeDirect()); | 
|---|
| 178 |  | 
|---|
| 179 |         pickup->initializeIdentifier(); | 
|---|
| 180 |     } | 
|---|
| 181 |  | 
|---|
| 182 |     /** | 
|---|
| 183 |     @brief | 
|---|
| 184 |         Get the meta type of this MetaPickup. | 
|---|
| 185 |     @return | 
|---|
| 186 |         Returns a string with the meta type of the MetaPickup. | 
|---|
| 187 |     */ | 
|---|
| 188 |     const std::string& MetaPickup::getMetaType(void) | 
|---|
| 189 |     { | 
|---|
| 190 |         switch(this->getMetaTypeDirect()) | 
|---|
| 191 |         { | 
|---|
| 192 |             case pickupMetaType::none: | 
|---|
| 193 |                 return MetaPickup::metaTypeNone_s; | 
|---|
| 194 |             case pickupMetaType::use: | 
|---|
| 195 |                 return MetaPickup::metaTypeUse_s; | 
|---|
| 196 |             case pickupMetaType::drop: | 
|---|
| 197 |                 return MetaPickup::metaTypeDrop_s; | 
|---|
| 198 |             case pickupMetaType::destroy: | 
|---|
| 199 |                 return MetaPickup::metaTypeDestroy_s; | 
|---|
| 200 |             case pickupMetaType::destroyCarrier: | 
|---|
| 201 |                 return MetaPickup::metaTypeDestroyCarrier_s; | 
|---|
| 202 |             default: | 
|---|
| 203 |                 return BLANKSTRING; | 
|---|
| 204 |         } | 
|---|
| 205 |     } | 
|---|
| 206 |  | 
|---|
| 207 |     /** | 
|---|
| 208 |     @brief | 
|---|
| 209 |         Set the meta type of this MetaPickup. | 
|---|
| 210 |     @param type | 
|---|
| 211 |         A string with the type to be set. | 
|---|
| 212 |     */ | 
|---|
| 213 |     void MetaPickup::setMetaType(const std::string& type) | 
|---|
| 214 |     { | 
|---|
| 215 |         if(type == MetaPickup::metaTypeNone_s) | 
|---|
| 216 |         { | 
|---|
| 217 |             this->setMetaTypeDirect(pickupMetaType::none); | 
|---|
| 218 |         } | 
|---|
| 219 |         else if(type == MetaPickup::metaTypeUse_s) | 
|---|
| 220 |         { | 
|---|
| 221 |             this->setMetaTypeDirect(pickupMetaType::use); | 
|---|
| 222 |         } | 
|---|
| 223 |         else if(type == MetaPickup::metaTypeDrop_s) | 
|---|
| 224 |         { | 
|---|
| 225 |             this->setMetaTypeDirect(pickupMetaType::drop); | 
|---|
| 226 |         } | 
|---|
| 227 |         else if(type == MetaPickup::metaTypeDestroy_s) | 
|---|
| 228 |         { | 
|---|
| 229 |             this->setMetaTypeDirect(pickupMetaType::destroy); | 
|---|
| 230 |         } | 
|---|
| 231 |         else if(type == MetaPickup::metaTypeDestroyCarrier_s) | 
|---|
| 232 |         { | 
|---|
| 233 |             this->setMetaTypeDirect(pickupMetaType::destroyCarrier); | 
|---|
| 234 |         } | 
|---|
| 235 |         else | 
|---|
| 236 |             COUT(2) << "Invalid metaType '" << type << "' in MetaPickup." << std::endl; | 
|---|
| 237 |     } | 
|---|
| 238 |  | 
|---|
| 239 | } | 
|---|