本文整理汇总了C++中Item::GetItemComponent方法的典型用法代码示例。如果您正苦于以下问题:C++ Item::GetItemComponent方法的具体用法?C++ Item::GetItemComponent怎么用?C++ Item::GetItemComponent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Item
的用法示例。
在下文中一共展示了Item::GetItemComponent方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: AddItem
bool InventoryComponent::AddItem(ItemID newItemID, int quantity)
{
std::map<ItemID, Item>::iterator it = m_heldItems.find(newItemID);
if (it == m_heldItems.end())
{
Item newItem;
if (!ItemManager::CreateItemByID(newItemID, newItem))
return false;
ItemComponentID itemCompID = ItemComponent::GetIDFromName(BaseItemComponent::COMPONENT_NAME);
StrongItemComponentPtr itemComp = ConvertToStrongPtr<ItemComponent>(newItem.GetItemComponent(itemCompID));
if (!itemComp)
return false;
std::shared_ptr<BaseItemComponent> strongItemComp = CastComponentToDerived<StrongItemComponentPtr, BaseItemComponent>(itemComp);
strongItemComp->SetQuantity(quantity);
m_heldItems.insert(std::pair<ItemID, Item>(newItemID, newItem));
}
else
{
WeakItemComponentPtr weakPtr = (it)->second.GetItemComponent(ItemComponent::GetIDFromName(BaseItemComponent::COMPONENT_NAME));
if (weakPtr.expired())
return false;
StrongItemComponentPtr itemComp = ConvertToStrongPtr<ItemComponent>(weakPtr);
std::shared_ptr<BaseItemComponent> strongItemComp = CastComponentToDerived<StrongItemComponentPtr, BaseItemComponent>(itemComp);
strongItemComp->SetQuantity(strongItemComp->GetQuantity() + quantity);
}
return true;
}
示例2: DrawInventory
void UIManager::DrawInventory(sf::RenderWindow& rw)
{
const std::map<ItemID, Item>& items = m_playerInventory->GetItems();
Item nextItem;
sf::Text itemInfo;
itemInfo.setFont(m_font);
itemInfo.setCharacterSize(18);
//TODO Create render component, make dynamic
int i = 40;
for (std::map<ItemID, Item>::const_iterator it = items.begin(); it != items.end(); ++it)
{
nextItem = it->second;
StrongItemComponentPtr strComp = ConvertToStrongPtr(nextItem.GetItemComponent(ItemComponent::GetIDFromName(BaseItemComponent::COMPONENT_NAME)));
std::shared_ptr<BaseItemComponent> itemBaseComp= CastComponentToDerived<StrongItemComponentPtr, BaseItemComponent>(strComp);
StrongItemComponentPtr renderStrComp = ConvertToStrongPtr(nextItem.GetItemComponent(ItemComponent::GetIDFromName(ItemRenderComponent::COMPONENT_NAME)));
std::shared_ptr<ItemRenderComponent> renderComp = CastComponentToDerived<StrongItemComponentPtr, ItemRenderComponent>(renderStrComp);
renderComp->SetPosition(sf::Vector2f(20, i));
itemInfo.setString(itemBaseComp->GetItemName() + "\n" + itemBaseComp->GetDescription() + ", " + std::to_string(itemBaseComp->GetQuantity()));
itemInfo.setPosition(80, i);
rw.draw(itemInfo);
rw.draw(renderComp->GetSprite());
i += 40;
}
}
示例3: UseItem
void InventoryComponent::UseItem(ItemID itemId)
{
auto iter = m_heldItems.find(itemId);
if (iter == m_heldItems.end())
return;
Item usedItem = iter->second;
ItemComponentID itemCompID = ItemComponent::GetIDFromName(BaseItemComponent::COMPONENT_NAME);
StrongItemComponentPtr itemComp = ConvertToStrongPtr<ItemComponent>(usedItem.GetItemComponent(itemCompID));
if (!itemComp)
return;
std::shared_ptr<BaseItemComponent> strongItemComp = CastComponentToDerived<StrongItemComponentPtr, BaseItemComponent>(itemComp);
if (strongItemComp->GetQuantity() == 1)
m_heldItems.erase(iter);
else
strongItemComp->SetQuantity(strongItemComp->GetQuantity() - 1);
usedItem.Apply(m_owner);
}
示例4: DrawEquipment
void UIManager::DrawEquipment(sf::RenderWindow& rw)
{
//Get equipment
const std::map<Equipment::SlotName, Item> equipment = m_playerEquipment->GetEquipment();
//Create map of equipment render components, FIX THIS
std::map<Equipment::SlotName, std::shared_ptr<ItemRenderComponent>> equipmentRenderComponents;
Item nextItem;
std::shared_ptr<ItemRenderComponent> renderComp;
for (auto it = equipment.begin(); it != equipment.end(); ++it)
{
nextItem = it->second;
if (CheckConvertAndCastPtr(nextItem.GetItemComponent(ItemComponent::GetIDFromName(ItemRenderComponent::COMPONENT_NAME)), renderComp))
equipmentRenderComponents[it->first] = renderComp;
}
m_characterWindow->Draw(equipmentRenderComponents, rw);
}
示例5: HandleInput
void UIManager::HandleInput(sf::Event event, sf::Vector2i mousePos)
{
Item nextItem;
//If right click, open options menus
if (event.mouseButton.button == sf::Mouse::Right)
{
sf::Vector2f mousePosFloat(mousePos.x, mousePos.y);
//Check if mouse is in character window or inventory
if (m_characterWindow->GetBounds().contains(mousePosFloat))
{
const std::map<Equipment::SlotName, Item>& items = m_playerEquipment->GetEquipment();
for (auto it = items.begin(); it != items.end(); ++it)
{
nextItem = it->second;
StrongItemComponentPtr renderStrComp = ConvertToStrongPtr(nextItem.GetItemComponent(ItemComponent::GetIDFromName(ItemRenderComponent::COMPONENT_NAME)));
std::shared_ptr<ItemRenderComponent> renderComp = CastComponentToDerived<StrongItemComponentPtr, ItemRenderComponent>(renderStrComp);
sf::FloatRect spriteBounds = renderComp->GetSprite().getGlobalBounds();
if (spriteBounds.contains(mousePosFloat))
{
StrongItemComponentPtr baseItemComp = ConvertToStrongPtr(nextItem.GetItemComponent(ItemComponent::GetIDFromName(BaseItemComponent::COMPONENT_NAME)));
std::shared_ptr<BaseItemComponent> baseItemCompShared = CastComponentToDerived<StrongItemComponentPtr, BaseItemComponent>(baseItemComp);
ItemID id = reinterpret_cast<ItemID>(HashedString::hash_name(baseItemCompShared->GetItemName().c_str()));
newOpMenu->SetPosition(mousePosFloat);
newOpMenu->SetOpen(true);
newOpMenu->AttachItem(id);
}
}
}
else
{
const std::map<ItemID, Item>& items = m_playerInventory->GetItems();
Item nextItem;
for (std::map<ItemID, Item>::const_iterator it = items.begin(); it != items.end(); ++it)
{
nextItem = it->second;
StrongItemComponentPtr renderStrComp = ConvertToStrongPtr(nextItem.GetItemComponent(ItemComponent::GetIDFromName(ItemRenderComponent::COMPONENT_NAME)));
std::shared_ptr<ItemRenderComponent> renderComp = CastComponentToDerived<StrongItemComponentPtr, ItemRenderComponent>(renderStrComp);
sf::FloatRect spriteBounds = renderComp->GetSprite().getGlobalBounds();
if (spriteBounds.contains(mousePosFloat))
{
newOpMenu->SetPosition(mousePosFloat);
newOpMenu->SetOpen(true);
newOpMenu->AttachItem(it->first);
}
}
}
}
else if (event.mouseButton.button == sf::Mouse::Left)
{
sf::Vector2f mousePosFloat(mousePos.x, mousePos.y);
if (newOpMenu->IsOpen())
{
int response = newOpMenu->HandleInput(event, mousePosFloat); //0 = out of obunds, 1 = use, 2 = drop, 3 = delete;
if (response == 0)
newOpMenu->SetOpen(false);
else
{
if (m_characterWindow->GetBounds().contains(mousePosFloat))
{
if (response == 1)
m_playerEquipment->Unequip(newOpMenu->GetAttachedItem(), nextItem);
m_playerInventory->AddItem(nextItem, 1);
}
else
{
if (response == 1)
m_playerInventory->UseItem(newOpMenu->GetAttachedItem());
else if (response == 2)
m_playerInventory->DropItem(newOpMenu->GetAttachedItem());
else if (response == 3)
m_playerInventory->DeleteItem(newOpMenu->GetAttachedItem());
}
newOpMenu->SetOpen(false);
}
}
}
}