Esempio n. 1
0
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;
}
Esempio n. 2
0
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;
	}
}
Esempio n. 3
0
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);	
}
Esempio n. 4
0
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);
	
}
Esempio n. 5
0
bool InventoryComponent::AddItem(Item newItem, int quantity)
{
	std::shared_ptr<BaseItemComponent> strongItemComp;
	if (CheckConvertAndCastPtr<ItemComponent, BaseItemComponent>(newItem.GetItemComponent(ItemComponent::GetIDFromName(BaseItemComponent::COMPONENT_NAME)), strongItemComp))
	{
		ItemID itemID = reinterpret_cast<ItemID>(HashedString::hash_name(strongItemComp->GetItemName().c_str()));
		std::map<ItemID, Item>::iterator it = m_heldItems.find(itemID);
		if (it == m_heldItems.end())
		{
			strongItemComp->SetQuantity(quantity);
			m_heldItems.insert(std::pair<ItemID, Item>(itemID, newItem));
		}
		else
		{
			if (CheckConvertAndCastPtr<ItemComponent, BaseItemComponent>(it->second.GetItemComponent(ItemComponent::GetIDFromName(BaseItemComponent::COMPONENT_NAME)), strongItemComp))
			{
				strongItemComp->SetQuantity(strongItemComp->GetQuantity() + quantity);
			}	
		}
	}
	return true;
}
Esempio n. 6
0
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);
			}
		}
	}
}