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