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