Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup4/src/orxonox/interfaces/PickupCarrier.h @ 6707

Last change on this file since 6707 was 6632, checked in by dafrick, 15 years ago

Working towards a functioning PickupInventory.

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