[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 | { |
---|
[7127] | 48 | |
---|
[6474] | 49 | /** |
---|
| 50 | @brief |
---|
| 51 | Constructor. Registers the objects and initializes its member variables. |
---|
| 52 | */ |
---|
[6725] | 53 | Pickupable::Pickupable() : pickupIdentifier_(NULL), used_(false), pickedUp_(false) |
---|
[7127] | 54 | { |
---|
[6478] | 55 | RegisterRootObject(Pickupable); |
---|
[7127] | 56 | |
---|
[6474] | 57 | this->carrier_ = NULL; |
---|
[7127] | 58 | |
---|
[6480] | 59 | this->pickupIdentifier_ = new PickupIdentifier(this); |
---|
[6474] | 60 | } |
---|
[7127] | 61 | |
---|
[6474] | 62 | /** |
---|
| 63 | @brief |
---|
[7127] | 64 | Destructor. |
---|
[6474] | 65 | */ |
---|
| 66 | Pickupable::~Pickupable() |
---|
| 67 | { |
---|
[6477] | 68 | if(this->isUsed()) |
---|
| 69 | this->setUsed(false); |
---|
[7127] | 70 | |
---|
[6478] | 71 | if(this->isPickedUp() && this->getCarrier() != NULL) |
---|
[6477] | 72 | { |
---|
| 73 | this->getCarrier()->drop(this, false); |
---|
| 74 | this->setCarrier(NULL); |
---|
| 75 | } |
---|
[7127] | 76 | |
---|
[6725] | 77 | if(this->pickupIdentifier_ != NULL) |
---|
| 78 | this->pickupIdentifier_->destroy(); |
---|
[6474] | 79 | } |
---|
[7127] | 80 | |
---|
[6474] | 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; |
---|
[7127] | 93 | |
---|
[6474] | 94 | COUT(4) << "Pickupable (&" << this << ") set to used " << used << "." << std::endl; |
---|
[7127] | 95 | |
---|
[6474] | 96 | this->used_ = used; |
---|
| 97 | this->changedUsed(); |
---|
[6996] | 98 | |
---|
| 99 | GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()"); |
---|
[6474] | 100 | return true; |
---|
| 101 | } |
---|
[7127] | 102 | |
---|
[6474] | 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 | } |
---|
[7127] | 117 | |
---|
[6490] | 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 | } |
---|
[7127] | 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 | } |
---|
[7127] | 149 | |
---|
[6490] | 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; |
---|
[7127] | 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 | } |
---|
[7127] | 167 | |
---|
[6474] | 168 | /** |
---|
[7127] | 169 | @brief |
---|
[6474] | 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; |
---|
[7127] | 181 | |
---|
[6474] | 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 | } |
---|
[7127] | 187 | |
---|
[6474] | 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; |
---|
[7127] | 200 | |
---|
[6521] | 201 | COUT(4) << "Pickupable (&" << this << ") set to pickedUp " << pickedUp << "." << std::endl; |
---|
[7127] | 202 | |
---|
[6521] | 203 | this->pickedUp_ = pickedUp; |
---|
| 204 | this->changedPickedUp(); |
---|
[6996] | 205 | GUIManager::getInstance().getLuaState()->doString("PickupInventory.update()"); |
---|
[6521] | 206 | return true; |
---|
| 207 | } |
---|
[7127] | 208 | |
---|
[6521] | 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 | */ |
---|
[7094] | 215 | inline bool Pickupable::setCarrier(PickupCarrier* carrier, bool tell) |
---|
[6521] | 216 | { |
---|
| 217 | if(this->carrier_ == carrier) |
---|
| 218 | return false; |
---|
[7127] | 219 | |
---|
[6521] | 220 | COUT(4) << "Pickupable (&" << this << ") changed Carrier (& " << carrier << ")." << std::endl; |
---|
[7127] | 221 | |
---|
[6521] | 222 | this->carrier_ = carrier; |
---|
| 223 | this->changedCarrier(); |
---|
[7094] | 224 | if(tell && carrier != NULL) |
---|
| 225 | this->carrier_->pickups_.insert(this); |
---|
[6521] | 226 | return true; |
---|
| 227 | } |
---|
[7127] | 228 | |
---|
[6521] | 229 | /** |
---|
[7127] | 230 | @brief |
---|
[6474] | 231 | Sets the Pickupable to not picked up or dropped. |
---|
| 232 | This method will be called by the PickupCarrier dropping the Pickupable. |
---|
| 233 | @return |
---|
| 234 | Returns false if the pickup could not be dropped. |
---|
| 235 | */ |
---|
| 236 | bool Pickupable::dropped(void) |
---|
| 237 | { |
---|
| 238 | if(!this->isPickedUp()) //!< If the Pickupable is not picked up. |
---|
| 239 | return false; |
---|
[7127] | 240 | |
---|
[6474] | 241 | COUT(4) << "Pickupable (&" << this << ") got dropped up by a PickupCarrier (&" << this->getCarrier() << ")." << std::endl; |
---|
| 242 | this->setUsed(false); |
---|
| 243 | this->setPickedUp(false); |
---|
[7127] | 244 | |
---|
[6540] | 245 | bool created = this->createSpawner(); |
---|
[7127] | 246 | |
---|
[6474] | 247 | this->setCarrier(NULL); |
---|
[7127] | 248 | |
---|
[6475] | 249 | if(!created) |
---|
[6512] | 250 | { |
---|
[6475] | 251 | this->destroy(); |
---|
[6512] | 252 | } |
---|
[7127] | 253 | |
---|
[6474] | 254 | return true; |
---|
| 255 | } |
---|
[7127] | 256 | |
---|
[6474] | 257 | /** |
---|
| 258 | @brief |
---|
| 259 | Creates a duplicate of the Pickupable. |
---|
| 260 | @return |
---|
| 261 | Returns the clone of this pickup as a pointer to a Pickupable. |
---|
| 262 | */ |
---|
| 263 | Pickupable* Pickupable::clone(void) |
---|
| 264 | { |
---|
[6497] | 265 | OrxonoxClass* item = NULL; |
---|
| 266 | this->clone(item); |
---|
[7127] | 267 | |
---|
[6497] | 268 | Pickupable* pickup = dynamic_cast<Pickupable*>(item); |
---|
[7127] | 269 | |
---|
[6474] | 270 | COUT(4) << "Pickupable (&" << this << ") cloned. Clone is new Pickupable (&" << pickup << ")." << std::endl; |
---|
| 271 | return pickup; |
---|
| 272 | } |
---|
[7127] | 273 | |
---|
[6474] | 274 | /** |
---|
| 275 | @brief |
---|
| 276 | Creates a duplicate of the input OrxonoxClass. |
---|
| 277 | This method needs to be implemented by any Class inheriting from Pickupable. |
---|
| 278 | @param item |
---|
[6538] | 279 | A reference to a pointer to the OrxonoxClass that is to be duplicated. |
---|
[6474] | 280 | */ |
---|
[6497] | 281 | void Pickupable::clone(OrxonoxClass*& item) |
---|
[6474] | 282 | { |
---|
| 283 | SUPER(Pickupable, clone, item); |
---|
| 284 | } |
---|
[6996] | 285 | |
---|
| 286 | /** |
---|
| 287 | @brief |
---|
| 288 | Method to transcribe a Pickupable as a Rewardable to the player. |
---|
| 289 | @param player |
---|
| 290 | A pointer to the PlayerInfo, do whatever you want with it. |
---|
| 291 | @return |
---|
| 292 | Return true if successful. |
---|
| 293 | */ |
---|
| 294 | bool Pickupable::reward(PlayerInfo* player) |
---|
| 295 | { |
---|
| 296 | ControllableEntity* entity = player->getControllableEntity(); |
---|
| 297 | Pawn* pawn = static_cast<Pawn*>(entity); |
---|
| 298 | PickupCarrier* carrier = static_cast<PickupCarrier*>(pawn); |
---|
| 299 | return carrier->pickup(this); |
---|
| 300 | } |
---|
[7127] | 301 | |
---|
[6474] | 302 | } |
---|