Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 5, 2010, 6:26:54 PM (15 years ago)
Author:
dafrick
Message:

Additional documentation, code niceifying and potential bug fixing. Also: Renamed DroppedItem to DroppedPickup.

Location:
code/branches/pickup3/src/orxonox
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pickup3/src/orxonox/interfaces/PickupCarrier.h

    r6474 r6475  
    6565            @return Returns true if the Pickupable was picked up, false if not.
    6666            */
    67             inline bool pickup(Pickupable* pickup)
     67            bool pickup(Pickupable* pickup)
    6868                {
    6969                    bool pickedUp = this->pickups_.insert(pickup).second;
     
    7878            @return Returns true if the Pickupable has been dropped, false if not.
    7979            */
    80             inline bool drop(Pickupable* pickup)
     80            bool drop(Pickupable* pickup)
    8181                {
    8282                   bool dropped = this->pickups_.erase(pickup) == 1;
     
    8484                   {
    8585                       pickup->dropped();
    86                         //TODO: Create Spawner.
    8786                   }
    8887                   return dropped;
    8988                }
    9089               
    91             inline bool isTarget(Pickupable* pickup)
     90            /**
     91            @brief Can be used to check whether the PickupCarrier or a child of his is a target ot the input Pickupable.
     92            @param pickup A pointer to the Pickupable.
     93            @return Returns true if the PickupCarrier or one of its children is a target, false if not.
     94            */
     95            //TODO: Use?
     96            bool isTarget(const Pickupable* pickup)
    9297                {
    93                     if(pickup->isTarget(this))
     98                    if(pickup->isTarget(this)) //!< If the PickupCarrier itself is a target.
    9499                        return true;
    95                     const std::list<PickupCarrier*>* children = this->getChildren();
     100                   
     101                    //! Go recursively through all children to check whether they are a target.
     102                    std::list<PickupCarrier*>* children = this->getCarrierChildren();
    96103                    for(std::list<PickupCarrier*>::const_iterator it = children->begin(); it != children->end(); it++)
    97104                    {
     
    100107                    }
    101108                   
     109                    children->clear();
     110                    delete children;
     111                   
    102112                    return false;
    103113                }
     114               
     115            /**
     116            @brief Get the carrier that is both a child of the PickupCarrier (or the PickupCarrier itself) and a target of the input Pickupable.
     117            @param pickup A pounter to the Pickupable.
     118            @return Returns a pointer to the PickupCarrier that is the target of the input Pickupable.
     119            */
     120            PickupCarrier* getTarget(const Pickupable* pickup)
     121                {
     122                    if(!this->isTarget(pickup))
     123                        return NULL;
     124                   
     125                    if(pickup->isTarget(this)) //!< If the PickupCarrier itself is a target.
     126                        return this;
     127                   
     128                    //! Go recursively through all children to check whether they are the target.
     129                    std::list<PickupCarrier*>* children = this->getCarrierChildren();
     130                    for(std::list<PickupCarrier*>::iterator it = children->begin(); it != children->end(); it++)
     131                    {
     132                        if(pickup->isTarget(*it))
     133                            return *it;
     134                    }
     135                   
     136                    children->clear();
     137                    delete children;
     138                   
     139                    return NULL;
     140                }
    104141           
    105         protected:           
    106             //TODO: Good return type?
    107             virtual const std::list<PickupCarrier*>* getChildren(void) = 0;
    108             virtual PickupCarrier* getParent(void) = 0;
     142        protected:       
     143            /**
     144            @brief Get all direct children of this PickupSpawner.
     145                   This method needs to be implemented by any direct derivative class of PickupCarrier.
     146            @return Returns a pointer to a list of all direct children.
     147            */
     148            //TODO: Good return type? Maybe not const and destroyed in isTarget...
     149            virtual std::list<PickupCarrier*>* getCarrierChildren(void) = 0;
     150            /**
     151            @brief Get the parent of this PickupSpawner
     152                   This method needs to be implemented by any direct derivative class of PickupCarrier.
     153            @return Returns a pointer to the parent.
     154            */
     155            virtual PickupCarrier* getCarrierParent(void) = 0;
     156            /**
     157            @brief Get the (absolute) position of the PickupCarrier.
     158                   This method needs to be implemented by any direct derivative class of PickupCarrier.
     159            @return Returns the position as a Vector3.
     160            */
     161            virtual const Vector3& getCarrierPosition(void) = 0;
    109162       
    110163        private:
    111             std::set<Pickupable*> pickups_;
     164            std::set<Pickupable*> pickups_; //!< The list of Pickupables carried by this PickupCarrier.
    112165           
    113        
    114166    };
    115    
    116167}
    117168
  • code/branches/pickup3/src/orxonox/interfaces/Pickupable.cc

    r6474 r6475  
    3636#include "core/Identifier.h"
    3737#include "core/CoreIncludes.h"
     38#include "pickup/PickupIdentifier.h"
    3839#include "PickupCarrier.h"
    3940
     
    5253        this->pickedUp_ = false;
    5354        this->carrier_ = NULL;
     55       
     56        this->pickupIdentifier_ = new PickupIdentifier();
    5457    }
    5558   
     
    9194        Returns true if the given PickupCarrier is a target.
    9295    */
    93     bool Pickupable::isTarget(PickupCarrier* carrier)
     96    bool Pickupable::isTarget(const PickupCarrier* carrier) const
    9497    {
    9598        Identifier* identifier = carrier->getIdentifier();
     
    156159        this->setUsed(false);
    157160        this->setPickedUp(false);
     161       
     162        bool created = this->createSpawner(this->getCarrier()->getCarrierPosition());
     163       
    158164        this->setCarrier(NULL);
     165        if(!created)
     166            this->destroy();
     167       
    159168        return true;
    160169    }
  • code/branches/pickup3/src/orxonox/interfaces/Pickupable.h

    r6474 r6475  
    3939#include <list>
    4040#include "core/Super.h"
    41 #include "pickup/PickupIdentifier.h"
    4241
    4342#include "core/OrxonoxClass.h"
     
    8382            bool dropped(void); //!< Sets the Pickupable to not picked up or dropped.
    8483           
    85             bool isTarget(PickupCarrier* carrier); //!< Get whether the given PickupCarrier is a target of this pickup.
     84            bool isTarget(const PickupCarrier* carrier) const; //!< Get whether the given PickupCarrier is a target of this pickup.
    8685            bool addTarget(PickupCarrier* target); //!< Add a PickupCarrier as target of this pickup.
    8786           
     
    108107            */
    109108            virtual const PickupIdentifier* getPickupIdentifier(void)
    110                 { return &this->pickupIdentifier_; }
     109                { return this->pickupIdentifier_; }
    111110               
    112111            virtual void destroy(void)
     
    120119            void initializeIdentifier(void) {}
    121120           
     121            /**
     122            @brief Facilitates the creation of a PickupSpawner upon dropping of the Pickupable.
     123                   This method must be implemented by any class directly inheriting from Pickupable. It is most easily done by just creating a new DroppedPickup, e.g.:
     124                   DroppedPickup(BaseObject* creator, Pickupable* pickup, const Vector3& position, float triggerDistance);
     125            @param position The position at which the PickupSpawner should be placed.
     126            @return Returns true if a spawner was created, false if not.
     127            */
     128            virtual bool createSpawner(const Vector3& position) = 0;
     129           
    122130            //TODO: Move to private and create get method in protected.
    123             PickupIdentifier pickupIdentifier_; //!< The PickupIdentifier of this Pickupable.
     131            PickupIdentifier* pickupIdentifier_; //!< The PickupIdentifier of this Pickupable.
    124132           
    125133        private:
  • code/branches/pickup3/src/orxonox/pickup/PickupIdentifier.cc

    r6474 r6475  
    3434{
    3535   
     36    /**
     37    @brief
     38        Constructor. Registers the object and initializes member variables.
     39    */
    3640    PickupIdentifier::PickupIdentifier()
    3741    {
    3842        RegisterRootObject(PickupIdentifier);
    3943       
    40        
     44        this->classIdentifier_ = NULL;
    4145    }
    4246   
     
    5256        Pointer to the second PickupIdentifier, b.
    5357    @return
    54         Returns an int.
     58        Returns an integer. 0 if the two compared PickupIdentifiers are the same, <0 if a < b and >0 if a > b.
    5559    */
    56     int PickupIdentifier::compare(const PickupIdentifier& identifier) const
     60    int PickupIdentifier::compare(const PickupIdentifier* identifier) const
    5761    {
    58         if(!identifier.classIdentifier_->isExactlyA(this->classIdentifier_))
    59             return this->classIdentifier_->getName().compare(identifier.classIdentifier_->getName());
     62        //! If the classIdentifiers are not the same (meaning the PickupIdentifiers identify different classes), the obviously the two Pickupables identified by the PickupIdentifiers cannot be the same. An ordering is established through the alphabetical ordering of the respective classnames.
     63        if(!identifier->classIdentifier_->isExactlyA(this->classIdentifier_))
     64            return this->classIdentifier_->getName().compare(identifier->classIdentifier_->getName());
    6065       
    61         if(!(this->parameters_.size() == identifier.parameters_.size()))
     66        //! If the class is the same for both PickupIdentifiers we go on to check the parameters of the class.
     67        //! If the two have a different number of parameters then obviusly something is very wrong.
     68        if(!(this->parameters_.size() == identifier->parameters_.size()))
    6269        {
    6370            COUT(1) << "Something went wrong in PickupIdentifier!" << std::endl;
    64             return this->parameters_.size()-identifier.parameters_.size();
     71            return this->parameters_.size()-identifier->parameters_.size();
    6572        }
    6673       
     74        //! We iterate through all parameters and compar their values (which are strings). The first parameter is the most significant. The ordering is once again established by the alphabetical comparison of the two value strings.
    6775        for(std::map<std::string, std::string>::const_iterator it = this->parameters_.begin(); it != this->parameters_.end(); it++)
    6876        {
    69             if(identifier.parameters_.find(it->first) == identifier.parameters_.end())
     77            //!< If a parameter present in one of the identifiers is not found in the other, once again, something is very wrong.
     78            if(identifier->parameters_.find(it->first) == identifier->parameters_.end())
    7079            {
    7180                COUT(1) << "Something went wrong in PickupIdentifier!" << std::endl;
    7281                return -1;
    7382            }
    74             if(identifier.parameters_.find(it->first)->second != it->second)
    75                 return it->second.compare(identifier.parameters_.find(it->first)->second);
     83            if(identifier->parameters_.find(it->first)->second != it->second)
     84                return it->second.compare(identifier->parameters_.find(it->first)->second);
    7685        }
    7786           
     
    7988    }
    8089   
     90    /**
     91    @brief
     92        Add the class of the Pickupable to its PickupIdentifier.
     93    @param identifier
     94        A pointer to the Identifier of the class.
     95    */
    8196    void PickupIdentifier::addClass(Identifier* identifier)
    8297    {
     
    8499    }
    85100   
     101    /**
     102    @brief
     103        Add a parameter to the PickupIdentifier.
     104    @param name
     105        The name of the parameter.
     106    @param value
     107        The value of the parameter.
     108    @return
     109        Returns false if the parameter already existed, true if not.
     110    */
    86111    bool PickupIdentifier::addParameter(std::string & name, std::string & value)
    87112    {
  • code/branches/pickup3/src/orxonox/pickup/PickupIdentifier.h

    r6474 r6475  
    3232#include "OrxonoxPrereqs.h"
    3333
    34 #include "core/OrxonoxClass.h"
    35 #include "core/Identifier.h"
    3634#include <map>
    3735#include <string>
     36#include "core/Identifier.h"
     37
     38#include "core/OrxonoxClass.h"
    3839
    3940namespace orxonox
    4041{
    4142   
     43    /**
     44    @brief
     45        The purpose of the PickupIdentifier class is to identify different types of pickups allthough they are of the same class.
     46        This allows for more generic classes (or pickups in this case) that can be a number of different pickup types and can be identified as such without the need for a lot of different classes. An example is the HealthPickup class that encompasses a wide variety of different types of health pickups, e.g a HealthPickup that adds 10 health every second for 10 seconds or a HealthPickup that adds 100 health as soon as it is picked up, a.s.o.
     47        To that purpose this class provides functionality to compare two PickupIdentifier (and since all Pickupables have an Identifier through that Pickupables can be compared). It als provides functionality to add parameters that distinguish between different types of pickups in the same pickup class.
     48        Lastly a struct is provided that can be used in stl containers to establish a strictly lesser ordering between PickupIdentifiers (and thus Pickupables).
     49    @author
     50        Damian 'Mozork' Frick
     51    */
    4252    class _OrxonoxExport PickupIdentifier : virtual public OrxonoxClass
    4353    {
    4454       
    4555        public:
    46             PickupIdentifier(void);
    47             ~PickupIdentifier();
     56            PickupIdentifier(void); //!< Constructor.
     57            ~PickupIdentifier(); //!< Destructor.
    4858           
    49             virtual int compare(const PickupIdentifier& identifier) const;
     59            virtual int compare(const PickupIdentifier* identifier) const; //!< Compares two PickupIdentifiers and returns 0 if a == b, <0 if a < b and >0 if a > b for a.compare(b).
    5060           
    51             void addClass(Identifier* identifier);
    52             bool addParameter(std::string & name, std::string & value);
     61            void addClass(Identifier* identifier); //!< Add the class of the Pickupable to its PickupIdentifier.
     62            bool addParameter(std::string & name, std::string & value); //!< Add a parameter to the PickupIdentifier.
    5363           
    5464        private:
    55             Identifier* classIdentifier_;
    56             std::map<std::string, std::string> parameters_;
    57            
     65            Identifier* classIdentifier_; //!< The Identifier of the class.
     66            std::map<std::string, std::string> parameters_; //!< The parameters identifying the type of the pickup beyond the class.
    5867           
    5968    };
    6069   
    61     //TODO: Needed?
     70    /**
     71    @brief
     72        Struct that overloads the compare operation between two PickupIdentifier pointers.
     73    */
    6274    struct PickupIdentifierCompare
    6375    {
    64         bool operator() (const PickupIdentifier& lhs, const PickupIdentifier& rhs) const
    65             { return lhs.compare(rhs) < 0; }
    66     };
    67    
    68     struct PickupIdentifierPtrCompare
    69     {
    7076        bool operator() (const PickupIdentifier* lhs, const PickupIdentifier* rhs) const
    71             { return lhs->compare(*rhs) < 0; }
     77            { return lhs->compare(rhs) < 0; }
    7278    };
    7379   
  • code/branches/pickup3/src/orxonox/worldentities/pawns/Pawn.h

    r6466 r6475  
    138138            //TODO: Remove.
    139139            //PickupCollection pickups_;
    140             virtual const std::list<PickupCarrier*>* getChildren(void)
     140            virtual std::list<PickupCarrier*>* getCarrierChildren(void)
    141141                { return new std::list<PickupCarrier*>(); }
    142             virtual PickupCarrier* getParent(void)
     142            virtual PickupCarrier* getCarrierParent(void)
    143143                { return NULL; }
     144            virtual const Vector3& getCarrierPosition(void)
     145                { return this->getWorldPosition(); };
    144146
    145147            float health_;
Note: See TracChangeset for help on using the changeset viewer.