Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/map/src/orxonox/interfaces/PickupCarrier.h

Last change on this file was 6711, checked in by dafrick, 15 years ago

Merged pickup4 branch back to trunk.

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