| 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 |  | 
|---|
| 37 | #include "interfaces/PickupCarrier.h" | 
|---|
| 38 | #include "worldentities/pawns/Pawn.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "MetaPickup.h" | 
|---|
| 41 |  | 
|---|
| 42 | namespace orxonox { | 
|---|
| 43 |  | 
|---|
| 44 |     RegisterClass(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(Context* context) : Pickup(context) | 
|---|
| 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->setDurationType(PickupDurationType::once); | 
|---|
| 82 |         this->metaType_ = PickupMetaType::none; | 
|---|
| 83 |     } | 
|---|
| 84 |  | 
|---|
| 85 |     /** | 
|---|
| 86 |     @brief | 
|---|
| 87 |         Method for creating a MetaPickup object through XML. | 
|---|
| 88 |     */ | 
|---|
| 89 |     void MetaPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) | 
|---|
| 90 |     { | 
|---|
| 91 |         SUPER(MetaPickup, XMLPort, xmlelement, mode); | 
|---|
| 92 |  | 
|---|
| 93 |         XMLPortParam(MetaPickup, "metaType", setMetaTypeAsString, getMetaTypeAsString, xmlelement, mode); | 
|---|
| 94 |     } | 
|---|
| 95 |  | 
|---|
| 96 |     /** | 
|---|
| 97 |     @brief | 
|---|
| 98 |         Is called when the pickup has transited from used to unused or the other way around. | 
|---|
| 99 |         Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedUsed); to their changdeUsed method. | 
|---|
| 100 |     */ | 
|---|
| 101 |     void MetaPickup::changedUsed(void) | 
|---|
| 102 |     { | 
|---|
| 103 |         SUPER(MetaPickup, changedUsed); | 
|---|
| 104 |  | 
|---|
| 105 |         // If the MetaPickup transited to used, and the metaType is not none. | 
|---|
| 106 |         if(this->isUsed() && this->metaType_ != PickupMetaType::none) | 
|---|
| 107 |         { | 
|---|
| 108 |             PickupCarrier* carrier = this->getCarrier(); | 
|---|
| 109 |             if(this->getMetaType() != PickupMetaType::none && carrier != nullptr) | 
|---|
| 110 |             { | 
|---|
| 111 |                 // If the metaType is destroyCarrier, then the PickupCarrier is destroyed. | 
|---|
| 112 |                 if(this->getMetaType() == PickupMetaType::destroyCarrier) | 
|---|
| 113 |                 { | 
|---|
| 114 |                     Pawn* pawn = orxonox_cast<Pawn*>(carrier); | 
|---|
| 115 |                     pawn->kill(); | 
|---|
| 116 |                     return; | 
|---|
| 117 |                 } | 
|---|
| 118 |                 std::set<Pickupable*> pickups = carrier->getPickups(); | 
|---|
| 119 |                 // Iterate over all Pickupables of the PickupCarrier. | 
|---|
| 120 |                 for(Pickupable* pickup : pickups) | 
|---|
| 121 |                 { | 
|---|
| 122 |                     if(pickup == nullptr || pickup == this) | 
|---|
| 123 |                         continue; | 
|---|
| 124 |  | 
|---|
| 125 |                     // If the metaType is use, then the Pickupable is set to used. | 
|---|
| 126 |                     if(this->getMetaType() == PickupMetaType::use && !pickup->isUsed()) | 
|---|
| 127 |                     { | 
|---|
| 128 |                         pickup->setUsed(true); | 
|---|
| 129 |                     } | 
|---|
| 130 |                     // If the metaType is drop, then the Pickupable is dropped. | 
|---|
| 131 |                     else if(this->getMetaType() == PickupMetaType::drop) | 
|---|
| 132 |                     { | 
|---|
| 133 |                         pickup->drop(); | 
|---|
| 134 |                     } | 
|---|
| 135 |                     // If the metaType is destroy, then the Pickupable is destroyed. | 
|---|
| 136 |                     else if(this->getMetaType() == PickupMetaType::destroy) | 
|---|
| 137 |                     { | 
|---|
| 138 |                         pickup->Pickupable::destroy(); | 
|---|
| 139 |                     } | 
|---|
| 140 |                 } | 
|---|
| 141 |             } | 
|---|
| 142 |             this->Pickupable::destroy(); | 
|---|
| 143 |         } | 
|---|
| 144 |     } | 
|---|
| 145 |  | 
|---|
| 146 |     /** | 
|---|
| 147 |     @brief | 
|---|
| 148 |         Get the meta type of this MetaPickup. | 
|---|
| 149 |     @return | 
|---|
| 150 |         Returns a string with the meta type of the MetaPickup. | 
|---|
| 151 |     */ | 
|---|
| 152 |     const std::string& MetaPickup::getMetaTypeAsString(void) const | 
|---|
| 153 |     { | 
|---|
| 154 |         switch(this->getMetaType()) | 
|---|
| 155 |         { | 
|---|
| 156 |             case PickupMetaType::none: | 
|---|
| 157 |                 return MetaPickup::metaTypeNone_s; | 
|---|
| 158 |             case PickupMetaType::use: | 
|---|
| 159 |                 return MetaPickup::metaTypeUse_s; | 
|---|
| 160 |             case PickupMetaType::drop: | 
|---|
| 161 |                 return MetaPickup::metaTypeDrop_s; | 
|---|
| 162 |             case PickupMetaType::destroy: | 
|---|
| 163 |                 return MetaPickup::metaTypeDestroy_s; | 
|---|
| 164 |             case PickupMetaType::destroyCarrier: | 
|---|
| 165 |                 return MetaPickup::metaTypeDestroyCarrier_s; | 
|---|
| 166 |             default: | 
|---|
| 167 |                 return BLANKSTRING; | 
|---|
| 168 |         } | 
|---|
| 169 |     } | 
|---|
| 170 |  | 
|---|
| 171 |     /** | 
|---|
| 172 |     @brief | 
|---|
| 173 |         Set the meta type of this MetaPickup. | 
|---|
| 174 |     @param type | 
|---|
| 175 |         A string with the type to be set. | 
|---|
| 176 |     */ | 
|---|
| 177 |     void MetaPickup::setMetaTypeAsString(const std::string& type) | 
|---|
| 178 |     { | 
|---|
| 179 |         if(type == MetaPickup::metaTypeNone_s) | 
|---|
| 180 |         { | 
|---|
| 181 |             this->setMetaType(PickupMetaType::none); | 
|---|
| 182 |         } | 
|---|
| 183 |         else if(type == MetaPickup::metaTypeUse_s) | 
|---|
| 184 |         { | 
|---|
| 185 |             this->setMetaType(PickupMetaType::use); | 
|---|
| 186 |         } | 
|---|
| 187 |         else if(type == MetaPickup::metaTypeDrop_s) | 
|---|
| 188 |         { | 
|---|
| 189 |             this->setMetaType(PickupMetaType::drop); | 
|---|
| 190 |         } | 
|---|
| 191 |         else if(type == MetaPickup::metaTypeDestroy_s) | 
|---|
| 192 |         { | 
|---|
| 193 |             this->setMetaType(PickupMetaType::destroy); | 
|---|
| 194 |         } | 
|---|
| 195 |         else if(type == MetaPickup::metaTypeDestroyCarrier_s) | 
|---|
| 196 |         { | 
|---|
| 197 |             this->setMetaType(PickupMetaType::destroyCarrier); | 
|---|
| 198 |         } | 
|---|
| 199 |         else | 
|---|
| 200 |             orxout(internal_warning, context::pickups) << "Invalid metaType '" << type << "' in MetaPickup." << endl; | 
|---|
| 201 |     } | 
|---|
| 202 |  | 
|---|
| 203 | } | 
|---|