Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5953


Ignore:
Timestamp:
Oct 14, 2009, 4:36:56 PM (15 years ago)
Author:
dafrick
Message:

Made DroppedItem inherit from PickupSpawner. Also minor changes in PickupSpawner.

Location:
code/branches/pickup2
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pickup2

  • code/branches/pickup2/src/orxonox/pickup/DroppedItem.cc

    r5947 r5953  
    3939namespace orxonox
    4040{
    41     CreateFactory(DroppedItem);
     41    CreateFactory(DroppedItem); //TODO: This isn't needed, is it?
    4242
    4343    /**
     
    4545        Constructor. Registers object and sets default values.
    4646    */
    47     DroppedItem::DroppedItem(BaseObject* creator) : StaticEntity(creator)
     47    DroppedItem::DroppedItem(BaseObject* creator) : PickupSpawner(creator)
    4848    {
    4949        RegisterObject(DroppedItem);
     50    }
    5051
    51         this->triggerDistance_ = 20.0f;
    52         this->timeToLive_ = 0;
    53         this->item_ = NULL;
     52    DroppedItem::DroppedItem(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems) : PickupSpawner(creator, item, triggerDistance, respawnTime, maxSpawnedItems)
     53    {
     54        RegisterObject(DroppedItem);
     55        this->item_ = item;
    5456    }
    5557
     
    5860        Default destructor.
    5961    */
    60     //TODO: Destroy something?
    6162    DroppedItem::~DroppedItem()
    6263    {
     
    6465    }
    6566
    66     /**
    67     @brief
    68         Checks whether any pawn is in triggerDistance of the Item and calls this->trigger if so.
    69     @param dt
    70         The  duration of the last time interval.   
    71     */
    72     //TODO: Replace this with a DistanceTrigger!
    73     void DroppedItem::tick(float dt)
     67    BaseItem* DroppedItem::getItem(void)
    7468    {
    75         if (this->item_)
    76         {
    77             for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) //!< Iterate through all Pawns.
    78             {
    79                 Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
    80                 if (distance.length() < this->triggerDistance_)
    81                     this->trigger(*it);
    82             }
    83         }
    84     }
    85 
    86     /**
    87     @brief
    88         Called when the DroppedItem is triggered. Adds the item to the triggering pawn.
    89     */
    90     void DroppedItem::trigger(Pawn* pawn)
    91     {
    92         if (this->item_->pickedUp(pawn)) //If pickup was successful.
    93         {
    94             COUT(3) << "DroppedItem '" << this->item_->getPickupIdentifier() << "' picked up." << std::endl;
    95             this->destroy();
    96         }
    97     }
    98 
    99     /**
    100     @brief
    101         Creates a timer to call this->timerCallback() at expiration of timeToLive.
    102     */
    103     //TODO: Better Comments.
    104     void DroppedItem::createTimer()
    105     {
    106         if (this->timeToLive_ > 0)
    107         {
    108             this->timer_.setTimer(this->timeToLive_, false, createExecutor(createFunctor(&DroppedItem::timerCallback, this)), false);
    109         }
    110     }
    111    
    112     /**
    113     @brief
    114         Destroys the item. Called by the set timer upon its expiration.
    115     */
    116     //TODO: Choose better function name if this doesn't create dependency inconsistencies. e.g. this->destroy() or this->timeOut()
    117     //Make certain that only one pawn has the same item, because if not, deliting the item would lead to a possible segfault.
    118     //If the item is destroyed here, shouldn't it be destroyed in the destructor as well?
    119     void DroppedItem::timerCallback()
    120     {
    121         if (this->item_)
    122         {
    123             COUT(3) << "Delete DroppedItem with '" << this->item_->getPickupIdentifier() << "'" << std::endl;
    124             this->item_->destroy();
    125         }
    126 
    127         this->destroy();
     69        return this->item_;
    12870    }
    12971
     
    13375    */
    13476    //TODO: Comment.
    135     //This is for pawns dropping items they have...
    136     //Probably better to create a spawner with only 1 item in it.
    137     //Various different thigs are done here, which in my opinion should eighter be done in XML or some where else, preferably in XML.
    13877    //Each pickup should have a XML template where the Model and Billboard, and so on, is specified.
    139     //The position, item and timetoLive should be specified by this Classes XMLPort function.
    140     //These adjustments above, will very likely create inkonsistencies in the level files, possibly templates.
    14178    /*static*/ DroppedItem* DroppedItem::createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour, float timeToLive)
    14279    {
    143         DroppedItem* drop = new DroppedItem(item);
     80        //TODO: triggerDistance?
     81        float triggerDistance = 20.0;
     82        DroppedItem* droppedItem = new DroppedItem(item, item, triggerDistance, 0, 1);
     83       
     84        //TODO: Do this somehwere else?
    14485        Model* model = new Model(item);
    14586        Billboard* billboard = new Billboard(item);
     
    15293        billboard->setScale(0.5f);
    15394
    154         drop->setPosition(position);
    155         drop->attach(model);
    156         drop->attach(billboard);
    157 
    158         drop->setItem(item);
    159 
    160         drop->setTimeToLive(timeToLive);
    161         drop->createTimer();
     95        droppedItem->setPosition(position);
     96        droppedItem->attach(model);
     97        droppedItem->attach(billboard);
    16298
    16399        COUT(3) << "Created DroppedItem for '" << item->getPickupIdentifier() << "' at (" << position.x << "," << position.y << "," << position.z << ")." << std::endl;
    164100
    165         return drop;
     101        return droppedItem;
    166102    }
    167103
  • code/branches/pickup2/src/orxonox/pickup/DroppedItem.h

    r5947 r5953  
    3737#include "OrxonoxPrereqs.h"
    3838
    39 #include "tools/Timer.h"
    40 #include "tools/interfaces/Tickable.h"
    41 #include "worldentities/StaticEntity.h"
     39#include "PickupSpawner.h"
    4240
    4341namespace orxonox
    4442{
    45     class _OrxonoxExport DroppedItem : public StaticEntity, public Tickable
     43    class _OrxonoxExport DroppedItem : public PickupSpawner
    4644    {
    47     public:
    48         DroppedItem(BaseObject* creator);
    49         virtual ~DroppedItem();
     45        public:
     46            DroppedItem(BaseObject* creator);
     47            DroppedItem(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems);
     48            virtual ~DroppedItem();
    5049
    51         //TODO: Comment.
    52         //DroppedItem -> Item with no owner, alone in space?
    53         //Would be much nicer if it would be triggered by a standard issue DistanceTrigger.
    54         //Where is this created? I see no XMLPort.
    55         //Where is the item for this created? What happens if more than one pawn triggers this?
    56         //Add more than just one items, or even better create the ability to add a Collection.? Rename to ...?
     50            static DroppedItem* createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0);
     51            static DroppedItem* createDefaultDrop(BaseItem* item, Pawn* pawn, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0);
    5752
    58         void tick(float dt);
    59         void trigger(Pawn* pawn);
     53        protected:
     54            virtual BaseItem* getItem(void);
    6055
    61         static DroppedItem* createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0);
    62         static DroppedItem* createDefaultDrop(BaseItem* item, Pawn* pawn, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0);
     56        private:
    6357
    64         void createTimer(); //TODO: Can this be made private, too?
    65         void timerCallback(); //TODO: This should really be private.
     58            BaseItem* item_; //!< The dropped item.
    6659
    67         inline float getTriggerDistance() const
    68             { return this->triggerDistance_; }
    69         inline void setTriggerDistance(float distance)
    70             { this->triggerDistance_ = distance; }
    71 
    72         inline BaseItem* getItem() const
    73             { return this->item_; }
    74         //TODO: Needs to be public?
    75         inline void setItem(BaseItem* item)
    76             { this->item_ = item; }
    77 
    78         inline float getTimeToLive() const
    79             { return this->timeToLive_; }
    80         //TODO: Needs to be public?
    81         inline void setTimeToLive(float time)
    82             { this->timeToLive_ = time; }
    83     private:
    84         float timeToLive_;
    85         float triggerDistance_;
    86         BaseItem* item_;
    87 
    88         Timer timer_;
    8960    };
    9061}
  • code/branches/pickup2/src/orxonox/pickup/PickupSpawner.cc

    r5947 r5953  
    4646namespace orxonox
    4747{
     48
    4849    const float PickupSpawner::bounceSpeed_s = 6.0f;
    4950    const float PickupSpawner::rotationSpeed_s = 1.0f;
     
    6061    PickupSpawner::PickupSpawner(BaseObject* creator) : StaticEntity(creator)
    6162    {
     63        this->initialize();
     64    }
     65
     66    PickupSpawner::PickupSpawner(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems) : StaticEntity(creator)
     67    {
     68        this->initialize();
     69 
     70        //TODO: Does this actually work?
     71        this->itemTemplateName_ = item->getIdentifier()->getName();
     72        this->itemTemplate_ = Template::getTemplate(this->itemTemplateName_);
     73
     74        this->triggerDistance_ = triggerDistance;
     75        this->respawnTime_ = respawnTime;
     76        this->setMaxSpawnedItems(maxSpawnedItems);
     77    }
     78
     79    void PickupSpawner::initialize(void)
     80    {
    6281        RegisterObject(PickupSpawner);
    6382
    64         this->itemTemplate_ = 0;
     83        this->itemTemplate_ = NULL;
    6584        this->triggerDistance_ = 20;
    6685        this->respawnTime_ = 0.0f;
    6786        this->tickSum_ = 0.0f;
     87        this->maxSpawnedItems_ = INF;
     88        this->spawnsRemaining_ = INF;
    6889    }
    6990
     
    92113        XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode);
    93114        XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode);
     115        XMLPortParam(PickupSpawner, "maxSpawnedItems", setMaxSpawnedItems, getMaxSpawnedItems, xmlelement, mode);
    94116
    95117        //TODO: Kill hack.
     
    137159    }
    138160
     161    void PickupSpawner::setMaxSpawnedItems(int items)
     162    {
     163        this->maxSpawnedItems_ = items;
     164        this->spawnsRemaining_ = items;
     165    }
     166
    139167    /**
    140168    @brief
     
    148176        if (this->isActive())
    149177        {
     178            //! Triggers as soon as a Pawn is in the specified distance.
    150179            for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
    151180            {
     
    154183                    this->trigger(*it);
    155184            }
     185
     186            //! Animation.
    156187            this->yaw(Radian(rotationSpeed_s*dt));
    157188            this->tickSum_ += bounceSpeed_s*dt;
     
    174205    void PickupSpawner::trigger(Pawn* pawn)
    175206    {
    176         if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier())
    177         {
    178             BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this);
    179             BaseItem* asItem = orxonox_cast<BaseItem*>(newObject);
    180             if (asItem)
     207        if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier()) //!< Checks whether PickupItem is active, amongst other things.
     208        {
     209            BaseItem* item = this->getItem();
     210            if (item != NULL) //!< If the conversion was successful.
    181211            {
    182                 asItem->setPickupIdentifier(this->itemTemplateName_);
    183                 asItem->addTemplate(this->itemTemplate_);
    184 
    185                 if (asItem->pickedUp(pawn))
     212                item->setPickupIdentifier(this->itemTemplateName_); //TODO: Needed?
     213                item->addTemplate(this->itemTemplate_); //TODO: Does what?
     214
     215                if(item->pickedUp(pawn))
    186216                {
    187217                    COUT(3) << this->itemTemplateName_ << " got picked up." << std::endl;
    188218
    189                     if (this->respawnTime_ > 0.0f)
     219
     220                    if(this->spawnsRemaining_ != INF)
     221                    {
     222                        this->spawnsRemaining_--;
     223                    }
     224
     225                    if (this->spawnsRemaining_ != 0 && this->respawnTime_ > 0.0f)
    190226                    {
    191227                        this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this)));
     
    196232                }
    197233                else
    198                     newObject->destroy();
     234                {
     235                    item->destroy();
     236                }
    199237            }
    200238        }
     239
     240        if(this->spawnsRemaining_ == 0)
     241        {
     242            COUT(3) << "PickupSpawner empty, selfdistruct initialized." << std::endl;
     243            this->setActive(false);
     244            this->destroy();
     245        }
     246    }
     247
     248    /**
     249    @brief
     250        Creates a BaseItem of the type specified by the PickupSpawner.
     251    @return
     252        The BaseItem created.
     253    */   
     254    BaseItem* PickupSpawner::getItem(void)
     255    {
     256        BaseObject* newItem = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); //!< Creates new object of specified item type.
     257        return orxonox_cast<BaseItem*>(newItem);
    201258    }
    202259
  • code/branches/pickup2/src/orxonox/pickup/PickupSpawner.h

    r5947 r5953  
    5353            //TODO: Add limit of items spawned here. Also possibility to spawn collections?
    5454            PickupSpawner(BaseObject* creator);
     55            PickupSpawner(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems);
    5556            virtual ~PickupSpawner();
    5657
     
    102103            inline void setRespawnTime(float time)
    103104                { this->respawnTime_ = time; }
     105
     106
     107            inline int getMaxSpawnedItems(void)
     108                { return this->maxSpawnedItems_; }
     109            void setMaxSpawnedItems(int items);
     110
     111        protected:
     112            virtual BaseItem* getItem(void);
     113
    104114        private:
     115            void initialize(void);
     116
    105117            std::string itemTemplateName_;          //!< Template name of the item to spawn.
    106118            Template* itemTemplate_;                //!< Template of the item to spawn.
     119
     120            int maxSpawnedItems_;                   //!< Maximum number of items spawned by this PickupSpawner.
     121            int spawnsRemaining_;                   //!< Number of items that can be spawned by this PickupSpawner until it selfdestructs.
    107122
    108123            float triggerDistance_;                 //!< Distance in which this gets triggered.
     
    116131            float respawnTime_;                     //!< Time after which this gets re-actived.
    117132            Timer respawnTimer_;                    //!< Timer used for re-activating.
     133
     134            static const int INF = -1;             //!< Constant for infinity.
    118135    };
    119136}
Note: See TracChangeset for help on using the changeset viewer.