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