[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" |
---|
| 36 | #include "interfaces/PickupCarrier.h" |
---|
| 37 | #include "pickup/PickupIdentifier.h" |
---|
| 38 | |
---|
[6518] | 39 | #include "MetaPickup.h" |
---|
[6512] | 40 | |
---|
| 41 | namespace orxonox { |
---|
| 42 | |
---|
[6518] | 43 | CreateFactory(MetaPickup); |
---|
[6512] | 44 | |
---|
[6538] | 45 | //! Setting the static variables to their values. |
---|
[6518] | 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"; |
---|
[6512] | 49 | |
---|
[6519] | 50 | /** |
---|
| 51 | @brief |
---|
[6538] | 52 | Constructor. Registers and initializes the object. |
---|
[6519] | 53 | */ |
---|
[6518] | 54 | MetaPickup::MetaPickup(BaseObject* creator) : Pickup(creator) |
---|
[6512] | 55 | { |
---|
[6518] | 56 | RegisterObject(MetaPickup); |
---|
[6512] | 57 | |
---|
[6538] | 58 | this->initialize(); |
---|
[6512] | 59 | } |
---|
| 60 | |
---|
[6538] | 61 | /** |
---|
| 62 | @brief |
---|
| 63 | Destructor. |
---|
| 64 | */ |
---|
[6518] | 65 | MetaPickup::~MetaPickup() |
---|
[6512] | 66 | { |
---|
| 67 | |
---|
| 68 | } |
---|
| 69 | |
---|
[6538] | 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 | */ |
---|
[6518] | 87 | void MetaPickup::initializeIdentifier(void) |
---|
[6512] | 88 | { |
---|
[6518] | 89 | std::string val = this->getMetaType(); |
---|
| 90 | std::string type = "metaType"; |
---|
[6512] | 91 | this->pickupIdentifier_->addParameter(type, val); |
---|
| 92 | } |
---|
| 93 | |
---|
[6538] | 94 | /** |
---|
| 95 | @brief |
---|
| 96 | Method for creating a MetaPickup object through XML. |
---|
| 97 | */ |
---|
[6518] | 98 | void MetaPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
[6512] | 99 | { |
---|
[6518] | 100 | SUPER(MetaPickup, XMLPort, xmlelement, mode); |
---|
[6512] | 101 | |
---|
[6518] | 102 | XMLPortParam(MetaPickup, "metaType", setMetaType, getMetaType, xmlelement, mode); |
---|
[6512] | 103 | |
---|
| 104 | this->initializeIdentifier(); |
---|
| 105 | } |
---|
| 106 | |
---|
[6538] | 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 | */ |
---|
[6518] | 112 | void MetaPickup::changedUsed(void) |
---|
[6512] | 113 | { |
---|
[6518] | 114 | SUPER(MetaPickup, changedUsed); |
---|
[6512] | 115 | |
---|
[6538] | 116 | //! If the MetaPickup transited to used. |
---|
[6512] | 117 | if(this->isUsed()) |
---|
| 118 | { |
---|
| 119 | PickupCarrier* carrier = this->getCarrier(); |
---|
[6518] | 120 | if(this->getMetaTypeDirect() != pickupMetaType::none && carrier != NULL) |
---|
[6512] | 121 | { |
---|
| 122 | std::set<Pickupable*> pickups = carrier->getPickups(); |
---|
[6538] | 123 | //! Set all Pickupables carried by the PickupCarrier either to used or drop them, depending o the meta type. |
---|
[6512] | 124 | for(std::set<Pickupable*>::iterator it = pickups.begin(); it != pickups.end(); it++) |
---|
| 125 | { |
---|
| 126 | Pickup* pickup = dynamic_cast<Pickup*>(*it); |
---|
[6518] | 127 | if(this->getMetaTypeDirect() == pickupMetaType::use) |
---|
[6512] | 128 | { |
---|
| 129 | if(pickup != NULL && pickup != this && pickup->isOnUse() && !pickup->isUsed()) |
---|
| 130 | { |
---|
| 131 | pickup->setUsed(true); |
---|
| 132 | } |
---|
| 133 | } |
---|
[6518] | 134 | if(this->getMetaTypeDirect() == pickupMetaType::drop) |
---|
[6512] | 135 | { |
---|
| 136 | if(pickup != NULL && pickup != this) |
---|
| 137 | { |
---|
| 138 | carrier->drop(pickup); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | this->destroy(); |
---|
| 144 | } |
---|
| 145 | } |
---|
[6538] | 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 | } |
---|
[6512] | 165 | |
---|
[6538] | 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 | */ |
---|
[6518] | 172 | const std::string& MetaPickup::getMetaType(void) |
---|
[6512] | 173 | { |
---|
[6518] | 174 | switch(this->getMetaTypeDirect()) |
---|
[6512] | 175 | { |
---|
[6518] | 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; |
---|
[6512] | 182 | default: |
---|
| 183 | return BLANKSTRING; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
[6538] | 187 | /** |
---|
| 188 | @brief |
---|
| 189 | Set the meta type of this MetaPickup. |
---|
| 190 | @param type |
---|
| 191 | A string with the type to be set. |
---|
| 192 | */ |
---|
[6518] | 193 | void MetaPickup::setMetaType(const std::string& type) |
---|
[6512] | 194 | { |
---|
[6518] | 195 | if(type == MetaPickup::metaTypeNone_s) |
---|
[6512] | 196 | { |
---|
[6518] | 197 | this->setMetaTypeDirect(pickupMetaType::none); |
---|
[6512] | 198 | } |
---|
[6518] | 199 | else if(type == MetaPickup::metaTypeUse_s) |
---|
[6512] | 200 | { |
---|
[6518] | 201 | this->setMetaTypeDirect(pickupMetaType::use); |
---|
[6512] | 202 | } |
---|
[6518] | 203 | else if(type == MetaPickup::metaTypeDrop_s) |
---|
[6512] | 204 | { |
---|
[6518] | 205 | this->setMetaTypeDirect(pickupMetaType::drop); |
---|
[6512] | 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | } |
---|