[7494] | 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 PickupCarrier.cc |
---|
| 31 | @brief Implementation of the PickupCarrier class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "PickupCarrier.h" |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/Identifier.h" |
---|
[7547] | 38 | |
---|
[7494] | 39 | #include "Pickupable.h" |
---|
| 40 | |
---|
| 41 | namespace orxonox { |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | @brief |
---|
| 45 | Constructor. Registers the object. |
---|
| 46 | */ |
---|
| 47 | PickupCarrier::PickupCarrier() |
---|
| 48 | { |
---|
| 49 | RegisterRootObject(PickupCarrier); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | @brief |
---|
| 54 | Destructor. |
---|
| 55 | */ |
---|
| 56 | PickupCarrier::~PickupCarrier() |
---|
| 57 | { |
---|
| 58 | |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | @brief |
---|
| 63 | Is called before the PickupCarrier is effectively destroyed. |
---|
| 64 | */ |
---|
| 65 | void PickupCarrier::preDestroy(void) |
---|
| 66 | { |
---|
| 67 | std::set<Pickupable*>::iterator it = this->pickups_.begin(); |
---|
[8297] | 68 | Pickupable* temp; |
---|
| 69 | // Iterate over all pickups that are attached to this PickupCarrier and destroy them. |
---|
[7494] | 70 | while(it != this->pickups_.end()) |
---|
| 71 | { |
---|
[8297] | 72 | temp = *it; |
---|
[7494] | 73 | (*it)->carrierDestroyed(); |
---|
| 74 | it = this->pickups_.begin(); |
---|
[8300] | 75 | if(it != this->pickups_.end() && temp == *it) // Infinite loop avoidance, in case the pickup wasn't removed from the carrier somewhere in the carrierDestroy() procedure. |
---|
[7494] | 76 | { |
---|
[8858] | 77 | orxout(internal_warning, context::pickups) << "Oops. In a PickupCarrier, while cleaning up, a Pickupable (&" << temp << ") didn't unregister itself as it should have." << endl;; |
---|
[7494] | 78 | it++; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | this->pickups_.clear(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | @brief |
---|
| 87 | Can be used to check whether the PickupCarrier or a child of his is a target ot the input Pickupable. |
---|
| 88 | @param pickup |
---|
| 89 | A pointer to the Pickupable. |
---|
| 90 | @return |
---|
| 91 | Returns true if the PickupCarrier or one of its children is a target, false if not. |
---|
| 92 | */ |
---|
[7547] | 93 | bool PickupCarrier::isTarget(const Pickupable* pickup) const |
---|
[7494] | 94 | { |
---|
| 95 | if(pickup->isTarget(this)) // If the PickupCarrier itself is a target. |
---|
| 96 | return true; |
---|
| 97 | |
---|
[8303] | 98 | bool isTarget = false; |
---|
[7494] | 99 | // Go recursively through all children to check whether they are a target. |
---|
| 100 | std::vector<PickupCarrier*>* children = this->getCarrierChildren(); |
---|
| 101 | for(std::vector<PickupCarrier*>::const_iterator it = children->begin(); it != children->end(); it++) |
---|
| 102 | { |
---|
| 103 | if((*it)->isTarget(pickup)) |
---|
[8303] | 104 | { |
---|
| 105 | isTarget = true; |
---|
| 106 | break; |
---|
| 107 | } |
---|
[7494] | 108 | } |
---|
| 109 | |
---|
| 110 | children->clear(); |
---|
| 111 | delete children; |
---|
| 112 | |
---|
[8306] | 113 | return isTarget; |
---|
[7494] | 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | @brief |
---|
| 118 | Get the carrier that is both a child of the PickupCarrier (or the PickupCarrier itself) and a target of the input Pickupable. |
---|
| 119 | @param pickup |
---|
| 120 | A pounter to the Pickupable. |
---|
| 121 | @return |
---|
| 122 | Returns a pointer to the PickupCarrier that is the target of the input Pickupable. |
---|
| 123 | */ |
---|
| 124 | PickupCarrier* PickupCarrier::getTarget(const Pickupable* pickup) |
---|
| 125 | { |
---|
| 126 | if(!this->isTarget(pickup)) |
---|
| 127 | return NULL; |
---|
| 128 | |
---|
| 129 | if(pickup->isTarget(this)) // If the PickupCarrier itself is a target. |
---|
| 130 | return this; |
---|
| 131 | |
---|
[8302] | 132 | PickupCarrier* target = NULL; |
---|
[7494] | 133 | // Go recursively through all children to check whether they are the target. |
---|
| 134 | std::vector<PickupCarrier*>* children = this->getCarrierChildren(); |
---|
| 135 | for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++) |
---|
| 136 | { |
---|
| 137 | if(pickup->isTarget(*it)) |
---|
[8302] | 138 | { |
---|
| 139 | target = *it; |
---|
| 140 | break; |
---|
| 141 | } |
---|
[7494] | 142 | } |
---|
| 143 | |
---|
| 144 | children->clear(); |
---|
| 145 | delete children; |
---|
| 146 | |
---|
[8302] | 147 | return target; |
---|
[7494] | 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | @brief |
---|
| 152 | Adds a Pickupable to the list of pickups that are carried by this PickupCarrier. |
---|
| 153 | @param pickup |
---|
| 154 | A pointer to the pickup to be added. |
---|
| 155 | @return |
---|
| 156 | Returns true if successfull, false if the Pickupable was already present. |
---|
| 157 | */ |
---|
| 158 | bool PickupCarrier::addPickup(Pickupable* pickup) |
---|
| 159 | { |
---|
[8858] | 160 | orxout(verbose, context::pickups) << "Adding Pickupable (&" << pickup << ") to PickupCarrier (&" << this << ")" << endl; |
---|
[7494] | 161 | return this->pickups_.insert(pickup).second; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | @brief |
---|
| 166 | Removes a Pickupable from the list of pickups that are carried by this PickupCarrier. |
---|
| 167 | @param pickup |
---|
| 168 | A pointer to the pickup to be removed. |
---|
| 169 | @return |
---|
| 170 | Returns true if successfull, false if the Pickupable was not present in the list. |
---|
| 171 | */ |
---|
| 172 | bool PickupCarrier::removePickup(Pickupable* pickup) |
---|
| 173 | { |
---|
[8858] | 174 | orxout(verbose, context::pickups) << "Removing Pickupable (&" << pickup << ") from PickupCarrier (&" << this << ")" << endl; |
---|
[7494] | 175 | return this->pickups_.erase(pickup) == 1; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | } |
---|