[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 | /** |
---|
[6540] | 30 | @file PickupCarrier.h |
---|
[6405] | 31 | @brief Definition of the PickupCarrier class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #ifndef _PickupCarrier_H__ |
---|
| 35 | #define _PickupCarrier_H__ |
---|
| 36 | |
---|
| 37 | #include "OrxonoxPrereqs.h" |
---|
| 38 | |
---|
[6474] | 39 | #include <list> |
---|
[6405] | 40 | #include <set> |
---|
[6474] | 41 | #include "Pickupable.h" |
---|
[6512] | 42 | #include "core/Identifier.h" |
---|
[6533] | 43 | #include "core/WeakPtr.h" |
---|
[6405] | 44 | |
---|
[6474] | 45 | #include "core/OrxonoxClass.h" |
---|
| 46 | |
---|
[6965] | 47 | namespace orxonox |
---|
| 48 | { |
---|
[6521] | 49 | |
---|
[6710] | 50 | //! Forward-declarations. |
---|
[6711] | 51 | class PickupManager; |
---|
[6710] | 52 | class Pickup; |
---|
| 53 | class HealthPickup; |
---|
| 54 | class InvisiblePickup; |
---|
| 55 | class MetaPickup; |
---|
[7034] | 56 | class DronePickup; |
---|
[6710] | 57 | class SpeedPickup; |
---|
| 58 | |
---|
[6474] | 59 | /** |
---|
| 60 | @brief |
---|
| 61 | The PickupCarrier interface provides the means, for any class implementing it, to possess Pickupables. |
---|
| 62 | @author |
---|
| 63 | Damian 'Mozork' Frick |
---|
| 64 | */ |
---|
[6965] | 65 | class _OrxonoxExport PickupCarrier : virtual public OrxonoxClass |
---|
| 66 | { |
---|
[6563] | 67 | //! So that the different Pickupables have full access to their PickupCarrier. |
---|
[6711] | 68 | friend class Pickupable; |
---|
| 69 | friend class PickupManager; |
---|
[7127] | 70 | //! Friends. |
---|
[6512] | 71 | friend class Pickup; |
---|
| 72 | friend class HealthPickup; |
---|
[6710] | 73 | friend class InvisiblePickup; |
---|
| 74 | friend class MetaPickup; |
---|
[7034] | 75 | friend class DronePickup; |
---|
[6709] | 76 | friend class SpeedPickup; |
---|
| 77 | |
---|
[6405] | 78 | public: |
---|
[6474] | 79 | PickupCarrier(); //!< Constructor. |
---|
| 80 | virtual ~PickupCarrier(); //!< Destructor. |
---|
[6709] | 81 | |
---|
[6474] | 82 | /** |
---|
| 83 | @brief Can be called to pick up a Pickupable. |
---|
| 84 | @param pickup A pointer to the Pickupable. |
---|
| 85 | @return Returns true if the Pickupable was picked up, false if not. |
---|
| 86 | */ |
---|
[6475] | 87 | bool pickup(Pickupable* pickup) |
---|
[6466] | 88 | { |
---|
| 89 | bool pickedUp = this->pickups_.insert(pickup).second; |
---|
[6474] | 90 | if(pickedUp) |
---|
[6512] | 91 | { |
---|
| 92 | COUT(4) << "Picked up Pickupable " << pickup->getIdentifier()->getName() << "(&" << pickup << ")." << std::endl; |
---|
[6474] | 93 | pickup->pickedUp(this); |
---|
[6512] | 94 | } |
---|
[6466] | 95 | return pickedUp; |
---|
| 96 | } |
---|
[6709] | 97 | |
---|
[6474] | 98 | /** |
---|
| 99 | @brief Can be called to drop a Pickupable. |
---|
| 100 | @param pickup A pointer to the Pickupable. |
---|
[6477] | 101 | @param drop If the Pickupable should just be removed from the PickupCarrier without further action, this can be set to false. true is default. |
---|
[6474] | 102 | @return Returns true if the Pickupable has been dropped, false if not. |
---|
| 103 | */ |
---|
[6477] | 104 | bool drop(Pickupable* pickup, bool drop = true) |
---|
[6709] | 105 | { |
---|
[6512] | 106 | bool dropped = this->pickups_.erase(pickup) == 1; |
---|
| 107 | if(dropped && drop) |
---|
| 108 | { |
---|
| 109 | COUT(4) << "Dropping Pickupable " << pickup->getIdentifier()->getName() << "(&" << pickup << ")." << std::endl; |
---|
| 110 | pickup->dropped(); |
---|
| 111 | } |
---|
| 112 | return dropped; |
---|
[6466] | 113 | } |
---|
[6709] | 114 | |
---|
[6475] | 115 | /** |
---|
| 116 | @brief Can be used to check whether the PickupCarrier or a child of his is a target ot the input Pickupable. |
---|
| 117 | @param pickup A pointer to the Pickupable. |
---|
| 118 | @return Returns true if the PickupCarrier or one of its children is a target, false if not. |
---|
| 119 | */ |
---|
| 120 | bool isTarget(const Pickupable* pickup) |
---|
[6466] | 121 | { |
---|
[6475] | 122 | if(pickup->isTarget(this)) //!< If the PickupCarrier itself is a target. |
---|
[6466] | 123 | return true; |
---|
[6709] | 124 | |
---|
[6475] | 125 | //! Go recursively through all children to check whether they are a target. |
---|
[6711] | 126 | std::vector<PickupCarrier*>* children = this->getCarrierChildren(); |
---|
| 127 | for(std::vector<PickupCarrier*>::const_iterator it = children->begin(); it != children->end(); it++) |
---|
[6466] | 128 | { |
---|
| 129 | if((*it)->isTarget(pickup)) |
---|
| 130 | return true; |
---|
| 131 | } |
---|
[6709] | 132 | |
---|
[6475] | 133 | children->clear(); |
---|
| 134 | delete children; |
---|
[6709] | 135 | |
---|
[6466] | 136 | return false; |
---|
| 137 | } |
---|
[6709] | 138 | |
---|
[6475] | 139 | /** |
---|
| 140 | @brief Get the carrier that is both a child of the PickupCarrier (or the PickupCarrier itself) and a target of the input Pickupable. |
---|
| 141 | @param pickup A pounter to the Pickupable. |
---|
| 142 | @return Returns a pointer to the PickupCarrier that is the target of the input Pickupable. |
---|
| 143 | */ |
---|
| 144 | PickupCarrier* getTarget(const Pickupable* pickup) |
---|
| 145 | { |
---|
| 146 | if(!this->isTarget(pickup)) |
---|
| 147 | return NULL; |
---|
[6709] | 148 | |
---|
[6475] | 149 | if(pickup->isTarget(this)) //!< If the PickupCarrier itself is a target. |
---|
| 150 | return this; |
---|
[6709] | 151 | |
---|
[6475] | 152 | //! Go recursively through all children to check whether they are the target. |
---|
[6711] | 153 | std::vector<PickupCarrier*>* children = this->getCarrierChildren(); |
---|
| 154 | for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++) |
---|
[6475] | 155 | { |
---|
| 156 | if(pickup->isTarget(*it)) |
---|
| 157 | return *it; |
---|
| 158 | } |
---|
[6709] | 159 | |
---|
[6475] | 160 | children->clear(); |
---|
| 161 | delete children; |
---|
[6709] | 162 | |
---|
[6475] | 163 | return NULL; |
---|
| 164 | } |
---|
[6709] | 165 | |
---|
[6540] | 166 | /** |
---|
| 167 | @brief Get the (absolute) position of the PickupCarrier. |
---|
| 168 | This method needs to be implemented by any direct derivative class of PickupCarrier. |
---|
| 169 | @return Returns the position as a Vector3. |
---|
| 170 | */ |
---|
| 171 | virtual const Vector3& getCarrierPosition(void) = 0; |
---|
[7127] | 172 | |
---|
[6475] | 173 | /** |
---|
[6711] | 174 | @brief Get the name of this PickupCarrier. |
---|
| 175 | @return Returns the name as a string. |
---|
| 176 | */ |
---|
| 177 | const std::string& getCarrierName(void) { return this->carrierName_; } // tolua_export |
---|
[7127] | 178 | |
---|
| 179 | protected: |
---|
[6711] | 180 | /** |
---|
[6475] | 181 | @brief Get all direct children of this PickupSpawner. |
---|
| 182 | This method needs to be implemented by any direct derivative class of PickupCarrier. |
---|
[6540] | 183 | The returned list will be deleted by the methods calling this function. |
---|
[6709] | 184 | @return Returns a pointer to a list of all direct children. |
---|
[6475] | 185 | */ |
---|
[6711] | 186 | virtual std::vector<PickupCarrier*>* getCarrierChildren(void) = 0; |
---|
[6475] | 187 | /** |
---|
| 188 | @brief Get the parent of this PickupSpawner |
---|
| 189 | This method needs to be implemented by any direct derivative class of PickupCarrier. |
---|
| 190 | @return Returns a pointer to the parent. |
---|
| 191 | */ |
---|
| 192 | virtual PickupCarrier* getCarrierParent(void) = 0; |
---|
[6709] | 193 | |
---|
[6521] | 194 | /** |
---|
| 195 | @brief Get all Pickupables this PickupCarrier has. |
---|
| 196 | @return Returns the set of all Pickupables this PickupCarrier has. |
---|
| 197 | */ |
---|
[6512] | 198 | std::set<Pickupable*>& getPickups(void) |
---|
| 199 | { return this->pickups_; } |
---|
[7127] | 200 | |
---|
[6711] | 201 | /** |
---|
| 202 | @brief Set the name of this PickupCarrier. |
---|
| 203 | The name needs to be set in the constructor of every class inheriting from PickupCarrier, by calling setCarrierName(). |
---|
| 204 | @param name The name to be set. |
---|
| 205 | */ |
---|
| 206 | void setCarrierName(const std::string& name) |
---|
| 207 | { this->carrierName_ = name; } |
---|
[7127] | 208 | |
---|
[6405] | 209 | private: |
---|
[6475] | 210 | std::set<Pickupable*> pickups_; //!< The list of Pickupables carried by this PickupCarrier. |
---|
[6711] | 211 | std::string carrierName_; //!< The name of the PickupCarrier, as displayed in the PickupInventory. |
---|
[7127] | 212 | |
---|
[6711] | 213 | /** |
---|
| 214 | @brief Get the number of carrier children this PickupCarrier has. |
---|
| 215 | @return Returns the number of carrier children. |
---|
| 216 | */ |
---|
| 217 | unsigned int getNumCarrierChildren(void) |
---|
| 218 | { |
---|
| 219 | std::vector<PickupCarrier*>* list = this->getCarrierChildren(); |
---|
| 220 | unsigned int size = list->size(); |
---|
| 221 | delete list; |
---|
| 222 | return size; |
---|
| 223 | } |
---|
[7127] | 224 | |
---|
[6711] | 225 | /** |
---|
| 226 | @brief Get the index-th child of this PickupCarrier. |
---|
| 227 | @param index The index of the child to return. |
---|
| 228 | @return Returns the index-th child. |
---|
| 229 | */ |
---|
| 230 | PickupCarrier* getCarrierChild(unsigned int index) |
---|
| 231 | { |
---|
| 232 | std::vector<PickupCarrier*>* list = this->getCarrierChildren(); |
---|
| 233 | if(list->size() < index) |
---|
| 234 | return NULL; |
---|
| 235 | PickupCarrier* carrier = (*list)[index]; |
---|
| 236 | delete list; |
---|
| 237 | return carrier; |
---|
| 238 | } |
---|
[7127] | 239 | |
---|
[6711] | 240 | /** |
---|
| 241 | @brief Get the number of Pickupables this PickupCarrier carries. |
---|
| 242 | @return returns the number of pickups. |
---|
| 243 | */ |
---|
| 244 | unsigned int getNumPickups(void) |
---|
| 245 | { return this->pickups_.size(); } |
---|
[7127] | 246 | |
---|
[6711] | 247 | /** |
---|
| 248 | @brief Get the index-th Pickupable of this PickupCarrier. |
---|
| 249 | @param index The index of the Pickupable to return. |
---|
| 250 | @return Returns the index-th pickup. |
---|
| 251 | */ |
---|
| 252 | Pickupable* getPickup(unsigned int index) |
---|
| 253 | { |
---|
| 254 | std::set<Pickupable*>::iterator it; |
---|
| 255 | for(it = this->pickups_.begin(); index != 0 && it != this->pickups_.end(); it++) |
---|
| 256 | index--; |
---|
| 257 | if(it == this->pickups_.end()) |
---|
| 258 | return NULL; |
---|
| 259 | return *it; |
---|
| 260 | } |
---|
[7127] | 261 | |
---|
[6965] | 262 | }; |
---|
| 263 | } |
---|
[6709] | 264 | |
---|
[6405] | 265 | #endif /* _PickupCarrier_H__ */ |
---|