Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/interfaces/PickupCarrier.h @ 6710

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

Merged ppspickups2 branch into trunk.

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