[6405] | 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 PickupCollection.cc |
---|
[6405] | 31 | @brief Implementation of PickupCollection. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "core/CoreIncludes.h" |
---|
| 35 | #include "core/XMLPort.h" |
---|
[7494] | 36 | |
---|
[6405] | 37 | #include "interfaces/PickupCarrier.h" |
---|
[7494] | 38 | |
---|
[7163] | 39 | #include "CollectiblePickup.h" |
---|
[6475] | 40 | #include "DroppedPickup.h" |
---|
| 41 | #include "PickupCollectionIdentifier.h" |
---|
| 42 | |
---|
[6538] | 43 | #include "PickupCollection.h" |
---|
| 44 | |
---|
[6405] | 45 | namespace orxonox |
---|
| 46 | { |
---|
[7163] | 47 | |
---|
[6519] | 48 | CreateFactory(PickupCollection); |
---|
| 49 | |
---|
[6405] | 50 | /** |
---|
| 51 | @brief |
---|
| 52 | Default Constructor. |
---|
[7494] | 53 | @param creator |
---|
| 54 | The creator of the object. |
---|
[6405] | 55 | */ |
---|
[8305] | 56 | PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator), pickupCollectionIdentifier_(NULL) |
---|
[6405] | 57 | { |
---|
| 58 | RegisterObject(PickupCollection); |
---|
[7163] | 59 | |
---|
[6480] | 60 | this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(this); |
---|
[7163] | 61 | this->processingUsed_ = false; |
---|
| 62 | this->processingPickedUp_ = false; |
---|
[6405] | 63 | } |
---|
[7163] | 64 | |
---|
[6405] | 65 | /** |
---|
| 66 | @brief |
---|
[6538] | 67 | Destructor. Iterates through all Pickupables this PickupCollection consists of and destroys them if they haven't been already. |
---|
[6405] | 68 | */ |
---|
[9290] | 69 | PickupCollection::~PickupCollection() |
---|
[6405] | 70 | { |
---|
[7163] | 71 | // Destroy all Pickupables constructing this PickupCollection. |
---|
[9290] | 72 | for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) |
---|
[6405] | 73 | { |
---|
[9290] | 74 | (*it)->wasRemovedFromCollection(); |
---|
[9296] | 75 | (*it)->destroy(); |
---|
[6405] | 76 | } |
---|
[7163] | 77 | this->pickups_.clear(); |
---|
[9279] | 78 | |
---|
[8305] | 79 | if(this->pickupCollectionIdentifier_ != NULL) |
---|
[9294] | 80 | this->pickupCollectionIdentifier_->destroy(); |
---|
[6405] | 81 | } |
---|
[7163] | 82 | |
---|
[6405] | 83 | /** |
---|
| 84 | @brief |
---|
| 85 | Creates an instance of this Class through XML. |
---|
| 86 | */ |
---|
| 87 | void PickupCollection::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 88 | { |
---|
| 89 | SUPER(PickupCollection, XMLPort, xmlelement, mode); |
---|
[7163] | 90 | |
---|
| 91 | XMLPortObject(PickupCollection, CollectiblePickup, "pickupables", addPickupable, getPickupable, xmlelement, mode); |
---|
[6405] | 92 | } |
---|
[7163] | 93 | |
---|
[6538] | 94 | /** |
---|
| 95 | @brief |
---|
| 96 | Is called when the pickup has transited from used to unused or the other way around. |
---|
| 97 | Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedUsed); to their changdeUsed method. |
---|
[6475] | 98 | */ |
---|
[6405] | 99 | void PickupCollection::changedUsed(void) |
---|
| 100 | { |
---|
| 101 | SUPER(PickupCollection, changedUsed); |
---|
[7163] | 102 | |
---|
| 103 | this->processingUsed_ = true; |
---|
| 104 | // Change used for all Pickupables this PickupCollection consists of. |
---|
[9290] | 105 | for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) |
---|
[7163] | 106 | (*it)->setUsed(this->isUsed()); |
---|
[7533] | 107 | |
---|
[7163] | 108 | this->processingUsed_ = false; |
---|
| 109 | |
---|
| 110 | this->changedUsedAction(); |
---|
[6405] | 111 | } |
---|
[7163] | 112 | |
---|
[6538] | 113 | /** |
---|
| 114 | @brief |
---|
[7163] | 115 | Helper method. |
---|
| 116 | Checks whether due to changes in the used status of the pickups of this PickupCollection the used status of this PickupCollection has to change as well. |
---|
| 117 | */ |
---|
| 118 | void PickupCollection::changedUsedAction(void) |
---|
| 119 | { |
---|
| 120 | if(this->processingUsed_) |
---|
| 121 | return; |
---|
| 122 | |
---|
[9295] | 123 | size_t numPickupsEnabled = 0; |
---|
| 124 | size_t numPickupsInUse = 0; |
---|
| 125 | for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) |
---|
| 126 | { |
---|
| 127 | if ((*it)->isEnabled()) |
---|
| 128 | ++numPickupsEnabled; |
---|
| 129 | if ((*it)->isUsed()) |
---|
| 130 | ++numPickupsInUse; |
---|
| 131 | } |
---|
| 132 | |
---|
[7163] | 133 | // If all the pickups are not in use but the PickupCollection is. |
---|
[9295] | 134 | if(numPickupsInUse == 0 && this->isUsed()) |
---|
[7163] | 135 | this->setUsed(false); |
---|
| 136 | |
---|
| 137 | // If all the enabled pickups are in use but the PickupCollection is not. |
---|
[9295] | 138 | if(numPickupsInUse > 0 && numPickupsInUse == numPickupsEnabled && !this->isUsed()) |
---|
[7163] | 139 | this->setUsed(true); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | @brief |
---|
[6538] | 144 | Is called when the pickup has changed its PickupCarrier. |
---|
| 145 | Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedCarrier); to their changedCarrier method. |
---|
| 146 | */ |
---|
[6521] | 147 | void PickupCollection::changedCarrier(void) |
---|
[6405] | 148 | { |
---|
[6466] | 149 | SUPER(PickupCollection, changedCarrier); |
---|
[7163] | 150 | |
---|
| 151 | // Change the PickupCarrier for all Pickupables this PickupCollection consists of. |
---|
[9290] | 152 | for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) |
---|
[6405] | 153 | { |
---|
[7163] | 154 | if(this->getCarrier() == NULL) |
---|
| 155 | (*it)->setCarrier(NULL); |
---|
| 156 | else |
---|
| 157 | (*it)->setCarrier(this->getCarrier()->getTarget(*it)); |
---|
[6405] | 158 | } |
---|
| 159 | } |
---|
[7163] | 160 | |
---|
[6538] | 161 | /** |
---|
| 162 | @brief |
---|
| 163 | Is called when the pickup has transited from picked up to dropped or the other way around. |
---|
| 164 | Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedPickedUp); to their changedPickedUp method. |
---|
| 165 | */ |
---|
[6521] | 166 | void PickupCollection::changedPickedUp() |
---|
| 167 | { |
---|
| 168 | SUPER(PickupCollection, changedPickedUp); |
---|
[7163] | 169 | |
---|
| 170 | this->processingPickedUp_ = true; |
---|
| 171 | // Change the pickedUp status for all Pickupables this PickupCollection consists of. |
---|
[9290] | 172 | for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ) |
---|
| 173 | (*(it++))->setPickedUp(this->isPickedUp()); |
---|
[7533] | 174 | |
---|
[7163] | 175 | this->processingPickedUp_ = false; |
---|
| 176 | |
---|
| 177 | this->changedPickedUpAction(); |
---|
[6521] | 178 | } |
---|
[7163] | 179 | |
---|
[6538] | 180 | /** |
---|
| 181 | @brief |
---|
[7163] | 182 | Helper method. |
---|
| 183 | Checks whether due to changes in the picked up status of the pickups of this PickupCollection the picked up status of this PickupCollection has to change as well. |
---|
| 184 | */ |
---|
| 185 | void PickupCollection::changedPickedUpAction(void) |
---|
| 186 | { |
---|
| 187 | if(this->processingPickedUp_) |
---|
| 188 | return; |
---|
| 189 | |
---|
[9279] | 190 | // If at least all the enabled pickups of this PickupCollection are no longer picked up. |
---|
[9295] | 191 | bool isOnePickupEnabledAndPickedUp = false; |
---|
| 192 | for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) |
---|
| 193 | { |
---|
| 194 | if ((*it)->isEnabled() && (*it)->isPickedUp()) |
---|
| 195 | { |
---|
| 196 | isOnePickupEnabledAndPickedUp = true; |
---|
| 197 | break; |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | if(!isOnePickupEnabledAndPickedUp && this->isPickedUp()) |
---|
[7163] | 201 | this->Pickupable::destroy(); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | @brief |
---|
[7494] | 206 | Creates a duplicate of the input Pickupable. |
---|
[6538] | 207 | This method needs to be implemented by any Class inheriting from Pickupable. |
---|
| 208 | @param item |
---|
| 209 | A reference to a pointer to the OrxonoxClass that is to be duplicated. |
---|
| 210 | */ |
---|
[6497] | 211 | void PickupCollection::clone(OrxonoxClass*& item) |
---|
[6405] | 212 | { |
---|
[6466] | 213 | if(item == NULL) |
---|
| 214 | item = new PickupCollection(this); |
---|
[7163] | 215 | |
---|
[6466] | 216 | SUPER(PickupCollection, clone, item); |
---|
[7163] | 217 | |
---|
[9279] | 218 | PickupCollection* pickup = orxonox_cast<PickupCollection*>(item); |
---|
[7163] | 219 | // Clone all Pickupables this PickupCollection consist of. |
---|
[9290] | 220 | for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) |
---|
[6405] | 221 | { |
---|
[7163] | 222 | Pickupable* newPickup = (*it)->clone(); |
---|
| 223 | CollectiblePickup* collectible = static_cast<CollectiblePickup*>(newPickup); |
---|
| 224 | pickup->addPickupable(collectible); |
---|
[6405] | 225 | } |
---|
| 226 | } |
---|
[7163] | 227 | |
---|
[6538] | 228 | /** |
---|
| 229 | @brief |
---|
| 230 | Get whether a given class, represented by the input Identifier, is a target of this PickupCollection. |
---|
[7401] | 231 | @param carrier |
---|
| 232 | A pointer to the PickupCarrier we want to know of, whether it is a target of this PickupCollection. |
---|
[6538] | 233 | @return |
---|
| 234 | Returns true if the PickupCarrier identified by the input PickupIdentififer it is a target of this PickupCollection, false if not. |
---|
| 235 | */ |
---|
[7547] | 236 | bool PickupCollection::isTarget(const PickupCarrier* carrier) const |
---|
[6519] | 237 | { |
---|
[9290] | 238 | for(std::list<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) |
---|
[6519] | 239 | { |
---|
[7163] | 240 | if(!carrier->isTarget(*it)) |
---|
[6519] | 241 | return false; |
---|
| 242 | } |
---|
[7163] | 243 | |
---|
[6519] | 244 | return true; |
---|
| 245 | } |
---|
[7163] | 246 | |
---|
[6538] | 247 | /** |
---|
| 248 | @brief |
---|
| 249 | Get the PickupIdentifier of this PickupCollection. |
---|
| 250 | This is in fact the PickupCollectionIdentifier. |
---|
| 251 | @return |
---|
| 252 | Returns a pointer to the PickupIdentifier of this PickupCollection. |
---|
| 253 | */ |
---|
[7547] | 254 | const PickupIdentifier* PickupCollection::getPickupIdentifier(void) const |
---|
[6475] | 255 | { |
---|
| 256 | return this->pickupCollectionIdentifier_; |
---|
| 257 | } |
---|
[7163] | 258 | |
---|
[6538] | 259 | /** |
---|
| 260 | @brief |
---|
| 261 | Add the input Pickupable to list of Pickupables combined by this PickupCollection. |
---|
| 262 | @param pickup |
---|
| 263 | The Pickupable to be added. |
---|
| 264 | @return |
---|
| 265 | Returns true if successful, |
---|
| 266 | */ |
---|
[7163] | 267 | bool PickupCollection::addPickupable(CollectiblePickup* pickup) |
---|
[6538] | 268 | { |
---|
| 269 | if(pickup == NULL) |
---|
| 270 | return false; |
---|
[7163] | 271 | |
---|
| 272 | this->pickups_.push_back(pickup); |
---|
[9290] | 273 | pickup->wasAddedToCollection(this); |
---|
[9295] | 274 | this->pickupsChanged(); |
---|
[6538] | 275 | return true; |
---|
| 276 | } |
---|
[7163] | 277 | |
---|
[6538] | 278 | /** |
---|
| 279 | @brief |
---|
| 280 | Get the Pickupable at the given index. |
---|
| 281 | @param index |
---|
| 282 | The index the Pickupable is fetched from. |
---|
| 283 | @return |
---|
| 284 | Returns a pointer to the Pickupable at the index given by index. |
---|
| 285 | */ |
---|
[7547] | 286 | const Pickupable* PickupCollection::getPickupable(unsigned int index) const |
---|
[6538] | 287 | { |
---|
[9292] | 288 | if(this->pickups_.size() >= index) |
---|
| 289 | return NULL; |
---|
| 290 | |
---|
[9293] | 291 | std::list<CollectiblePickup*>::const_iterator it = this->pickups_.begin(); |
---|
[9292] | 292 | std::advance(it, index); |
---|
| 293 | return *it; |
---|
[6538] | 294 | } |
---|
[7163] | 295 | |
---|
[6538] | 296 | /** |
---|
| 297 | @brief |
---|
[9290] | 298 | Removes the Pickup from the Collection. |
---|
| 299 | @param pickup |
---|
| 300 | The Pickup to be removed. |
---|
| 301 | @return |
---|
| 302 | Returns true if the pickup was in the collection. |
---|
| 303 | */ |
---|
| 304 | bool PickupCollection::removePickupable(CollectiblePickup* pickup) |
---|
| 305 | { |
---|
| 306 | for(std::list<CollectiblePickup*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); ++it) |
---|
| 307 | { |
---|
| 308 | if (*it == pickup) |
---|
| 309 | { |
---|
| 310 | this->pickups_.erase(it); |
---|
| 311 | pickup->wasRemovedFromCollection(); |
---|
[9295] | 312 | this->pickupsChanged(); |
---|
[9290] | 313 | return true; |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | return false; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | @brief |
---|
[7163] | 321 | Informs the PickupCollection, that one of its pickups has changed its used status to the input value. |
---|
| 322 | This is used internally by the CollectiblePickup class. |
---|
| 323 | @param changed |
---|
[9279] | 324 | The value the used status has changed to. |
---|
[7163] | 325 | */ |
---|
| 326 | void PickupCollection::pickupChangedUsed(bool changed) |
---|
| 327 | { |
---|
| 328 | this->changedUsedAction(); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /** |
---|
| 332 | @brief |
---|
| 333 | Informs the PickupCollection, that one of its pickups has changed its picked up status to the input value. |
---|
| 334 | This is used internally by the CollectiblePickup class. |
---|
| 335 | @param changed |
---|
| 336 | The value the picked up status has changed to. |
---|
| 337 | */ |
---|
| 338 | void PickupCollection::pickupChangedPickedUp(bool changed) |
---|
| 339 | { |
---|
| 340 | this->changedPickedUpAction(); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | /** |
---|
| 344 | @brief |
---|
| 345 | Informs the PickupCollection, that one of its pickups has been disabled. |
---|
| 346 | This is used internally by the CollectiblePickup class. |
---|
| 347 | */ |
---|
| 348 | void PickupCollection::pickupDisabled(void) |
---|
| 349 | { |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | /** |
---|
| 353 | @brief |
---|
[9295] | 354 | Helpfer function if the number of pickups in this collection has changed. |
---|
| 355 | */ |
---|
| 356 | void PickupCollection::pickupsChanged(void) |
---|
| 357 | { |
---|
| 358 | this->changedUsedAction(); |
---|
| 359 | this->changedPickedUpAction(); |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | /** |
---|
| 363 | @brief |
---|
[6538] | 364 | Facilitates the creation of a PickupSpawner upon dropping of the Pickupable. |
---|
| 365 | This method must be implemented by any class directly inheriting from Pickupable. It is most easily done by just creating a new DroppedPickup, e.g.: |
---|
| 366 | DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position); |
---|
| 367 | @return |
---|
| 368 | Returns true if a spawner was created, false if not. |
---|
| 369 | */ |
---|
[6540] | 370 | bool PickupCollection::createSpawner(void) |
---|
[6538] | 371 | { |
---|
[6540] | 372 | new DroppedPickup(this, this, this->getCarrier()); |
---|
[6538] | 373 | return true; |
---|
| 374 | } |
---|
[7163] | 375 | |
---|
| 376 | } |
---|