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