| 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 PickupCollection.cc | 
|---|
| 31 |     @brief Implementation of PickupCollection. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "core/CoreIncludes.h" | 
|---|
| 35 | #include "core/XMLPort.h" | 
|---|
| 36 | #include "interfaces/PickupCarrier.h" | 
|---|
| 37 | #include "DroppedPickup.h" | 
|---|
| 38 | #include "PickupCollectionIdentifier.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "PickupCollection.h" | 
|---|
| 41 |  | 
|---|
| 42 | namespace orxonox | 
|---|
| 43 | { | 
|---|
| 44 |   | 
|---|
| 45 |     CreateFactory(PickupCollection); | 
|---|
| 46 |  | 
|---|
| 47 |     /** | 
|---|
| 48 |     @brief | 
|---|
| 49 |         Default Constructor. | 
|---|
| 50 |     */ | 
|---|
| 51 |     PickupCollection::PickupCollection(BaseObject* creator) : BaseObject(creator) | 
|---|
| 52 |     { | 
|---|
| 53 |         RegisterObject(PickupCollection); | 
|---|
| 54 |          | 
|---|
| 55 |         this->pickupCollectionIdentifier_ = new PickupCollectionIdentifier(this); | 
|---|
| 56 |     } | 
|---|
| 57 |      | 
|---|
| 58 |     /** | 
|---|
| 59 |     @brief | 
|---|
| 60 |         Destructor. Iterates through all Pickupables this PickupCollection consists of and destroys them if they haven't been already. | 
|---|
| 61 |     */ | 
|---|
| 62 |     PickupCollection::~PickupCollection() | 
|---|
| 63 |     { | 
|---|
| 64 |         //! Destroy all Pickupables constructing this PickupCollection. | 
|---|
| 65 |         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) | 
|---|
| 66 |         { | 
|---|
| 67 |             if((*it).get() != NULL) | 
|---|
| 68 |                 (*it).get()->destroy(); | 
|---|
| 69 |         } | 
|---|
| 70 |     } | 
|---|
| 71 |      | 
|---|
| 72 |     /** | 
|---|
| 73 |     @brief | 
|---|
| 74 |         Creates an instance of this Class through XML. | 
|---|
| 75 |     */ | 
|---|
| 76 |     void PickupCollection::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 77 |     { | 
|---|
| 78 |         SUPER(PickupCollection, XMLPort, xmlelement, mode); | 
|---|
| 79 |          | 
|---|
| 80 |         XMLPortObject(PickupCollection, Pickupable, "pickupables", addPickupable, getPickupable, xmlelement, mode); | 
|---|
| 81 |          | 
|---|
| 82 |         this->initializeIdentifier(); | 
|---|
| 83 |     } | 
|---|
| 84 |      | 
|---|
| 85 |     /** | 
|---|
| 86 |     @brief | 
|---|
| 87 |         Initializes the PickupIdentifier for this pickup. | 
|---|
| 88 |     */ | 
|---|
| 89 |     void PickupCollection::initializeIdentifier(void) | 
|---|
| 90 |     { | 
|---|
| 91 |         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) | 
|---|
| 92 |         { | 
|---|
| 93 |             this->pickupCollectionIdentifier_->addPickup((*it).get()->getPickupIdentifier()); | 
|---|
| 94 |         } | 
|---|
| 95 |     } | 
|---|
| 96 |      | 
|---|
| 97 |     /** | 
|---|
| 98 |     @brief | 
|---|
| 99 |         Is called when the pickup has transited from used to unused or the other way around. | 
|---|
| 100 |         Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedUsed); to their changdeUsed method. | 
|---|
| 101 |     */ | 
|---|
| 102 |     void PickupCollection::changedUsed(void) | 
|---|
| 103 |     { | 
|---|
| 104 |         SUPER(PickupCollection, changedUsed); | 
|---|
| 105 |          | 
|---|
| 106 |         //! Change used for all Pickupables this PickupCollection consists of. | 
|---|
| 107 |         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) | 
|---|
| 108 |         { | 
|---|
| 109 |             (*it).get()->setUsed(this->isUsed()); | 
|---|
| 110 |         } | 
|---|
| 111 |     } | 
|---|
| 112 |      | 
|---|
| 113 |     /** | 
|---|
| 114 |     @brief | 
|---|
| 115 |         Is called when the pickup has changed its PickupCarrier. | 
|---|
| 116 |         Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedCarrier); to their changedCarrier method. | 
|---|
| 117 |     */ | 
|---|
| 118 |     void PickupCollection::changedCarrier(void) | 
|---|
| 119 |     { | 
|---|
| 120 |         SUPER(PickupCollection, changedCarrier); | 
|---|
| 121 |          | 
|---|
| 122 |         //! Change the PickupCarrier for all Pickupables this PickupCollection consists of. | 
|---|
| 123 |         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) | 
|---|
| 124 |         { | 
|---|
| 125 |             (*it).get()->setCarrier(this->getCarrier()->getTarget(*it)); | 
|---|
| 126 |         } | 
|---|
| 127 |     } | 
|---|
| 128 |      | 
|---|
| 129 |     /** | 
|---|
| 130 |     @brief | 
|---|
| 131 |         Is called when the pickup has transited from picked up to dropped or the other way around. | 
|---|
| 132 |         Any Class overwriting this method must call its SUPER function by adding SUPER(Classname, changedPickedUp); to their changedPickedUp method. | 
|---|
| 133 |     */ | 
|---|
| 134 |     void PickupCollection::changedPickedUp() | 
|---|
| 135 |     { | 
|---|
| 136 |         SUPER(PickupCollection, changedPickedUp); | 
|---|
| 137 |          | 
|---|
| 138 |         //! Change the pickedUp status for all Pickupables this PickupCollection consists of. | 
|---|
| 139 |         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) | 
|---|
| 140 |         { | 
|---|
| 141 |             (*it).get()->setPickedUp(this->isPickedUp()); | 
|---|
| 142 |         } | 
|---|
| 143 |     } | 
|---|
| 144 |      | 
|---|
| 145 |     /** | 
|---|
| 146 |     @brief | 
|---|
| 147 |         Creates a duplicate of the input OrxonoxClass. | 
|---|
| 148 |         This method needs to be implemented by any Class inheriting from Pickupable. | 
|---|
| 149 |     @param item | 
|---|
| 150 |         A reference to a pointer to the OrxonoxClass that is to be duplicated. | 
|---|
| 151 |     */ | 
|---|
| 152 |     void PickupCollection::clone(OrxonoxClass*& item) | 
|---|
| 153 |     { | 
|---|
| 154 |         if(item == NULL) | 
|---|
| 155 |             item = new PickupCollection(this); | 
|---|
| 156 |          | 
|---|
| 157 |         SUPER(PickupCollection, clone, item); | 
|---|
| 158 |          | 
|---|
| 159 |         PickupCollection* pickup = dynamic_cast<PickupCollection*>(item); | 
|---|
| 160 |         //! Clone all Pickupables this PickupCollection consist of. | 
|---|
| 161 |         for(std::vector<WeakPtr<Pickupable> >::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) | 
|---|
| 162 |         { | 
|---|
| 163 |             Pickupable* newPickup = (*it).get()->clone(); | 
|---|
| 164 |             pickup->addPickupable(newPickup); | 
|---|
| 165 |         } | 
|---|
| 166 |  | 
|---|
| 167 |         pickup->initializeIdentifier(); | 
|---|
| 168 |     } | 
|---|
| 169 |      | 
|---|
| 170 |     /** | 
|---|
| 171 |     @brief | 
|---|
| 172 |         Get whether a given class, represented by the input Identifier, is a target of this PickupCollection. | 
|---|
| 173 |     @param identifier | 
|---|
| 174 |         A pointer to the PickupIdentifier of the PickupCarrier we want to know of, whether it is a target of this PickupCollection. | 
|---|
| 175 |     @return | 
|---|
| 176 |         Returns true if the PickupCarrier identified by the input PickupIdentififer it is a target of this PickupCollection, false if not. | 
|---|
| 177 |     */ | 
|---|
| 178 |     bool PickupCollection::isTarget(PickupCarrier* carrier) const | 
|---|
| 179 |     { | 
|---|
| 180 |         for(std::vector<WeakPtr<Pickupable> >::const_iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) | 
|---|
| 181 |         { | 
|---|
| 182 |             if(!carrier->isTarget((*it).get())) | 
|---|
| 183 |                 return false; | 
|---|
| 184 |         } | 
|---|
| 185 |          | 
|---|
| 186 |         return true; | 
|---|
| 187 |     } | 
|---|
| 188 |      | 
|---|
| 189 |     /** | 
|---|
| 190 |     @brief | 
|---|
| 191 |         Get the PickupIdentifier of this PickupCollection. | 
|---|
| 192 |         This is in fact the PickupCollectionIdentifier. | 
|---|
| 193 |     @return | 
|---|
| 194 |         Returns a pointer to the PickupIdentifier of this PickupCollection. | 
|---|
| 195 |     */ | 
|---|
| 196 |     const PickupIdentifier* PickupCollection::getPickupIdentifier(void) | 
|---|
| 197 |     { | 
|---|
| 198 |         return this->pickupCollectionIdentifier_; | 
|---|
| 199 |     } | 
|---|
| 200 |      | 
|---|
| 201 |     /** | 
|---|
| 202 |     @brief | 
|---|
| 203 |         Add the input Pickupable to list of Pickupables combined by this PickupCollection. | 
|---|
| 204 |     @param pickup | 
|---|
| 205 |         The Pickupable to be added. | 
|---|
| 206 |     @return | 
|---|
| 207 |         Returns true if successful, | 
|---|
| 208 |     */ | 
|---|
| 209 |     bool PickupCollection::addPickupable(Pickupable* pickup) | 
|---|
| 210 |     { | 
|---|
| 211 |         if(pickup == NULL) | 
|---|
| 212 |             return false; | 
|---|
| 213 |          | 
|---|
| 214 |         WeakPtr<Pickupable> ptr = pickup; //!< Create a weak pointer to be able to test in the constructor if the Pointer is still valid. | 
|---|
| 215 |         this->pickups_.push_back(ptr); | 
|---|
| 216 |         return true; | 
|---|
| 217 |     } | 
|---|
| 218 |      | 
|---|
| 219 |     /** | 
|---|
| 220 |     @brief | 
|---|
| 221 |         Get the Pickupable at the given index. | 
|---|
| 222 |     @param index | 
|---|
| 223 |         The index the Pickupable is fetched from. | 
|---|
| 224 |     @return | 
|---|
| 225 |         Returns a pointer to the Pickupable at the index given by index. | 
|---|
| 226 |     */ | 
|---|
| 227 |     const Pickupable* PickupCollection::getPickupable(unsigned int index) | 
|---|
| 228 |     { | 
|---|
| 229 |         return this->pickups_[index].get(); | 
|---|
| 230 |     } | 
|---|
| 231 |          | 
|---|
| 232 |     /** | 
|---|
| 233 |     @brief | 
|---|
| 234 |         Facilitates the creation of a PickupSpawner upon dropping of the Pickupable. | 
|---|
| 235 |         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.: | 
|---|
| 236 |         DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position); | 
|---|
| 237 |     @param position | 
|---|
| 238 |         The position at which the PickupSpawner should be placed. | 
|---|
| 239 |     @return | 
|---|
| 240 |         Returns true if a spawner was created, false if not. | 
|---|
| 241 |     */ | 
|---|
| 242 |     bool PickupCollection::createSpawner(void) | 
|---|
| 243 |     { | 
|---|
| 244 |         new DroppedPickup(this, this, this->getCarrier()); | 
|---|
| 245 |         return true; | 
|---|
| 246 |     } | 
|---|
| 247 |      | 
|---|
| 248 | } | 
|---|