| 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 Pickupable.cc | 
|---|
| 31 |     @brief Implementation of the Pickupable class. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "Pickupable.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "core/Identifier.h" | 
|---|
| 37 | #include "core/CoreIncludes.h" | 
|---|
| 38 | #include "pickup/PickupIdentifier.h" | 
|---|
| 39 | #include "PickupCarrier.h" | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 |      | 
|---|
| 44 |     /** | 
|---|
| 45 |     @brief | 
|---|
| 46 |         Constructor. Registers the objects and initializes its member variables. | 
|---|
| 47 |     */ | 
|---|
| 48 |     Pickupable::Pickupable() : pickupIdentifier_(NULL), used_(false), pickedUp_(false) | 
|---|
| 49 |     {         | 
|---|
| 50 |         RegisterRootObject(Pickupable); | 
|---|
| 51 |          | 
|---|
| 52 |         this->carrier_ = NULL; | 
|---|
| 53 |          | 
|---|
| 54 |         this->pickupIdentifier_ = new PickupIdentifier(this); | 
|---|
| 55 |     } | 
|---|
| 56 |      | 
|---|
| 57 |     /** | 
|---|
| 58 |     @brief | 
|---|
| 59 |         Destructor.  | 
|---|
| 60 |     */ | 
|---|
| 61 |     Pickupable::~Pickupable() | 
|---|
| 62 |     { | 
|---|
| 63 |         if(this->isUsed()) | 
|---|
| 64 |             this->setUsed(false); | 
|---|
| 65 |          | 
|---|
| 66 |         if(this->isPickedUp() && this->getCarrier() != NULL) | 
|---|
| 67 |         { | 
|---|
| 68 |             this->getCarrier()->drop(this, false); | 
|---|
| 69 |             this->setCarrier(NULL); | 
|---|
| 70 |         } | 
|---|
| 71 |          | 
|---|
| 72 |         if(this->pickupIdentifier_ != NULL) | 
|---|
| 73 |             this->pickupIdentifier_->destroy(); | 
|---|
| 74 |     } | 
|---|
| 75 |      | 
|---|
| 76 |     /** | 
|---|
| 77 |     @brief | 
|---|
| 78 |         Sets the Pickupable to used or unused, depending on the input. | 
|---|
| 79 |     @param used | 
|---|
| 80 |         If used is true the Pickupable is set to used, it is set to unused, otherwise. | 
|---|
| 81 |     @return | 
|---|
| 82 |         Returns true if the used state was changed, false if not. | 
|---|
| 83 |     */ | 
|---|
| 84 |     bool Pickupable::setUsed(bool used) | 
|---|
| 85 |     { | 
|---|
| 86 |         if(this->used_ == used) | 
|---|
| 87 |             return false; | 
|---|
| 88 |          | 
|---|
| 89 |         COUT(4) << "Pickupable (&" << this << ") set to used " << used << "." << std::endl; | 
|---|
| 90 |          | 
|---|
| 91 |         this->used_ = used; | 
|---|
| 92 |         this->changedUsed(); | 
|---|
| 93 |         return true; | 
|---|
| 94 |     } | 
|---|
| 95 |      | 
|---|
| 96 |     /** | 
|---|
| 97 |     @brief | 
|---|
| 98 |         Get whether the given PickupCarrier is a target of this Pickupable. | 
|---|
| 99 |     @param carrier | 
|---|
| 100 |         The PickupCarrier of which it has to be determinde whether it is a target of this Pickupable. | 
|---|
| 101 |     @return | 
|---|
| 102 |         Returns true if the given PickupCarrier is a target. | 
|---|
| 103 |     */ | 
|---|
| 104 |     bool Pickupable::isTarget(const PickupCarrier* carrier) const | 
|---|
| 105 |     { | 
|---|
| 106 |         if(carrier == NULL) | 
|---|
| 107 |             return false; | 
|---|
| 108 |         return this->isTarget(carrier->getIdentifier()); | 
|---|
| 109 |     } | 
|---|
| 110 |      | 
|---|
| 111 |     /** | 
|---|
| 112 |     @brief | 
|---|
| 113 |         Get whether the given Identififer is a target of this Pickupable. | 
|---|
| 114 |     @param identifier | 
|---|
| 115 |         The PickupCarrier of which it has to be determinde whether it is a target of this Pickupable. | 
|---|
| 116 |     @return | 
|---|
| 117 |         Returns true if the given PickupCarrier is a target. | 
|---|
| 118 |     */ | 
|---|
| 119 |     bool Pickupable::isTarget(const Identifier* identifier) const | 
|---|
| 120 |     { | 
|---|
| 121 |         //! Iterate through all targets of this Pickupable. | 
|---|
| 122 |         for(std::list<Identifier*>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) | 
|---|
| 123 |         { | 
|---|
| 124 |             if(identifier->isA(*it)) | 
|---|
| 125 |                 return true; | 
|---|
| 126 |         } | 
|---|
| 127 |         return false; | 
|---|
| 128 |     } | 
|---|
| 129 |          | 
|---|
| 130 |     /** | 
|---|
| 131 |     @brief | 
|---|
| 132 |         Add a PickupCarrier as target of this Pickupable. | 
|---|
| 133 |     @param target | 
|---|
| 134 |         The PickupCarrier to be added. | 
|---|
| 135 |     @return | 
|---|
| 136 |         Returns true if the target was added, false if not. | 
|---|
| 137 |     */ | 
|---|
| 138 |     bool Pickupable::addTarget(PickupCarrier* target) | 
|---|
| 139 |     { | 
|---|
| 140 |         return this->addTarget(target->getIdentifier()); | 
|---|
| 141 |     } | 
|---|
| 142 |      | 
|---|
| 143 |     /** | 
|---|
| 144 |     @brief | 
|---|
| 145 |         Add a class, representetd by the input Identifier, as target of this Pickupable. | 
|---|
| 146 |     @param target | 
|---|
| 147 |         The Identifier to be added. | 
|---|
| 148 |     @return | 
|---|
| 149 |         Returns true if the target was added, false if not. | 
|---|
| 150 |     */ | 
|---|
| 151 |     bool Pickupable::addTarget(Identifier* target) | 
|---|
| 152 |     { | 
|---|
| 153 |         if(this->isTarget(target)) //!< If the input target is already present in the list of targets. | 
|---|
| 154 |             return false; | 
|---|
| 155 |          | 
|---|
| 156 |         COUT(4) << "Target " << target->getName() << " added to Pickupable (&" << this << ")." << std::endl; | 
|---|
| 157 |         this->targets_.push_back(target); | 
|---|
| 158 |         return true; | 
|---|
| 159 |     } | 
|---|
| 160 |      | 
|---|
| 161 |     /** | 
|---|
| 162 |     @brief   | 
|---|
| 163 |         Sets the Pickupable to picked up. | 
|---|
| 164 |         This method will be called by the PickupCarrier picking the Pickupable up. | 
|---|
| 165 |     @param carrier | 
|---|
| 166 |         The PickupCarrier that picked the Pickupable up. | 
|---|
| 167 |     @return | 
|---|
| 168 |         Returns false if, for some reason, the pickup could not be picked up, e.g. it was picked up already. | 
|---|
| 169 |     */ | 
|---|
| 170 |     bool Pickupable::pickedUp(PickupCarrier* carrier) | 
|---|
| 171 |     { | 
|---|
| 172 |         if(this->isPickedUp()) //!< If the Pickupable is already picked up. | 
|---|
| 173 |             return false; | 
|---|
| 174 |          | 
|---|
| 175 |         COUT(4) << "Pickupable (&" << this << ") got picked up by a PickupCarrier (&" << carrier << ")." << std::endl; | 
|---|
| 176 |         this->setCarrier(carrier); | 
|---|
| 177 |         this->setPickedUp(true); | 
|---|
| 178 |         return true; | 
|---|
| 179 |     } | 
|---|
| 180 |      | 
|---|
| 181 |     /** | 
|---|
| 182 |     @brief | 
|---|
| 183 |         Helper method to set the Pickupable to either picked up or not picked up. | 
|---|
| 184 |     @param pickedUp | 
|---|
| 185 |         The value this->pickedUp_ should be set to. | 
|---|
| 186 |     @return | 
|---|
| 187 |         Returns true if the pickedUp status was changed, false if not. | 
|---|
| 188 |     */ | 
|---|
| 189 |     bool Pickupable::setPickedUp(bool pickedUp) | 
|---|
| 190 |     { | 
|---|
| 191 |         if(this->pickedUp_ == pickedUp) | 
|---|
| 192 |             return false; | 
|---|
| 193 |          | 
|---|
| 194 |         COUT(4) << "Pickupable (&" << this << ") set to pickedUp " << pickedUp << "." << std::endl; | 
|---|
| 195 |          | 
|---|
| 196 |         this->pickedUp_ = pickedUp; | 
|---|
| 197 |         this->changedPickedUp(); | 
|---|
| 198 |         return true; | 
|---|
| 199 |     } | 
|---|
| 200 |          | 
|---|
| 201 |     /** | 
|---|
| 202 |     @brief | 
|---|
| 203 |         Sets the carrier of the Pickupable. | 
|---|
| 204 |     @param carrier | 
|---|
| 205 |         Sets the input PickupCarrier as the carrier of the pickup. | 
|---|
| 206 |     */ | 
|---|
| 207 |     inline bool Pickupable::setCarrier(PickupCarrier* carrier) | 
|---|
| 208 |     { | 
|---|
| 209 |         if(this->carrier_ == carrier) | 
|---|
| 210 |             return false; | 
|---|
| 211 |          | 
|---|
| 212 |         COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl; | 
|---|
| 213 |          | 
|---|
| 214 |         this->carrier_ = carrier; | 
|---|
| 215 |         this->changedCarrier(); | 
|---|
| 216 |         return true; | 
|---|
| 217 |     } | 
|---|
| 218 |      | 
|---|
| 219 |     /** | 
|---|
| 220 |     @brief  | 
|---|
| 221 |         Sets the Pickupable to not picked up or dropped. | 
|---|
| 222 |         This method will be called by the PickupCarrier dropping the Pickupable. | 
|---|
| 223 |     @return | 
|---|
| 224 |         Returns false if the pickup could not be dropped. | 
|---|
| 225 |     */ | 
|---|
| 226 |     bool Pickupable::dropped(void) | 
|---|
| 227 |     { | 
|---|
| 228 |         if(!this->isPickedUp()) //!< If the Pickupable is not picked up. | 
|---|
| 229 |             return false; | 
|---|
| 230 |          | 
|---|
| 231 |         COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl; | 
|---|
| 232 |         this->setUsed(false); | 
|---|
| 233 |         this->setPickedUp(false); | 
|---|
| 234 |          | 
|---|
| 235 |         bool created = this->createSpawner(); | 
|---|
| 236 |          | 
|---|
| 237 |         this->setCarrier(NULL); | 
|---|
| 238 |          | 
|---|
| 239 |         if(!created) | 
|---|
| 240 |         { | 
|---|
| 241 |             this->destroy(); | 
|---|
| 242 |         } | 
|---|
| 243 |          | 
|---|
| 244 |         return true; | 
|---|
| 245 |     } | 
|---|
| 246 |      | 
|---|
| 247 |     /** | 
|---|
| 248 |     @brief | 
|---|
| 249 |         Creates a duplicate of the Pickupable. | 
|---|
| 250 |     @return | 
|---|
| 251 |         Returns the clone of this pickup as a pointer to a Pickupable. | 
|---|
| 252 |     */ | 
|---|
| 253 |     Pickupable* Pickupable::clone(void) | 
|---|
| 254 |     { | 
|---|
| 255 |         OrxonoxClass* item = NULL; | 
|---|
| 256 |         this->clone(item); | 
|---|
| 257 |          | 
|---|
| 258 |         Pickupable* pickup = dynamic_cast<Pickupable*>(item); | 
|---|
| 259 |          | 
|---|
| 260 |         COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl; | 
|---|
| 261 |         return pickup; | 
|---|
| 262 |     } | 
|---|
| 263 |      | 
|---|
| 264 |     /** | 
|---|
| 265 |     @brief | 
|---|
| 266 |         Creates a duplicate of the input OrxonoxClass. | 
|---|
| 267 |         This method needs to be implemented by any Class inheriting from Pickupable. | 
|---|
| 268 |     @param item | 
|---|
| 269 |         A reference to a pointer to the OrxonoxClass that is to be duplicated. | 
|---|
| 270 |     */ | 
|---|
| 271 |     void Pickupable::clone(OrxonoxClass*& item) | 
|---|
| 272 |     { | 
|---|
| 273 |         SUPER(Pickupable, clone, item); | 
|---|
| 274 |     } | 
|---|
| 275 |      | 
|---|
| 276 | } | 
|---|