Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/environment/src/orxonox/interfaces/PickupCarrier.cc @ 8328

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

Some documenting and cleaning up/re-organization in pickups module.

File size: 5.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.cc
31    @brief Implementation of the PickupCarrier class.
32*/
33
34#include "PickupCarrier.h"
35
36#include "core/CoreIncludes.h"
37#include "core/Identifier.h"
38#include "Pickupable.h"
39
40namespace orxonox {
41
42    /**
43    @brief
44        Constructor. Registers the object.
45    */
46    PickupCarrier::PickupCarrier()
47    {
48        RegisterRootObject(PickupCarrier);
49    }
50
51    /**
52    @brief
53        Destructor.
54    */
55    PickupCarrier::~PickupCarrier()
56    {
57
58    }
59
60    /**
61    @brief
62        Is called before the PickupCarrier is effectively destroyed.
63    */
64    void PickupCarrier::preDestroy(void)
65    {
66        std::set<Pickupable*>::iterator it = this->pickups_.begin();
67        std::set<Pickupable*>::iterator temp;
68        while(it != this->pickups_.end())
69        {
70            (*it)->carrierDestroyed();
71            temp = it;
72            it = this->pickups_.begin();
73            if(it == temp) // Infinite loop avoidance, in case the pickup wasn't removed from the carrier somewhere in the carrierDestroy() procedure.
74            {
75                COUT(2) << "Oops. In a PickupCarrier, while cleaning up, a Pickupable (&" << (*temp) << ") didn't unregister itself as it should have." << std::endl;;
76                it++;
77            }
78        }
79
80        this->pickups_.clear();
81    }
82
83    /**
84    @brief
85        Can be used to check whether the PickupCarrier or a child of his is a target ot the input Pickupable.
86    @param pickup
87        A pointer to the Pickupable.
88    @return
89        Returns true if the PickupCarrier or one of its children is a target, false if not.
90    */
91    bool PickupCarrier::isTarget(const Pickupable* pickup)
92    {
93        if(pickup->isTarget(this)) // If the PickupCarrier itself is a target.
94            return true;
95
96        // Go recursively through all children to check whether they are a target.
97        std::vector<PickupCarrier*>* children = this->getCarrierChildren();
98        for(std::vector<PickupCarrier*>::const_iterator it = children->begin(); it != children->end(); it++)
99        {
100            if((*it)->isTarget(pickup))
101                return true;
102        }
103
104        children->clear();
105        delete children;
106
107        return false;
108    }
109
110    /**
111    @brief
112        Get the carrier that is both a child of the PickupCarrier (or the PickupCarrier itself) and a target of the input Pickupable.
113    @param pickup
114        A pounter to the Pickupable.
115    @return
116        Returns a pointer to the PickupCarrier that is the target of the input Pickupable.
117    */
118    PickupCarrier* PickupCarrier::getTarget(const Pickupable* pickup)
119    {
120        if(!this->isTarget(pickup))
121            return NULL;
122
123        if(pickup->isTarget(this)) // If the PickupCarrier itself is a target.
124            return this;
125
126        // Go recursively through all children to check whether they are the target.
127        std::vector<PickupCarrier*>* children = this->getCarrierChildren();
128        for(std::vector<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++)
129        {
130            if(pickup->isTarget(*it))
131                return *it;
132        }
133
134        children->clear();
135        delete children;
136
137        return NULL;
138    }
139
140    /**
141    @brief
142        Adds a Pickupable to the list of pickups that are carried by this PickupCarrier.
143    @param pickup
144        A pointer to the pickup to be added.
145    @return
146        Returns true if successfull, false if the Pickupable was already present.
147    */
148    bool PickupCarrier::addPickup(Pickupable* pickup)
149    {
150        COUT(4) << "Adding Pickupable (&" << pickup << ") to PickupCarrier (&" << this << ")" << std::endl;
151        return this->pickups_.insert(pickup).second;
152    }
153
154    /**
155    @brief
156        Removes a Pickupable from the list of pickups that are carried by this PickupCarrier.
157    @param pickup
158        A pointer to the pickup to be removed.
159    @return
160        Returns true if successfull, false if the Pickupable was not present in the list.
161    */
162    bool PickupCarrier::removePickup(Pickupable* pickup)
163    {
164        COUT(4) << "Removing Pickupable (&" << pickup << ") from PickupCarrier (&" << this << ")" << std::endl;
165        return this->pickups_.erase(pickup) == 1;
166    }
167
168}
Note: See TracBrowser for help on using the repository browser.