Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 11241


Ignore:
Timestamp:
Oct 17, 2016, 8:35:19 PM (8 years ago)
Author:
patricwi
Message:

KeyBindings-logic added, eventhough still all leading to ESC

Location:
code/branches/HUD_HS16/src/modules/pickup
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/HUD_HS16/src/modules/pickup/PickupManager.cc

    r11240 r11241  
    7676        this->defaultRepresentation_ = new PickupRepresentation();
    7777
    78         orxout() << "PickupManager created." << endl;
     78        orxout(internal_info, context::pickups) << "PickupManager created." << endl;
    7979    }
    8080
     
    285285        if(pickedUp) // If the Pickupable has changed to picked up, it is added to the required lists.
    286286        {
    287             index = this->getPickupIndex(); // Ge a new identifier (index) for the Pickupable.
     287            index = this->getPickupIndex(); // Get a new identifier (index) for the Pickupable.
    288288            // Add the Pickupable to the indexes_ and pickups_ lists.
    289289            this->indexes_[pickup] = index;
    290290            this->pickups_[index] = pickup;
    291             // TODO: Add pickup keybinding -------------------------------------------
     291
     292             //Add pickup keybinding
    292293            if( KeyBinderManager::exists() )
    293                 KeyBinderManager::getInstance().getCurrent()->setBinding("KeyESC", "Keys.KeyNumpad0",true);
     294                addKeyBindingForNewPickup(pickup, index);
    294295            else
    295296                orxout() << "Could not create new keybinding because KeyBinderManager doesn't exist." << endl;
    296             //----------------------------
    297 
    298297        }
    299298        else // If it was dropped, it is removed from the required lists.
     
    303302            index = it->second;
    304303
    305             // Remove the Pickupable from the indexes_ and pickups_ list.
    306             this->indexes_.erase(it);
    307             this->pickups_.erase(index);
    308             // TODO: Remove pickup keybinding ------------------------------------
     304            //Remove pickup keybinding
    309305            if( KeyBinderManager::exists() )
    310                   KeyBinderManager::getInstance().getCurrent()->setBinding("", "Keys.KeyNumpad0",true);
     306                  removeKeyBindingForOldPickup(pickup, index);
    311307            else
    312308                orxout() << "Could not delete old keybinding because KeyBinderManager doesn't exist." << endl;
    313             //----------------------------
     309
     310            // Remove the Pickupable from the indexes_ and pickups_ list.
     311            this->indexes_.erase(pickup);
     312            this->pickups_.find(index)->second=nullptr; //set to null, so that can be indentified as free slot by getPickupIndex()
    314313        }
    315314
     
    336335            }
    337336        }
     337
     338    }
     339
     340    //PRECONDITION: KeyBinderManager exists, pickup is not NULL, 0 < index < 9
     341    //FUNCTION: Adds a keybinding for the new pickup using its index
     342    void PickupManager::addKeyBindingForNewPickup(Pickupable* pickup, uint32_t index)
     343    {
     344        std::string name="Keys.KeyNumpad";
     345        std::string binding="";
     346
     347        name.append(std::to_string(index));
     348
     349        KeyBinderManager::getInstance().getCurrent()->setBinding("KeyESC", name, true); // TODO do not set all bindings to ESC
     350
     351        orxout() << "Keybinding for item " << index << " has been added on keybinding " << name << endl;
     352
     353
     354    }
     355
     356    //PRECONDITION: KeyBinderManager exists, pickup is not NULL, 0 < index < 9
     357    //FUNCTION: Removes the keybinding of the pickup using its index
     358    void PickupManager::removeKeyBindingForOldPickup(Pickupable* pickup, uint32_t index)
     359    {
     360        std::string name="Keys.KeyNumpad";
     361        std::string binding="";
     362
     363        name.append(std::to_string(index));
     364
     365        KeyBinderManager::getInstance().getCurrent()->setBinding("", name, true);
     366
     367        orxout() << "Keybinding for item " << index << " has been removed on keybinding " << name << endl;
    338368
    339369    }
     
    417447            Pickupable* pickupable = this->pickups_.find(pickup)->second;
    418448            if(pickupable != nullptr)
     449            {
    419450                pickupable->drop();
     451
     452            }
    420453        }
    421454        // If we're neither server nor standalone we drop the pickup by calling dropPickupNetworked() of the PickupManager on the server.
     
    510543    /**
    511544    @brief
    512         Get a new index for a Pickupable.
    513         This will work as long as the number of Pickupables that are picked up is sufficiently small and as long as they don't exist forever.
     545        Get a new index between 0 and 9 for a Pickupable.
     546        If all slots are occupied, the Pickupable in the first slot will be dropped.
    514547    @return
    515548        Returns the new index.
     
    517550    uint32_t PickupManager::getPickupIndex(void)
    518551    {
    519         if(this->pickupHighestIndex_ == uint32_t(~0x0)-1) // If we've reached the highest possible number, we wrap around.
    520             this->pickupHighestIndex_ = 0;
    521         return this->pickupHighestIndex_++;
     552        //check if there are free slots available
     553
     554        for(uint32_t i=0; i<10; i++)
     555        {
     556            if(pickups_.find(i)->second==nullptr) return i;
     557        }
     558        //all slots are full and we have to drop sth
     559        orxout() << "everything was full and we have now dropped the first element" << endl;
     560        this->dropPickup(0);
     561        return 0;
    522562    }
    523563
  • code/branches/HUD_HS16/src/modules/pickup/PickupManager.h

    r11071 r11241  
    167167            uint32_t getPickupIndex(void); //!< Get a new index for a Pickupable.
    168168
     169            void addKeyBindingForNewPickup(Pickupable* pickup, uint32_t index);
     170            void removeKeyBindingForOldPickup(Pickupable* pickup, uint32_t index);
     171           
    169172    }; // tolua_export
    170173
Note: See TracChangeset for help on using the changeset viewer.