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