Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/PickupCollectionIdentifier.cc @ 9290

Last change on this file since 9290 was 9290, checked in by landauf, 12 years ago

Fixed crash with MSVC if a PickupCollection was used
A depleted CollectiblePickup is now destroyed instead of being dropped
A destroyed CollectiblePickup removes itself from the PickupCollection
PickupCollection has to use a list instead of a vector because of this reason
Also PickupCollectionIdentifier needed to be changed because the number of pickups in a collection may now change
Probably also fixed a bug in PickupCollectionIdentifier::compare() because it2 was not incremented

not completely clean yet

  • Property svn:eol-style set to native
File size: 3.9 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 PickupCollectionIdentifier.cc
31    @brief Implementation of PickupCollectionIdentifier.
32*/
33
34#include "core/CoreIncludes.h"
35
36#include "PickupCollectionIdentifier.h"
37#include "PickupCollection.h"
38
39namespace orxonox
40{
41
42    /**
43    @brief
44        Constructor. Registers the object.
45    */
46    PickupCollectionIdentifier::PickupCollectionIdentifier(PickupCollection* collection) : PickupIdentifier(collection)
47    {
48        RegisterObject(PickupCollectionIdentifier);
49
50        this->collection_ = collection;
51    }
52
53    /**
54    @brief
55        Destructor.
56    */
57    PickupCollectionIdentifier::~PickupCollectionIdentifier()
58    {
59
60    }
61
62    /**
63    @brief
64        Compares a PickupCollectionIdentifier with a PickupIdentifier and returns 0 if a == b, <0 if a < b and >0 if a > b for a.compare(b), where a is a PickupCollectionIdentifier and b is just some PickupIdentifier.
65    @param identifier
66        Pointer to the second PickupIdentifier, b.
67    @return
68        Returns an integer. 0 if the two compared PickupIdentifiers are the same, <0 if a < b and >0 if a > b.
69    */
70    int PickupCollectionIdentifier::compare(const PickupIdentifier* identifier) const
71    {
72        assert(identifier);
73
74        // Slight un-niceity to cast the const PickupIdentifier to a const PickupCollectionIdentifier, but since we cast to a const, there is no harm done.
75        PickupIdentifier* temp = const_cast<PickupIdentifier*>(identifier);
76        const PickupCollectionIdentifier* collectionIdentifier = orxonox_cast<PickupCollectionIdentifier*>(temp);
77
78        // If the input PickupIdentifier 'identifier' is no PickupCollectionIdentifier then just the two PickupIdentifiers are compared.
79        if(collectionIdentifier == NULL)
80        {
81            return this->PickupIdentifier::compare(identifier);
82        }
83
84        // If the number of Pickupables each of the two PickupCollectionIdentifiers contain differ, the one with less is considered smaller.
85        if(this->collection_->getPickups().size() != collectionIdentifier->collection_->getPickups().size())
86            return this->collection_->getPickups().size()-collectionIdentifier->collection_->getPickups().size();
87
88        // Compare the Pickupables of the two PickupCollectionIdentifiers one after the other. the one with the first 'smaller' one is considered smaller.
89        std::list<CollectiblePickup*>::const_iterator it1 = this->collection_->getPickups().begin();
90        std::list<CollectiblePickup*>::const_iterator it2 = collectionIdentifier->collection_->getPickups().begin();
91        for( ; it1 != this->collection_->getPickups().end(); ++it1, ++it2)
92        {
93            const PickupIdentifier* id1 = (*it1)->getPickupIdentifier();
94            const PickupIdentifier* id2 = (*it2)->getPickupIdentifier();
95
96            if(id1->compare(id2) < 0)
97                return -1;
98            if(id2->compare(id1) < 0)
99                return 1;
100        }
101
102        // This means they are equal.
103        return 0;
104    }
105
106}
107
Note: See TracBrowser for help on using the repository browser.