[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 | { |
---|
[7533] | 127 | this->Pickupable::destroy(); //TODO: Needed? |
---|
[7163] | 128 | Pawn* pawn = orxonox_cast<Pawn*>(carrier); |
---|
| 129 | pawn->kill(); |
---|
| 130 | return; |
---|
| 131 | } |
---|
[6512] | 132 | std::set<Pickupable*> pickups = carrier->getPickups(); |
---|
[7163] | 133 | //! Set all Pickupables carried by the PickupCarrier either to used or drop them, depending on the meta type. |
---|
[6512] | 134 | for(std::set<Pickupable*>::iterator it = pickups.begin(); it != pickups.end(); it++) |
---|
| 135 | { |
---|
| 136 | Pickup* pickup = dynamic_cast<Pickup*>(*it); |
---|
[6518] | 137 | if(this->getMetaTypeDirect() == pickupMetaType::use) |
---|
[6512] | 138 | { |
---|
| 139 | if(pickup != NULL && pickup != this && pickup->isOnUse() && !pickup->isUsed()) |
---|
| 140 | { |
---|
| 141 | pickup->setUsed(true); |
---|
| 142 | } |
---|
| 143 | } |
---|
[6518] | 144 | if(this->getMetaTypeDirect() == pickupMetaType::drop) |
---|
[6512] | 145 | { |
---|
| 146 | if(pickup != NULL && pickup != this) |
---|
| 147 | { |
---|
[7163] | 148 | pickup->drop(); |
---|
[6512] | 149 | } |
---|
| 150 | } |
---|
[7163] | 151 | if(this->getMetaTypeDirect() == pickupMetaType::destroy) |
---|
| 152 | { |
---|
| 153 | if(pickup != NULL && pickup != this) |
---|
| 154 | { |
---|
| 155 | pickup->Pickupable::destroy(); |
---|
| 156 | } |
---|
| 157 | } |
---|
[6512] | 158 | } |
---|
| 159 | } |
---|
[7163] | 160 | this->Pickupable::destroy(); |
---|
[6512] | 161 | } |
---|
| 162 | } |
---|
[7163] | 163 | |
---|
[6538] | 164 | /** |
---|
| 165 | @brief |
---|
| 166 | Creates a duplicate of the input OrxonoxClass. |
---|
| 167 | @param item |
---|
| 168 | A pointer to the Orxonox class. |
---|
| 169 | */ |
---|
| 170 | void MetaPickup::clone(OrxonoxClass*& item) |
---|
| 171 | { |
---|
| 172 | if(item == NULL) |
---|
| 173 | item = new MetaPickup(this); |
---|
[7163] | 174 | |
---|
[6538] | 175 | SUPER(MetaPickup, clone, item); |
---|
[7163] | 176 | |
---|
[6538] | 177 | MetaPickup* pickup = dynamic_cast<MetaPickup*>(item); |
---|
| 178 | pickup->setMetaTypeDirect(this->getMetaTypeDirect()); |
---|
[7163] | 179 | |
---|
[6538] | 180 | pickup->initializeIdentifier(); |
---|
| 181 | } |
---|
[7163] | 182 | |
---|
[6538] | 183 | /** |
---|
| 184 | @brief |
---|
| 185 | Get the meta type of this MetaPickup. |
---|
| 186 | @return |
---|
| 187 | Returns a string with the meta type of the MetaPickup. |
---|
| 188 | */ |
---|
[6518] | 189 | const std::string& MetaPickup::getMetaType(void) |
---|
[6512] | 190 | { |
---|
[6518] | 191 | switch(this->getMetaTypeDirect()) |
---|
[6512] | 192 | { |
---|
[6518] | 193 | case pickupMetaType::none: |
---|
| 194 | return MetaPickup::metaTypeNone_s; |
---|
| 195 | case pickupMetaType::use: |
---|
| 196 | return MetaPickup::metaTypeUse_s; |
---|
| 197 | case pickupMetaType::drop: |
---|
| 198 | return MetaPickup::metaTypeDrop_s; |
---|
[7163] | 199 | case pickupMetaType::destroy: |
---|
| 200 | return MetaPickup::metaTypeDestroy_s; |
---|
| 201 | case pickupMetaType::destroyCarrier: |
---|
| 202 | return MetaPickup::metaTypeDestroyCarrier_s; |
---|
[6512] | 203 | default: |
---|
| 204 | return BLANKSTRING; |
---|
| 205 | } |
---|
| 206 | } |
---|
[7163] | 207 | |
---|
[6538] | 208 | /** |
---|
| 209 | @brief |
---|
| 210 | Set the meta type of this MetaPickup. |
---|
| 211 | @param type |
---|
| 212 | A string with the type to be set. |
---|
| 213 | */ |
---|
[6518] | 214 | void MetaPickup::setMetaType(const std::string& type) |
---|
[6512] | 215 | { |
---|
[6518] | 216 | if(type == MetaPickup::metaTypeNone_s) |
---|
[6512] | 217 | { |
---|
[6518] | 218 | this->setMetaTypeDirect(pickupMetaType::none); |
---|
[6512] | 219 | } |
---|
[6518] | 220 | else if(type == MetaPickup::metaTypeUse_s) |
---|
[6512] | 221 | { |
---|
[6518] | 222 | this->setMetaTypeDirect(pickupMetaType::use); |
---|
[6512] | 223 | } |
---|
[6518] | 224 | else if(type == MetaPickup::metaTypeDrop_s) |
---|
[6512] | 225 | { |
---|
[6518] | 226 | this->setMetaTypeDirect(pickupMetaType::drop); |
---|
[6512] | 227 | } |
---|
[7163] | 228 | else if(type == MetaPickup::metaTypeDestroy_s) |
---|
| 229 | { |
---|
| 230 | this->setMetaTypeDirect(pickupMetaType::destroy); |
---|
| 231 | } |
---|
| 232 | else if(type == MetaPickup::metaTypeDestroyCarrier_s) |
---|
| 233 | { |
---|
| 234 | this->setMetaTypeDirect(pickupMetaType::destroyCarrier); |
---|
| 235 | } |
---|
| 236 | else |
---|
| 237 | COUT(2) << "Invalid metaType '" << type << "' in MetaPickup." << std::endl; |
---|
[6512] | 238 | } |
---|
[7163] | 239 | |
---|
[6512] | 240 | } |
---|