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