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