Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Location:
code/branches/pickup2
Files:
2 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
Note: See TracChangeset for help on using the changeset viewer.