Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 23, 2009, 8:27:17 PM (15 years ago)
Author:
dafrick
Message:

Commit changes in pickup before merge.

Location:
code/branches/pickup2/src/modules/pickup
Files:
6 added
4 moved

Legend:

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

    r5953 r6405  
    2323 *      Daniel 'Huty' Haggenmueller
    2424 *   Co-authors:
    25  *      ...
     25 *      Damian 'Mozork' Frick
    2626 *
    2727 */
     
    3434#include "PickupSpawner.h"
    3535
    36 #include "BaseItem.h"
    37 
    3836#include "core/CoreIncludes.h"
    39 #include "core/GUIManager.h"     // HACK; see below
     37//#include "core/GUIManager.h"     // HACK; see below
    4038#include "core/Template.h"
    4139#include "core/XMLPort.h"
    4240#include "worldentities/pawns/Pawn.h"
    43 #include "PickupInventory.h"    // HACK; Only for hack, remove later
     41//#include "PickupInventory.h"    // HACK; Only for hack, remove later
    4442
    4543
     
    4745{
    4846
    49     const float PickupSpawner::bounceSpeed_s = 6.0f;
    50     const float PickupSpawner::rotationSpeed_s = 1.0f;
    51     const float PickupSpawner::bounceDistance_s = 4.0f;
     47//     const float PickupSpawner::bounceSpeed_s = 6.0f;
     48//     const float PickupSpawner::rotationSpeed_s = 1.0f;
     49//     const float PickupSpawner::bounceDistance_s = 4.0f;
    5250
    5351    CreateFactory(PickupSpawner);
     
    6462    }
    6563
    66     PickupSpawner::PickupSpawner(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems) : StaticEntity(creator)
     64    PickupSpawner::PickupSpawner(BaseObject* creator, Pickupable* pickup, float triggerDistance, float respawnTime, int maxSpawnedItems) : StaticEntity(creator)
    6765    {
    6866        this->initialize();
    6967 
    70         //TODO: Does this actually work?
    71         this->itemTemplateName_ = item->getIdentifier()->getName();
    72         this->itemTemplate_ = Template::getTemplate(this->itemTemplateName_);
     68        this->pickup_ = pickup;
    7369
    7470        this->triggerDistance_ = triggerDistance;
     
    8177        RegisterObject(PickupSpawner);
    8278
    83         this->itemTemplate_ = NULL;
     79        this->pickup_ = NULL;
     80       
    8481        this->triggerDistance_ = 20;
    85         this->respawnTime_ = 0.0f;
    86         this->tickSum_ = 0.0f;
     82        this->respawnTime_ = 0;
     83        this->tickSum_ = 0;
    8784        this->maxSpawnedItems_ = INF;
    8885        this->spawnsRemaining_ = INF;
     
    110107        SUPER(PickupSpawner, XMLPort, xmlelement, mode);
    111108
    112         XMLPortParam(PickupSpawner, "item", setItemTemplateName, getItemTemplateName, xmlelement, mode);
     109        XMLPortObject(PickupSpawner, Pickupable, "pickup", addPickupable, getPickupable, xmlelement, mode);
     110       
    113111        XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode);
    114112        XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode);
     
    119117        // Load the GUI image as soon as the PickupSpawner gets loaded
    120118        //  = less delays while running
    121         BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this);
    122         BaseItem* asItem = orxonox_cast<BaseItem*>(newObject);
    123         if (asItem)
    124         {
    125             asItem->addTemplate(this->itemTemplate_);
    126             PickupInventory::getImageForItem(asItem);
    127             newObject->destroy();
    128         }
     119//         BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this);
     120//         BaseItem* asItem = orxonox_cast<BaseItem*>(newObject);
     121//         if (asItem)
     122//         {
     123//             asItem->addTemplate(this->itemTemplate_);
     124//             PickupInventory::getImageForItem(asItem);
     125//             newObject->destroy();
     126//         }
    129127
    130128        //  & load the GUI itself too, along with some empty windows
    131129        //   = even less delays
    132         GUIManager::getInstance().showGUI("PickupInventory");
    133         GUIManager::getInstance().executeCode("hideGUI(\"PickupInventory\")");
    134         PickupInventory::getSingleton();
     130//         GUIManager::getInstance().showGUI("PickupInventory");
     131//         GUIManager::getInstance().executeCode("hideGUI(\"PickupInventory\")");
     132//         PickupInventory::getSingleton();
     133    }
     134   
     135    void PickupSpawner::addPickupable(Pickupable* pickup)
     136    {
     137        if(this->pickup_ != NULL)
     138        {
     139            COUT(1) << "addPickupable called, with this->pickup_ already set." << std::endl;
     140            return;
     141        }
     142        if(pickup == NULL)
     143        {
     144            COUT(1) << "Argument of addPickupable is NULL." << std::endl;
     145            return;
     146        }
     147       
     148        this->pickup_ = pickup;
     149    }
     150   
     151    Pickupable* PickupSpawner::getPickupable(void)
     152    {
     153        return this->pickup_;
    135154    }
    136155
     
    139158        Invoked when the activity has changed. Sets visibility of attached objects.
    140159    */
    141     void PickupSpawner::changedActivity()
    142     {
    143         SUPER(PickupSpawner, changedActivity);
    144 
    145         for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++)
    146             (*it)->setVisible(this->isActive());
    147     }
    148 
    149     /**
    150     @brief
    151         Set the template name of the item to spawn, also loads the template.
    152     @param name
    153         Name of the new template.
    154     */
    155     void PickupSpawner::setItemTemplateName(const std::string& name)
    156     {
    157         this->itemTemplateName_ = name;
    158         this->itemTemplate_ = Template::getTemplate(name);
    159     }
     160//     void PickupSpawner::changedActivity()
     161//     {
     162//         SUPER(PickupSpawner, changedActivity);
     163//
     164//         for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++)
     165//             (*it)->setVisible(this->isActive());
     166//     }
    160167
    161168    void PickupSpawner::setMaxSpawnedItems(int items)
     
    171178        Time since last tick.
    172179    */
    173     //TODO: Replace this with a real DistanceTrigger.
     180    //TODO: Replace this with a real DistanceTrigger?
    174181    void PickupSpawner::tick(float dt)
    175182    {
     183        //! If the PickupSpawner is active.
    176184        if (this->isActive())
    177185        {
    178             //! Triggers as soon as a Pawn is in the specified distance.
     186            //! Iterate trough all Pawns.
    179187            for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
    180188            {
    181189                Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
    182                 if (distance.length() < this->triggerDistance_)
     190                //! If a Pawn, that fits the target-range of the item spawned by this Pickup, is in trigger-distance.
     191                if (distance.length() < this->triggerDistance_ && this->pickup_->isTarget(*it))
     192                {
    183193                    this->trigger(*it);
     194                }
    184195            }
    185196
    186197            //! Animation.
    187             this->yaw(Radian(rotationSpeed_s*dt));
    188             this->tickSum_ += bounceSpeed_s*dt;
    189             this->translate(Vector3(0,bounceDistance_s*dt*sin(this->tickSum_),0));
    190             if (this->tickSum_ > 2*Ogre::Math::PI)
    191                 this->tickSum_ -= 2*Ogre::Math::PI;
     198//             this->yaw(Radian(rotationSpeed_s*dt));
     199//             this->tickSum_ += bounceSpeed_s*dt;
     200//             this->translate(Vector3(0,bounceDistance_s*dt*sin(this->tickSum_),0));
     201//             if (this->tickSum_ > 2*Ogre::Math::PI)
     202//                 this->tickSum_ -= 2*Ogre::Math::PI;
    192203        }
    193204    }
     
    205216    void PickupSpawner::trigger(Pawn* pawn)
    206217    {
    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.
     218        //TODO: If private, isActive doesn't need to be tested anymore.
     219        if (this->isActive()) //!< Checks whether PickupItem is active.
     220        {
     221            Pickupable* pickup = this->getPickup();
     222            if (pickup != NULL) //!< If everything went ok, and pickup is not NULL.
    211223            {
    212                 item->setPickupIdentifier(this->itemTemplateName_); //TODO: Needed?
    213                 item->addTemplate(this->itemTemplate_); //TODO: Does what?
    214 
    215                 if(item->pickedUp(pawn))
    216                 {
    217                     COUT(3) << this->itemTemplateName_ << " got picked up." << std::endl;
    218 
    219 
    220                     if(this->spawnsRemaining_ != INF)
    221                     {
    222                         this->spawnsRemaining_--;
    223                     }
    224 
    225                     if (this->spawnsRemaining_ != 0 && this->respawnTime_ > 0.0f)
    226                     {
    227                         this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this)));
    228 
    229                         this->setActive(false);
    230                         this->fireEvent();
    231                     }
     224                PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(pawn);
     225                if(carrier == NULL)
     226                {
     227                    COUT(1) << "This is bad. Pawn isn't PickupCarrier." << std::endl;
     228                    return;
     229                }
     230               
     231                if(pickup->pickup(carrier))
     232                {
     233                    COUT(3) << "Pickup got picked up." << std::endl;
     234
     235                    this->decrementSpawnsRemaining();
    232236                }
    233237                else
    234238                {
    235                     item->destroy();
     239                    pickup->destroy();
    236240                }
    237241            }
    238242        }
    239 
    240         if(this->spawnsRemaining_ == 0)
     243    }
     244   
     245    void PickupSpawner::decrementSpawnsRemaining(void)
     246    {
     247        if(this->spawnsRemaining_ != INF)
     248        {
     249            this->spawnsRemaining_--;
     250        }
     251        if(this->spawnsRemaining_ != 0 && this->respawnTime_ > 0)
     252        {
     253            //TODO: Nicer?
     254            this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this)));
     255
     256            this->setActive(false);
     257            this->fireEvent();
     258        }
     259        else
    241260        {
    242261            COUT(3) << "PickupSpawner empty, selfdistruct initialized." << std::endl;
    243262            this->setActive(false);
    244             this->destroy();
    245         }
    246     }
    247 
    248     /**
    249     @brief
    250         Creates a BaseItem of the type specified by the PickupSpawner.
     263            this->destroy(); //TODO: Implement destroy().
     264        }
     265    }
     266
     267    /**
     268    @brief
     269        Creates a new Pickupable.
    251270    @return
    252         The BaseItem created.
     271        The Pickupable created.
    253272    */   
    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);
     273    Pickupable* PickupSpawner::getPickup(void)
     274    {
     275        if(this->spawnsRemaining_ == 0)
     276        {
     277            COUT(1) << "Massive Error: PickupSpawner still alive until having spawned last item." << std::endl;
     278            return NULL;
     279        }
     280       
     281        Pickupable* pickup = this->pickup_->clone();
     282        return pickup;
    258283    }
    259284
  • code/branches/pickup2/src/modules/pickup/PickupSpawner.h

    r5953 r6405  
    3535#define _PickupSpawner_H__
    3636
    37 #include "OrxonoxPrereqs.h"
     37#include "pickup/PickupPrereqs.h"
    3838
    3939#include <string>
     
    4141#include "tools/interfaces/Tickable.h"
    4242#include "worldentities/StaticEntity.h"
     43#include "interfaces/Pickupable.h"
    4344
    4445namespace orxonox
     
    5152    {
    5253        public:
    53             //TODO: Add limit of items spawned here. Also possibility to spawn collections?
     54            //TODO: Add limit of items spawned here.
    5455            PickupSpawner(BaseObject* creator);
    55             PickupSpawner(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems);
     56            PickupSpawner(BaseObject* creator, Pickupable* pickup, float triggerDistance, float respawnTime, int maxSpawnedItems);
    5657            virtual ~PickupSpawner();
    5758
     
    5960            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);  //!< Method for creating a PickupSpawner through XML.
    6061            virtual void tick(float dt);
    61 
    62             void trigger(Pawn* pawn);                                       //!< Method called when a Pawn is close enough.
    63             void respawnTimerCallback();                                    //!< Method called when the timer runs out.
    64 
    65             /**
    66             @brief Get the template name for the item to spawn.
    67             @return Returns the name of the template of the item to spawn.
    68             */
    69             inline const std::string& getItemTemplateName() const
    70                 { return this->itemTemplateName_; }
    71             void setItemTemplateName(const std::string& name);              //!< Set the template name of the item to spawn.
    72 
    73             /**
    74                 @brief Get the template for the item to spawn.
    75                 @return Returns the template of the item to spawn.
    76             */
    77             inline Template* getItemTemplate() const
    78                 { return this->itemTemplate_; }
    7962
    8063            /**
     
    11093
    11194        protected:
    112             virtual BaseItem* getItem(void);
     95            virtual Pickupable* getPickup(void);
     96           
     97            void addPickupable(Pickupable* pickup);
     98            Pickupable* getPickupable(void);
     99           
     100            void decrementSpawnsRemaining(void);
    113101
    114102        private:
    115103            void initialize(void);
     104           
     105            void trigger(Pawn* pawn);                                       //!< Method called when a Pawn is close enough.
     106            void respawnTimerCallback();                                    //!< Method called when the timer runs out.
    116107
    117             std::string itemTemplateName_;          //!< Template name of the item to spawn.
    118             Template* itemTemplate_;                //!< Template of the item to spawn.
     108           
     109            Pickupable* pickup_;
    119110
    120111            int maxSpawnedItems_;                   //!< Maximum number of items spawned by this PickupSpawner.
Note: See TracChangeset for help on using the changeset viewer.