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/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
Note: See TracChangeset for help on using the changeset viewer.