Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pickup3/src/modules/pickup/PickupSpawner.cc

    r6474 r6475  
    3535
    3636#include "core/CoreIncludes.h"
    37 //#include "core/GUIManager.h"     // HACK; see below
    3837#include "core/Template.h"
    3938#include "core/XMLPort.h"
     
    4140#include "PickupManager.h"
    4241#include "PickupRepresentation.h"
    43 //#include "PickupInventory.h"    // HACK; Only for hack, remove later
    44 
    4542
    4643namespace orxonox
    4744{
    48 
    49 //     const float PickupSpawner::bounceSpeed_s = 6.0f;
    50 //     const float PickupSpawner::rotationSpeed_s = 1.0f;
    51 //     const float PickupSpawner::bounceDistance_s = 4.0f;
    5245
    5346    CreateFactory(PickupSpawner);
     
    6457       
    6558        this->initialize();
    66        
    67         PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(NULL);
    68         this->attach(representation->getSpawnerRepresentation(this));
    6959    }
    7060
     
    9585        this->setMaxSpawnedItems(maxSpawnedItems);
    9686       
    97         PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier());
    98         this->attach(representation->getSpawnerRepresentation(this));
     87        if(this->pickup_ == NULL)
     88        {
     89            COUT(2) << "A PickupSpawner was created without a valid Pickupable. This won't work." << std::endl;
     90            this->setActive(false);
     91        }
     92        else
     93        {
     94            PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier());
     95            this->attach(representation->getSpawnerRepresentation(this));
     96        }
    9997    }
    10098
     
    109107        this->triggerDistance_ = 20;
    110108        this->respawnTime_ = 0;
    111         this->tickSum_ = 0;
    112109        this->maxSpawnedItems_ = INF;
    113110        this->spawnsRemaining_ = INF;
     
    141138        XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode);
    142139        XMLPortParam(PickupSpawner, "maxSpawnedItems", setMaxSpawnedItems, getMaxSpawnedItems, xmlelement, mode);
    143 
    144         //TODO: Kill hack.
    145         // HACKs
    146         // Load the GUI image as soon as the PickupSpawner gets loaded
    147         //  = less delays while running
    148 //         BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this);
    149 //         BaseItem* asItem = orxonox_cast<BaseItem*>(newObject);
    150 //         if (asItem)
    151 //         {
    152 //             asItem->addTemplate(this->itemTemplate_);
    153 //             PickupInventory::getImageForItem(asItem);
    154 //             newObject->destroy();
    155 //         }
    156 
    157         //  & load the GUI itself too, along with some empty windows
    158         //   = even less delays
    159 //         GUIManager::getInstance().showGUI("PickupInventory");
    160 //         GUIManager::getInstance().executeCode("hideGUI(\"PickupInventory\")");
    161 //         PickupInventory::getSingleton();
     140       
     141        if(this->pickup_ == NULL)
     142        {
     143            COUT(2) << "A PickupSpawner was created without a valid Pickupable. This won't work." << std::endl;
     144            this->setActive(false);
     145        }
     146        else
     147        {
     148            PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier());
     149            this->attach(representation->getSpawnerRepresentation(this));
     150        }
     151    }
     152   
     153    /**
     154    @brief
     155        Invoked when the activity has changed. Sets visibility of attached objects.
     156    */
     157    void PickupSpawner::changedActivity()
     158    {
     159        SUPER(PickupSpawner, changedActivity);
     160
     161        for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++)
     162        {
     163            (*it)->setVisible(this->isActive());
     164        }
     165    }
     166     
     167    /**
     168    @brief
     169        Tick, checks if any Pawn is close enough to trigger.
     170    @param dt
     171        Time since last tick.
     172    */
     173    //TODO: Replace this with a real DistanceTrigger? Or better with collisions?
     174    void PickupSpawner::tick(float dt)
     175    {
     176        //! If the PickupSpawner is active.
     177        if (this->isActive())
     178        {
     179            //! Iterate trough all Pawns.
     180            for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
     181            {
     182                Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
     183                //! If a Pawn, that fits the target-range of the item spawned by this Pickup, is in trigger-distance.
     184                if (distance.length() < this->triggerDistance_ && this->pickup_->isTarget(*it))
     185                {
     186                    this->trigger(*it);
     187                }
     188            }
     189        }
     190    }
     191   
     192    /**
     193    @brief
     194        Sets the maximum number of spawned items.
     195    @param items
     196        The maximum number of spawned items to be set.
     197    */
     198    void PickupSpawner::setMaxSpawnedItems(int items)
     199    {
     200        this->maxSpawnedItems_ = items;
     201        this->spawnsRemaining_ = items;
     202    }
     203   
     204    /**
     205    @brief
     206        Decrements the number of remaining spawns.
     207        Sets the PickupSpawner to inactive for the duration of the respawnTime.
     208        Destroys the PickupSpawner if the number of remaining spawns has reached zero.
     209    */
     210    void PickupSpawner::decrementSpawnsRemaining(void)
     211    {
     212        if(this->spawnsRemaining_ != INF)
     213        {
     214            this->spawnsRemaining_--;
     215        }
     216        if(this->spawnsRemaining_ != 0 && this->respawnTime_ > 0)
     217        {
     218            //TODO: Nicer? Does this even work?
     219            this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this)));
     220
     221            this->setActive(false);
     222            this->fireEvent();
     223        }
     224        else
     225        {
     226            COUT(3) << "PickupSpawner empty, selfdestruct initialized." << std::endl;
     227            this->setActive(false);
     228            this->destroy();
     229        }
    162230    }
    163231   
     
    172240        if(this->pickup_ != NULL)
    173241        {
    174             COUT(1) << "addPickupable called, with this->pickup_ already set." << std::endl;
     242            COUT(1) << "In PickupSpawner: setPickupable called, with this->pickup_ already set." << std::endl;
    175243            return;
    176244        }
    177245        if(pickup == NULL)
    178246        {
    179             COUT(1) << "Argument of addPickupable is NULL." << std::endl;
     247            COUT(1) << "In PickupSpawner: Argument of setPickupable is NULL." << std::endl;
    180248            return;
    181249        }
     
    197265    /**
    198266    @brief
    199         Invoked when the activity has changed. Sets visibility of attached objects.
    200     */
    201 //     void PickupSpawner::changedActivity()
    202 //     {
    203 //         SUPER(PickupSpawner, changedActivity);
    204 //
    205 //         for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++)
    206 //             (*it)->setVisible(this->isActive());
    207 //     }
    208 
    209     /**
    210     @brief
    211         Sets the maximum number of spawned items.
    212     @param items
    213         The maximum number of spawned items to be set.
    214     */
    215     void PickupSpawner::setMaxSpawnedItems(int items)
    216     {
    217         this->maxSpawnedItems_ = items;
    218         this->spawnsRemaining_ = items;
    219     }
    220 
    221     /**
    222     @brief
    223         Tick, checks if any Pawn is close enough to trigger.
    224     @param dt
    225         Time since last tick.
    226     */
    227     //TODO: Replace this with a real DistanceTrigger? Or better with collisions?
    228     void PickupSpawner::tick(float dt)
    229     {
    230         //! If the PickupSpawner is active.
    231         if (this->isActive())
    232         {
    233             //! Iterate trough all Pawns.
    234             for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
    235             {
    236                 Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
    237                 //! If a Pawn, that fits the target-range of the item spawned by this Pickup, is in trigger-distance.
    238                 if (distance.length() < this->triggerDistance_ && this->pickup_->isTarget(*it))
    239                 {
    240                     this->trigger(*it);
    241                 }
    242             }
    243 
    244             //! Animation.
    245 //             this->yaw(Radian(rotationSpeed_s*dt));
    246 //             this->tickSum_ += bounceSpeed_s*dt;
    247 //             this->translate(Vector3(0,bounceDistance_s*dt*sin(this->tickSum_),0));
    248 //             if (this->tickSum_ > 2*Ogre::Math::PI)
    249 //                 this->tickSum_ -= 2*Ogre::Math::PI;
    250         }
    251     }
    252 
    253     /**
    254     @brief
    255267        Trigger the PickupSpawner.
    256 
    257         Adds the pickup to the Pawn that triggered,
    258         sets the timer to re-activate and deactives the PickupSpawner.
    259 
     268        Adds the pickup to the Pawn that triggered, sets the timer to re-activate and deactives the PickupSpawner.
    260269    @param pawn
    261270        Pawn which triggered the PickupSpawner.
    262271    */
     272    //TODO: Make more generic -> without pawn.
    263273    void PickupSpawner::trigger(Pawn* pawn)
    264274    {
    265         //TODO: If private, isActive doesn't need to be tested anymore.
    266275        if (this->isActive()) //!< Checks whether PickupItem is active.
    267276        {
     277            PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(pawn);
     278            if(carrier == NULL)
     279            {
     280                COUT(1) << "This is bad. Pawn isn't PickupCarrier." << std::endl;
     281                return;
     282            }
     283           
     284            if(!carrier->isTarget(this->pickup_))
     285            {
     286                COUT(4) << "PickupSpawner triggered but Pawn wasn't a target of the Pickupable." << std::endl;
     287                return;
     288            }
     289           
     290            PickupCarrier* target = carrier->getTarget(this->pickup_);
    268291            Pickupable* pickup = this->getPickup();
    269             if (pickup != NULL) //!< If everything went ok, and pickup is not NULL.
    270             {
    271                 //TODO: Not correct anymore.
    272                 PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(pawn);
    273                 if(carrier == NULL)
    274                 {
    275                     COUT(1) << "This is bad. Pawn isn't PickupCarrier." << std::endl;
    276                     return;
    277                 }
     292           
     293            if(target != NULL || pickup != NULL)
     294            {
     295                if(carrier->pickup(pickup))
     296                {
     297                    this->decrementSpawnsRemaining();
     298                }
     299                else
     300                {
     301                    pickup->destroy();
     302                }
     303            }
     304            else
     305            {
     306                //TODO: Really that severe?
     307                if(target == NULL)
     308                    COUT(1) << "PickupSpawner: Pickupable has no target." << std::endl;
    278309               
    279                 if(carrier->pickup(pickup))
    280                 {
    281                     COUT(3) << "Pickup got picked up." << std::endl;
    282 
    283                     this->decrementSpawnsRemaining();
     310                if(pickup == NULL)
     311                {
     312                    COUT(1) << "PickupSpawner: getPickup produced an error, no Pickupable created." << std::endl;
    284313                }
    285314                else
     
    288317                }
    289318            }
    290         }
    291     }
    292    
    293     /**
    294     @brief
    295         Decrements the number of remaining spawns.
    296         Sets the PickupSpawner to inactive for the duration of the respawnTime.
    297         Destroys the PickupSpawner if the number of remaining spawns has reached zero.
    298        
    299     */
    300     void PickupSpawner::decrementSpawnsRemaining(void)
    301     {
    302         if(this->spawnsRemaining_ != INF)
    303         {
    304             this->spawnsRemaining_--;
    305         }
    306         if(this->spawnsRemaining_ != 0 && this->respawnTime_ > 0)
    307         {
    308             //TODO: Nicer? Does this even work?
    309             this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this)));
    310 
    311             this->setActive(false);
    312             this->fireEvent();
    313         }
    314         else
    315         {
    316             COUT(3) << "PickupSpawner empty, selfdistruct initialized." << std::endl;
    317             this->setActive(false);
    318             this->destroy(); //TODO: Implement destroy().
    319319        }
    320320    }
Note: See TracChangeset for help on using the changeset viewer.