Illusion-UE5/hwanyoung2/Source/hwanyoung2/HYPlayerCharacController.cpp
2024-09-10 00:07:47 -04:00

73 lines
2.0 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "HYPlayerCharacController.h"
#include "HYGameStateBase.h"
#include "hwanyoung2Character.h"
AHYPlayerCharacController::AHYPlayerCharacController()
{
//this is hardcoding the limits:
InventorySlotLimit = 50;
InventoryWeightLimit = 500;
}
int32 AHYPlayerCharacController::GetInventoryWeight()
{
int32 TotalWeight = 0;
for (auto& Item : Inventory) {
TotalWeight += 1; //hard coding for now
}
return TotalWeight;
}
bool AHYPlayerCharacController::AddItemToInventoryByID(FName ID)
{
AHYGameStateBase* gameState = Cast<AHYGameStateBase>(GetWorld()->GetGameState());
UDataTable* itemDB = gameState->GetItemDatabase();
FInventoryItem* itemToAdd = itemDB->FindRow<FInventoryItem>(ID, "");
if (itemToAdd) {
if (Inventory.Num() < InventorySlotLimit &&
GetInventoryWeight() + itemToAdd->ItemWeight <= InventoryWeightLimit) {
Inventory.Add(*itemToAdd);
ReloadInventory();
return true;
}
}
return false;
}
void AHYPlayerCharacController::CraftItem(FInventoryItem ItemA, FInventoryItem ItemB, FInventoryItem ItemC, FInventoryItem ItemD)
{
for (auto Combination : ItemB.CraftCombinations) {
if (Combination.ItemAID == ItemA.ItemID
&& Combination.ItemBID == ItemB.ItemID
&& Combination.ItemCID == ItemC.ItemID
&& Combination.ItemDID == ItemD.ItemID) {
if (Combination.bDestroyItemA) Inventory.RemoveSingle(ItemA);
if (Combination.bDestroyItemB) Inventory.RemoveSingle(ItemB);
if (Combination.bDestroyItemC) Inventory.RemoveSingle(ItemC);
if (Combination.bDestroyItemD) Inventory.RemoveSingle(ItemD);
AddItemToInventoryByID(Combination.ProductID);
ReloadCraftUI();
}
}
}
void AHYPlayerCharacController::Interact()
{
if (CurrentInteractable) CurrentInteractable->Interact(this);
}
void AHYPlayerCharacController::SetupInputComponent()
{
Super::SetupInputComponent();
}
void AHYPlayerCharacController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
}