Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6592 was 6563, checked in by dafrick, 15 years ago

Some minor changes.

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