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