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-15 03:27:44 +00:00
|
|
|
#include "the_twilight_abyss/PlayerTemp/TempCharacter.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;
|
|
|
|
|
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();
|
|
|
|
|
2022-11-29 12:43:58 +00:00
|
|
|
//activates the AddItem function for every DefaultItem that inherits BaseItem
|
2022-11-14 17:42:26 +00:00
|
|
|
for(auto & BaseItem : DefaultItems)
|
|
|
|
{
|
|
|
|
AddItem(BaseItem);
|
2022-11-18 13:07:44 +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;
|
|
|
|
// for every item in inventory
|
|
|
|
for (auto & Item : Items)
|
|
|
|
{
|
|
|
|
//if the item is the same as the item that is being added
|
|
|
|
if (Item->ItemDisplayName.ToString() == BaseItem->ItemDisplayName.ToString())
|
|
|
|
{
|
|
|
|
//add the amount of the item that is being added to the item in the inventory
|
|
|
|
Item->StackCount++;
|
|
|
|
UE_LOG(LogTemp, Display, TEXT("ITEM STACKCOUNT: %d"), Item->StackCount);
|
|
|
|
isNewItem = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isNewItem == true)
|
|
|
|
{
|
|
|
|
Items.Add(BaseItem);
|
|
|
|
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-01-12 15:21:32 +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
|
|
|
{
|
|
|
|
if(BaseItem)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
|