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