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