2022-11-14 14:57:02 +00:00
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
#include "InventoryComponent.h"
|
2022-11-14 17:42:26 +00:00
|
|
|
#include "Items/BaseItem.h"
|
2022-11-14 14:57:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Sets default values for this component's properties
|
|
|
|
UInventoryComponent::UInventoryComponent()
|
|
|
|
{
|
|
|
|
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
|
|
|
|
// off to improve performance if you don't need them.
|
|
|
|
PrimaryComponentTick.bCanEverTick = true;
|
2023-05-12 22:50:30 +00:00
|
|
|
|
|
|
|
static ConstructorHelpers::FClassFinder<UUserWidget> InventoryNotificationWidgetClassFinder(TEXT("/Game/Blueprints/Status_UI/InventoryNotification"));
|
|
|
|
InventoryNotificationWidgetClass = InventoryNotificationWidgetClassFinder.Class;
|
2022-11-14 14:57:02 +00:00
|
|
|
|
2022-11-14 17:42:26 +00:00
|
|
|
MaxItemSlots = 10;
|
2022-11-14 14:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Called when the game starts
|
|
|
|
void UInventoryComponent::BeginPlay()
|
|
|
|
{
|
|
|
|
Super::BeginPlay();
|
|
|
|
|
2023-05-12 22:50:30 +00:00
|
|
|
InventoryNotificationWidgetInstance = CreateWidget<UUserWidget>(GetWorld(), InventoryNotificationWidgetClass);
|
|
|
|
InventoryNotificationWidgetInstance->AddToViewport();
|
|
|
|
|
2022-11-29 12:43:58 +00:00
|
|
|
//activates the AddItem function for every DefaultItem that inherits BaseItem
|
2023-05-09 04:40:31 +00:00
|
|
|
for (auto& BaseItem : DefaultItems)
|
2022-11-14 17:42:26 +00:00
|
|
|
{
|
|
|
|
AddItem(BaseItem);
|
2023-05-09 04:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 17:42:26 +00:00
|
|
|
bool UInventoryComponent::AddItem(class UBaseItem* BaseItem)
|
2022-11-14 14:57:02 +00:00
|
|
|
{
|
2022-11-14 17:42:26 +00:00
|
|
|
//if the items is over the maxinventoryslots then it wont add the item
|
|
|
|
if (Items.Num() >= MaxItemSlots || !BaseItem)
|
|
|
|
{
|
2022-11-18 13:07:44 +00:00
|
|
|
UE_LOG(LogTemp, Display, TEXT("THERE ARE MORE ITEMS THAN THE INVENTORY SLOTS"));
|
2022-11-14 17:42:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BaseItem->StoredItems = this;
|
|
|
|
BaseItem->World = GetWorld();
|
2023-01-12 15:21:32 +00:00
|
|
|
bool isNewItem = true;
|
2023-05-09 04:40:31 +00:00
|
|
|
for (auto& Item : Items)
|
2023-01-12 15:21:32 +00:00
|
|
|
{
|
|
|
|
//if the item is the same as the item that is being added
|
|
|
|
if (Item->ItemDisplayName.ToString() == BaseItem->ItemDisplayName.ToString())
|
|
|
|
{
|
2023-05-12 22:50:30 +00:00
|
|
|
Item->StackCount += BaseItem->StackCount;
|
|
|
|
OnItemAdd.Broadcast(BaseItem->ItemDisplayName.ToString(), BaseItem->StackCount);
|
2023-01-12 15:21:32 +00:00
|
|
|
UE_LOG(LogTemp, Display, TEXT("ITEM STACKCOUNT: %d"), Item->StackCount);
|
|
|
|
isNewItem = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isNewItem == true)
|
|
|
|
{
|
|
|
|
Items.Add(BaseItem);
|
2023-05-12 22:50:30 +00:00
|
|
|
OnItemAdd.Broadcast(BaseItem->ItemDisplayName.ToString(), BaseItem->StackCount);
|
2023-01-12 15:21:32 +00:00
|
|
|
UE_LOG(LogTemp, Display, TEXT("ITEM HAS BEEN ADDED TO INVENTORY"));
|
|
|
|
}
|
2022-11-29 14:15:20 +00:00
|
|
|
//Refreshes the inventory
|
2022-11-14 17:42:26 +00:00
|
|
|
OnInventoryUpdated.Broadcast();
|
2023-05-09 04:40:31 +00:00
|
|
|
return true;
|
2022-11-14 17:42:26 +00:00
|
|
|
}
|
2022-11-14 14:57:02 +00:00
|
|
|
|
2022-11-24 00:13:20 +00:00
|
|
|
// remove only gets called once on the same item
|
2022-11-18 13:07:44 +00:00
|
|
|
bool UInventoryComponent::Remove(class UBaseItem* BaseItem)
|
2022-11-14 17:42:26 +00:00
|
|
|
{
|
2023-05-09 04:40:31 +00:00
|
|
|
if (BaseItem)
|
2022-11-14 17:42:26 +00:00
|
|
|
{
|
2022-11-18 13:07:44 +00:00
|
|
|
UE_LOG(LogTemp, Display, TEXT("ItEM HAS BEEN REMOVED"));
|
2022-11-14 17:42:26 +00:00
|
|
|
BaseItem->StoredItems = nullptr;
|
|
|
|
BaseItem->World = nullptr;
|
2023-01-12 15:21:32 +00:00
|
|
|
Items.RemoveSingle(BaseItem);
|
2022-11-14 17:42:26 +00:00
|
|
|
OnInventoryUpdated.Broadcast(); // Updates UI
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2022-11-14 14:57:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 14:53:22 +00:00
|
|
|
UBaseItem* UInventoryComponent::GetItem(int Index)
|
2022-11-18 13:07:44 +00:00
|
|
|
{
|
|
|
|
return Items[Index];
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:56:30 +00:00
|
|
|
void UInventoryComponent::RemoveItem(UEatableItems* Item)
|
|
|
|
{
|
2023-01-12 15:21:32 +00:00
|
|
|
Item->StackCount--;
|
|
|
|
if (Item->StackCount <= 0)
|
|
|
|
{
|
|
|
|
Remove(Item); // activates the remove function up above
|
|
|
|
}
|
2023-01-12 13:56:30 +00:00
|
|
|
OnInventoryUpdated.Broadcast();
|
|
|
|
}
|
|
|
|
|
2023-05-09 04:40:31 +00:00
|
|
|
int UInventoryComponent::GetItemAmount(const int& ItemID)
|
|
|
|
{
|
|
|
|
if (Items.Num() > 0)
|
|
|
|
{
|
|
|
|
for (UBaseItem* const& Item : Items)
|
|
|
|
{
|
|
|
|
if (Item->ItemID == ItemID)
|
|
|
|
{
|
|
|
|
return Item->StackCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UInventoryComponent::RemoveItemID(const int& ItemID)
|
|
|
|
{
|
|
|
|
if (Items.Num() > 0)
|
|
|
|
{
|
|
|
|
for (UBaseItem* const& Item : Items)
|
|
|
|
{
|
|
|
|
if (Item->ItemID == ItemID)
|
|
|
|
{
|
|
|
|
RemoveItem(Cast<UEatableItems>(Item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UBaseItem* UInventoryComponent::GetItemByID(const int& ItemID)
|
|
|
|
{
|
|
|
|
if (Items.Num() > 0)
|
|
|
|
{
|
|
|
|
for (UBaseItem* const& Item : Items)
|
|
|
|
{
|
|
|
|
if (Item->ItemID == ItemID)
|
|
|
|
{
|
|
|
|
return Item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|