Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7010 was 6965, checked in by dafrick, 15 years ago

Cleaned up in PickupInventory, to be able to improve it at a later stage.

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