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