[6512] | 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 | |
---|
[6519] | 29 | /** |
---|
[6538] | 30 | @file MetaPickup.cc |
---|
[6519] | 31 | @brief Implementation of the MetaPickup class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[6512] | 34 | #include "core/CoreIncludes.h" |
---|
| 35 | #include "core/XMLPort.h" |
---|
[7163] | 36 | #include "worldentities/pawns/Pawn.h" |
---|
[6512] | 37 | #include "interfaces/PickupCarrier.h" |
---|
| 38 | #include "pickup/PickupIdentifier.h" |
---|
| 39 | |
---|
[6518] | 40 | #include "MetaPickup.h" |
---|
[6512] | 41 | |
---|
| 42 | namespace orxonox { |
---|
[7163] | 43 | |
---|
[6518] | 44 | CreateFactory(MetaPickup); |
---|
[7163] | 45 | |
---|
[6538] | 46 | //! Setting the static variables to their values. |
---|
[6518] | 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"; |
---|
[7163] | 50 | /*static*/ const std::string MetaPickup::metaTypeDestroy_s = "destroy"; |
---|
| 51 | /*static*/ const std::string MetaPickup::metaTypeDestroyCarrier_s = "destroyCarrier"; |
---|
| 52 | |
---|
[6519] | 53 | /** |
---|
| 54 | @brief |
---|
[6538] | 55 | Constructor. Registers and initializes the object. |
---|
[6519] | 56 | */ |
---|
[6518] | 57 | MetaPickup::MetaPickup(BaseObject* creator) : Pickup(creator) |
---|
[6512] | 58 | { |
---|
[6518] | 59 | RegisterObject(MetaPickup); |
---|
[7163] | 60 | |
---|
[6538] | 61 | this->initialize(); |
---|
[6512] | 62 | } |
---|
[7163] | 63 | |
---|
[6538] | 64 | /** |
---|
| 65 | @brief |
---|
| 66 | Destructor. |
---|
| 67 | */ |
---|
[6518] | 68 | MetaPickup::~MetaPickup() |
---|
[6512] | 69 | { |
---|
[7163] | 70 | |
---|
[6512] | 71 | } |
---|
[7163] | 72 | |
---|
[6538] | 73 | /** |
---|
| 74 | @brief |
---|
| 75 | Initializes the object. |
---|
| 76 | */ |
---|
| 77 | void MetaPickup::initialize(void) |
---|
| 78 | { |
---|
| 79 | this->addTarget(ClassIdentifier<PickupCarrier>::getIdentifier()); |
---|
[7163] | 80 | |
---|
[6538] | 81 | this->setActivationTypeDirect(pickupActivationType::immediate); |
---|
| 82 | this->setDurationTypeDirect(pickupDurationType::once); |
---|
| 83 | this->metaType_ = pickupMetaType::none; |
---|
| 84 | } |
---|
[7163] | 85 | |
---|
[6538] | 86 | /** |
---|
| 87 | @brief |
---|
| 88 | Helper method to initialize the PickupIdentifier. |
---|
| 89 | */ |
---|
[6518] | 90 | void MetaPickup::initializeIdentifier(void) |
---|
[6512] | 91 | { |
---|
[6518] | 92 | std::string val = this->getMetaType(); |
---|
| 93 | std::string type = "metaType"; |
---|
[6512] | 94 | this->pickupIdentifier_->addParameter(type, val); |
---|
| 95 | } |
---|
[7163] | 96 | |
---|
[6538] | 97 | /** |
---|
| 98 | @brief |
---|
| 99 | Method for creating a MetaPickup object through XML. |
---|
| 100 | */ |
---|
[6518] | 101 | void MetaPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
[6512] | 102 | { |
---|
[6518] | 103 | SUPER(MetaPickup, XMLPort, xmlelement, mode); |
---|
[7163] | 104 | |
---|
[6518] | 105 | XMLPortParam(MetaPickup, "metaType", setMetaType, getMetaType, xmlelement, mode); |
---|
[7163] | 106 | |
---|
[6512] | 107 | this->initializeIdentifier(); |
---|
| 108 | } |
---|
[7163] | 109 | |
---|
[6538] | 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 | */ |
---|
[6518] | 115 | void MetaPickup::changedUsed(void) |
---|
[6512] | 116 | { |
---|
[6518] | 117 | SUPER(MetaPickup, changedUsed); |
---|
[7163] | 118 | |
---|
[6538] | 119 | //! If the MetaPickup transited to used. |
---|
[6512] | 120 | if(this->isUsed()) |
---|
| 121 | { |
---|
| 122 | PickupCarrier* carrier = this->getCarrier(); |
---|
[6518] | 123 | if(this->getMetaTypeDirect() != pickupMetaType::none && carrier != NULL) |
---|
[6512] | 124 | { |
---|
[7163] | 125 | if(this->getMetaTypeDirect() == pickupMetaType::destroyCarrier) |
---|
| 126 | { |
---|
| 127 | Pawn* pawn = orxonox_cast<Pawn*>(carrier); |
---|
| 128 | pawn->kill(); |
---|
| 129 | return; |
---|
| 130 | } |
---|
[6512] | 131 | std::set<Pickupable*> pickups = carrier->getPickups(); |
---|
[7163] | 132 | //! Set all Pickupables carried by the PickupCarrier either to used or drop them, depending on the meta type. |
---|
[6512] | 133 | for(std::set<Pickupable*>::iterator it = pickups.begin(); it != pickups.end(); it++) |
---|
| 134 | { |
---|
| 135 | Pickup* pickup = dynamic_cast<Pickup*>(*it); |
---|
[6518] | 136 | if(this->getMetaTypeDirect() == pickupMetaType::use) |
---|
[6512] | 137 | { |
---|
| 138 | if(pickup != NULL && pickup != this && pickup->isOnUse() && !pickup->isUsed()) |
---|
| 139 | { |
---|
| 140 | pickup->setUsed(true); |
---|
| 141 | } |
---|
| 142 | } |
---|
[6518] | 143 | if(this->getMetaTypeDirect() == pickupMetaType::drop) |
---|
[6512] | 144 | { |
---|
| 145 | if(pickup != NULL && pickup != this) |
---|
| 146 | { |
---|
[7163] | 147 | pickup->drop(); |
---|
[6512] | 148 | } |
---|
| 149 | } |
---|
[7163] | 150 | if(this->getMetaTypeDirect() == pickupMetaType::destroy) |
---|
| 151 | { |
---|
| 152 | if(pickup != NULL && pickup != this) |
---|
| 153 | { |
---|
| 154 | pickup->Pickupable::destroy(); |
---|
| 155 | } |
---|
| 156 | } |
---|
[6512] | 157 | } |
---|
| 158 | } |
---|
[7163] | 159 | this->Pickupable::destroy(); |
---|
[6512] | 160 | } |
---|
| 161 | } |
---|
[7163] | 162 | |
---|
[6538] | 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); |
---|
[7163] | 173 | |
---|
[6538] | 174 | SUPER(MetaPickup, clone, item); |
---|
[7163] | 175 | |
---|
[6538] | 176 | MetaPickup* pickup = dynamic_cast<MetaPickup*>(item); |
---|
| 177 | pickup->setMetaTypeDirect(this->getMetaTypeDirect()); |
---|
[7163] | 178 | |
---|
[6538] | 179 | pickup->initializeIdentifier(); |
---|
| 180 | } |
---|
[7163] | 181 | |
---|
[6538] | 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 | */ |
---|
[6518] | 188 | const std::string& MetaPickup::getMetaType(void) |
---|
[6512] | 189 | { |
---|
[6518] | 190 | switch(this->getMetaTypeDirect()) |
---|
[6512] | 191 | { |
---|
[6518] | 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; |
---|
[7163] | 198 | case pickupMetaType::destroy: |
---|
| 199 | return MetaPickup::metaTypeDestroy_s; |
---|
| 200 | case pickupMetaType::destroyCarrier: |
---|
| 201 | return MetaPickup::metaTypeDestroyCarrier_s; |
---|
[6512] | 202 | default: |
---|
| 203 | return BLANKSTRING; |
---|
| 204 | } |
---|
| 205 | } |
---|
[7163] | 206 | |
---|
[6538] | 207 | /** |
---|
| 208 | @brief |
---|
| 209 | Set the meta type of this MetaPickup. |
---|
| 210 | @param type |
---|
| 211 | A string with the type to be set. |
---|
| 212 | */ |
---|
[6518] | 213 | void MetaPickup::setMetaType(const std::string& type) |
---|
[6512] | 214 | { |
---|
[6518] | 215 | if(type == MetaPickup::metaTypeNone_s) |
---|
[6512] | 216 | { |
---|
[6518] | 217 | this->setMetaTypeDirect(pickupMetaType::none); |
---|
[6512] | 218 | } |
---|
[6518] | 219 | else if(type == MetaPickup::metaTypeUse_s) |
---|
[6512] | 220 | { |
---|
[6518] | 221 | this->setMetaTypeDirect(pickupMetaType::use); |
---|
[6512] | 222 | } |
---|
[6518] | 223 | else if(type == MetaPickup::metaTypeDrop_s) |
---|
[6512] | 224 | { |
---|
[6518] | 225 | this->setMetaTypeDirect(pickupMetaType::drop); |
---|
[6512] | 226 | } |
---|
[7163] | 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; |
---|
[6512] | 237 | } |
---|
[7163] | 238 | |
---|
[6512] | 239 | } |
---|