| 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.h | 
|---|
| 31 | @brief Definition of the PickupCarrier class. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #ifndef _PickupCarrier_H__ | 
|---|
| 35 | #define _PickupCarrier_H__ | 
|---|
| 36 |  | 
|---|
| 37 | #include "OrxonoxPrereqs.h" | 
|---|
| 38 |  | 
|---|
| 39 | #include <list> | 
|---|
| 40 | #include <set> | 
|---|
| 41 | #include "Pickupable.h" | 
|---|
| 42 | #include "core/Identifier.h" | 
|---|
| 43 | #include "core/WeakPtr.h" | 
|---|
| 44 |  | 
|---|
| 45 | #include "core/OrxonoxClass.h" | 
|---|
| 46 |  | 
|---|
| 47 | namespace orxonox | 
|---|
| 48 | { | 
|---|
| 49 |  | 
|---|
| 50 | //! Forward-declarations. | 
|---|
| 51 | class PickupManager; | 
|---|
| 52 | class Pickup; | 
|---|
| 53 | class HealthPickup; | 
|---|
| 54 | class InvisiblePickup; | 
|---|
| 55 | class MetaPickup; | 
|---|
| 56 | class DronePickup; | 
|---|
| 57 | class SpeedPickup; | 
|---|
| 58 |  | 
|---|
| 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 | */ | 
|---|
| 65 | class _OrxonoxExport PickupCarrier : virtual public OrxonoxClass | 
|---|
| 66 | { | 
|---|
| 67 | //! So that the different Pickupables have full access to their PickupCarrier. | 
|---|
| 68 | friend class Pickupable; | 
|---|
| 69 | friend class PickupManager; | 
|---|
| 70 | //! Friends. | 
|---|
| 71 | friend class Pickup; | 
|---|
| 72 | friend class HealthPickup; | 
|---|
| 73 | friend class InvisiblePickup; | 
|---|
| 74 | friend class MetaPickup; | 
|---|
| 75 | friend class DronePickup; | 
|---|
| 76 | friend class SpeedPickup; | 
|---|
| 77 |  | 
|---|
| 78 | public: | 
|---|
| 79 | PickupCarrier(); //!< Constructor. | 
|---|
| 80 | virtual ~PickupCarrier(); //!< Destructor. | 
|---|
| 81 | void preDestroy(void); | 
|---|
| 82 |  | 
|---|
| 83 | /** | 
|---|
| 84 | @brief Can be used to check whether the PickupCarrier or a child of his is a target ot the input Pickupable. | 
|---|
| 85 | @param pickup A pointer to the Pickupable. | 
|---|
| 86 | @return Returns true if the PickupCarrier or one of its children is a target, false if not. | 
|---|
| 87 | */ | 
|---|
| 88 | bool isTarget(const Pickupable* pickup) | 
|---|
| 89 | { | 
|---|
| 90 | if(pickup->isTarget(this)) //!< If the PickupCarrier itself is a target. | 
|---|
| 91 | return true; | 
|---|
| 92 |  | 
|---|
| 93 | //! Go recursively through all children to check whether they are a target. | 
|---|
| 94 | std::vector<PickupCarrier*>* children = this->getCarrierChildren(); | 
|---|
| 95 | for(std::vector<PickupCarrier*>::const_iterator it = children->begin(); it != children->end(); it++) | 
|---|
| 96 | { | 
|---|
| 97 | if((*it)->isTarget(pickup)) | 
|---|
| 98 | return true; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | children->clear(); | 
|---|
| 102 | delete children; | 
|---|
| 103 |  | 
|---|
| 104 | return false; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | /** | 
|---|
| 108 | @brief Get the carrier that is both a child of the PickupCarrier (or the PickupCarrier itself) and a target of the input Pickupable. | 
|---|
| 109 | @param pickup A pounter to the Pickupable. | 
|---|
| 110 | @return Returns a pointer to the PickupCarrier that is the target of the input Pickupable. | 
|---|
| 111 | */ | 
|---|
| 112 | PickupCarrier* getTarget(const Pickupable* pickup) | 
|---|
| 113 | { | 
|---|
| 114 | if(!this->isTarget(pickup)) | 
|---|
| 115 | return NULL; | 
|---|
| 116 |  | 
|---|
| 117 | if(pickup->isTarget(this)) //!< If the PickupCarrier itself is a target. | 
|---|
| 118 | return this; | 
|---|
| 119 |  | 
|---|
| 120 | //! Go recursively through all children to check whether they are the target. | 
|---|
| 121 | std::vector<PickupCarrier*>* children = this->getCarrierChildren(); | 
|---|
| 122 | for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++) | 
|---|
| 123 | { | 
|---|
| 124 | if(pickup->isTarget(*it)) | 
|---|
| 125 | return *it; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | children->clear(); | 
|---|
| 129 | delete children; | 
|---|
| 130 |  | 
|---|
| 131 | return NULL; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | /** | 
|---|
| 135 | @brief Get the (absolute) position of the PickupCarrier. | 
|---|
| 136 | This method needs to be implemented by any direct derivative class of PickupCarrier. | 
|---|
| 137 | @return Returns the position as a Vector3. | 
|---|
| 138 | */ | 
|---|
| 139 | virtual const Vector3& getCarrierPosition(void) = 0; | 
|---|
| 140 |  | 
|---|
| 141 | protected: | 
|---|
| 142 | /** | 
|---|
| 143 | @brief Get all direct children of this PickupSpawner. | 
|---|
| 144 | This method needs to be implemented by any direct derivative class of PickupCarrier. | 
|---|
| 145 | The returned list will be deleted by the methods calling this function. | 
|---|
| 146 | @return Returns a pointer to a list of all direct children. | 
|---|
| 147 | */ | 
|---|
| 148 | virtual std::vector<PickupCarrier*>* getCarrierChildren(void) = 0; | 
|---|
| 149 | /** | 
|---|
| 150 | @brief Get the parent of this PickupSpawner | 
|---|
| 151 | This method needs to be implemented by any direct derivative class of PickupCarrier. | 
|---|
| 152 | @return Returns a pointer to the parent. | 
|---|
| 153 | */ | 
|---|
| 154 | virtual PickupCarrier* getCarrierParent(void) = 0; | 
|---|
| 155 |  | 
|---|
| 156 | /** | 
|---|
| 157 | @brief Get all Pickupables this PickupCarrier has. | 
|---|
| 158 | @return  Returns the set of all Pickupables this PickupCarrier has. | 
|---|
| 159 | */ | 
|---|
| 160 | std::set<Pickupable*>& getPickups(void) | 
|---|
| 161 | { return this->pickups_; } | 
|---|
| 162 |  | 
|---|
| 163 | private: | 
|---|
| 164 | std::set<Pickupable*> pickups_; //!< The list of Pickupables carried by this PickupCarrier. | 
|---|
| 165 |  | 
|---|
| 166 | /** | 
|---|
| 167 | @brief Adds a Pickupable to the list of pickups that are carried by this PickupCarrier. | 
|---|
| 168 | @param pickup A pointer to the pickup to be added. | 
|---|
| 169 | @return Returns true if successfull, false if the Pickupable was already present. | 
|---|
| 170 | */ | 
|---|
| 171 | bool addPickup(Pickupable* pickup) | 
|---|
| 172 | { | 
|---|
| 173 | COUT(4) << "Adding Pickupable (&" << pickup << ") to PickupCarrier (&" << this << ")" << std::endl; | 
|---|
| 174 | return this->pickups_.insert(pickup).second; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | /** | 
|---|
| 178 | @brief Removes a Pickupable from the list of pickups that are carried by thsi PickupCarrier. | 
|---|
| 179 | @param pickup A pointer to the pickup to be removed. | 
|---|
| 180 | @return Returns true if successfull, false if the Pickupable was not present in the list. | 
|---|
| 181 | */ | 
|---|
| 182 | bool removePickup(Pickupable* pickup) | 
|---|
| 183 | { | 
|---|
| 184 | COUT(4) << "Removing Pickupable (&" << pickup << ") from PickupCarrier (&" << this << ")" << std::endl; | 
|---|
| 185 | return this->pickups_.erase(pickup) == 1; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | }; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | #endif /* _PickupCarrier_H__ */ | 
|---|