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