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