Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/chat/src/orxonox/interfaces/PickupCarrier.h @ 6998

Last change on this file since 6998 was 6524, checked in by dafrick, 15 years ago

Merged pickup branch into trunk. Yay. Persisting bugs will be fixed, very soon.

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