Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/orxonox/interfaces/PickupCarrier.h @ 7150

Last change on this file since 7150 was 7150, checked in by dafrick, 14 years ago

Untangled Pickupable and PickupCarrier a little bit.

  • Property svn:eol-style set to native
File size: 9.3 KB
Line 
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 PickupCarrier.h
31    @brief Definition of the PickupCarrier class.
32*/
33
34#ifndef _PickupCarrier_H__
35#define _PickupCarrier_H__
36
37#include "OrxonoxPrereqs.h"
38
39#include <list>
40#include <set>
41#include "Pickupable.h"
42#include "core/Identifier.h"
43#include "core/WeakPtr.h"
44
45#include "core/OrxonoxClass.h"
46
47namespace orxonox
48{
49
50    //! Forward-declarations.
51    class PickupManager;
52    class Pickup;
53    class HealthPickup;
54    class InvisiblePickup;
55    class MetaPickup;
56    class DronePickup;
57    class SpeedPickup;
58
59    /**
60    @brief
61        The PickupCarrier interface provides the means, for any class implementing it, to possess Pickupables.
62    @author
63        Damian 'Mozork' Frick
64    */
65    class _OrxonoxExport PickupCarrier : virtual public OrxonoxClass
66    {
67        //! So that the different Pickupables have full access to their PickupCarrier.
68        friend class Pickupable;
69        friend class PickupManager;
70        //! Friends.
71        friend class Pickup;
72        friend class HealthPickup;
73        friend class InvisiblePickup;
74        friend class MetaPickup;
75        friend class DronePickup;
76        friend class SpeedPickup;
77
78        public:
79            PickupCarrier(); //!< Constructor.
80            virtual ~PickupCarrier(); //!< Destructor.
81
82            /**
83            @brief Can be used to check whether the PickupCarrier or a child of his is a target ot the input Pickupable.
84            @param pickup A pointer to the Pickupable.
85            @return Returns true if the PickupCarrier or one of its children is a target, false if not.
86            */
87            bool isTarget(const Pickupable* pickup)
88                {
89                    if(pickup->isTarget(this)) //!< If the PickupCarrier itself is a target.
90                        return true;
91
92                    //! Go recursively through all children to check whether they are a target.
93                    std::vector<PickupCarrier*>* children = this->getCarrierChildren();
94                    for(std::vector<PickupCarrier*>::const_iterator it = children->begin(); it != children->end(); it++)
95                    {
96                        if((*it)->isTarget(pickup))
97                            return true;
98                    }
99
100                    children->clear();
101                    delete children;
102
103                    return false;
104                }
105
106            /**
107            @brief Get the carrier that is both a child of the PickupCarrier (or the PickupCarrier itself) and a target of the input Pickupable.
108            @param pickup A pounter to the Pickupable.
109            @return Returns a pointer to the PickupCarrier that is the target of the input Pickupable.
110            */
111            PickupCarrier* getTarget(const Pickupable* pickup)
112                {
113                    if(!this->isTarget(pickup))
114                        return NULL;
115
116                    if(pickup->isTarget(this)) //!< If the PickupCarrier itself is a target.
117                        return this;
118
119                    //! Go recursively through all children to check whether they are the target.
120                    std::vector<PickupCarrier*>* children = this->getCarrierChildren();
121                    for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++)
122                    {
123                        if(pickup->isTarget(*it))
124                            return *it;
125                    }
126
127                    children->clear();
128                    delete children;
129
130                    return NULL;
131                }
132
133            /**
134            @brief Get the (absolute) position of the PickupCarrier.
135                   This method needs to be implemented by any direct derivative class of PickupCarrier.
136            @return Returns the position as a Vector3.
137            */
138            virtual const Vector3& getCarrierPosition(void) = 0;
139
140            /**
141            @brief Get the name of this PickupCarrier.
142            @return Returns the name as a string.
143            */
144            const std::string& getCarrierName(void) { return this->carrierName_; } // tolua_export
145
146        protected:
147            /**
148            @brief Get all direct children of this PickupSpawner.
149                   This method needs to be implemented by any direct derivative class of PickupCarrier.
150                   The returned list will be deleted by the methods calling this function.
151            @return Returns a pointer to a list of all direct children.
152            */
153            virtual std::vector<PickupCarrier*>* getCarrierChildren(void) = 0;
154            /**
155            @brief Get the parent of this PickupSpawner
156                   This method needs to be implemented by any direct derivative class of PickupCarrier.
157            @return Returns a pointer to the parent.
158            */
159            virtual PickupCarrier* getCarrierParent(void) = 0;
160
161            /**
162            @brief Adds a Pickupable to the list of pickups that are carried by this PickupCarrier.
163            @param pickup A pointer to the pickup to be added.
164            @return Returns true if successfull, false if the Pickupable was already present.
165            */
166            bool addPickup(Pickupable* pickup)
167                { return this->pickups_.insert(pickup).second; }
168
169            /**
170            @brief Removes a Pickupable from the list of pickups that are carried by thsi PickupCarrier.
171            @param pickup A pointer to the pickup to be removed.
172            @return Returns true if successfull, false if the Pickupable was not present in the list.
173            */
174            bool removePickup(Pickupable* pickup)
175                { return this->pickups_.erase(pickup) == 1; }
176
177            /**
178            @brief Get all Pickupables this PickupCarrier has.
179            @return  Returns the set of all Pickupables this PickupCarrier has.
180            */
181            std::set<Pickupable*>& getPickups(void)
182                { return this->pickups_; }
183
184            /**
185            @brief Set the name of this PickupCarrier.
186                   The name needs to be set in the constructor of every class inheriting from PickupCarrier, by calling setCarrierName().
187            @param name The name to be set.
188            */
189            void setCarrierName(const std::string& name)
190                { this->carrierName_ = name; }
191
192        private:
193            std::set<Pickupable*> pickups_; //!< The list of Pickupables carried by this PickupCarrier.
194            std::string carrierName_; //!< The name of the PickupCarrier, as displayed in the PickupInventory.
195
196            /**
197            @brief Get the number of carrier children this PickupCarrier has.
198            @return Returns the number of carrier children.
199            */
200            unsigned int getNumCarrierChildren(void)
201                {
202                    std::vector<PickupCarrier*>* list = this->getCarrierChildren();
203                    unsigned int size = list->size();
204                    delete list;
205                    return size;
206                }
207
208            /**
209            @brief Get the index-th child of this PickupCarrier.
210            @param index The index of the child to return.
211            @return Returns the index-th child.
212            */
213            PickupCarrier* getCarrierChild(unsigned int index)
214                {
215                    std::vector<PickupCarrier*>* list = this->getCarrierChildren();
216                    if(list->size() < index)
217                        return NULL;
218                    PickupCarrier* carrier = (*list)[index];
219                    delete list;
220                    return carrier;
221                }
222
223            /**
224            @brief Get the number of Pickupables this PickupCarrier carries.
225            @return returns the number of pickups.
226            */
227            unsigned int getNumPickups(void)
228                { return this->pickups_.size(); }
229
230            /**
231            @brief Get the index-th Pickupable of this PickupCarrier.
232            @param index The index of the Pickupable to return.
233            @return Returns the index-th pickup.
234            */
235            Pickupable* getPickup(unsigned int index)
236                {
237                    std::set<Pickupable*>::iterator it;
238                    for(it = this->pickups_.begin(); index != 0 && it != this->pickups_.end(); it++)
239                        index--;
240                    if(it == this->pickups_.end())
241                        return NULL;
242                    return *it;
243                }
244
245    };
246}
247
248#endif /* _PickupCarrier_H__ */
Note: See TracBrowser for help on using the repository browser.