Advertisement
devenshona

InventoryComponent.cpp

Jan 20th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "Components/InventoryComponent.h"
  5.  
  6. #include "Actors/BaseItemPickup.h"
  7.  
  8.  
  9. // Sets default values for this component's properties
  10. UInventoryComponent::UInventoryComponent()
  11. {
  12.     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
  13.     // off to improve performance if you don't need them.
  14.     PrimaryComponentTick.bCanEverTick = true;
  15.  
  16.     // ...
  17. }
  18.  
  19.  
  20. // Called when the game starts
  21. void UInventoryComponent::BeginPlay()
  22. {
  23.     Super::BeginPlay();
  24.  
  25.     // ...
  26.    
  27. }
  28.  
  29.  
  30. // Called every frame
  31. void UInventoryComponent::TickComponent(float DeltaTime, ELevelTick TickType,
  32.                                         FActorComponentTickFunction* ThisTickFunction)
  33. {
  34.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  35.  
  36.     // ...
  37. }
  38.  
  39. FReturnTypes UInventoryComponent::FindItemInInventory(FName Data)
  40. {
  41.     FReturnTypes NewReturn;
  42.     NewReturn.Index = INDEX_NONE;
  43.     NewReturn.Success = false;
  44.     NewReturn.InventoryItem = nullptr;
  45.     if (Data == "NONE")
  46.     {
  47.         return NewReturn; // Invalid Name
  48.     }
  49.     FStaticItemInfo* Row = DataTable->FindRow<FStaticItemInfo>(Data,FString());
  50.     if (!Row)
  51.     {
  52.         return NewReturn; // No Row Found
  53.     }
  54.     for (int32 i = 0; i < Contents.Num(); i++)
  55.     {
  56.         if (Contents[i].InventoryItem != nullptr && Contents[i].Quantity > 0)
  57.         {
  58.             if (Contents[i].InventoryItem == Row->ItemPickup->InventoryItem)
  59.             {
  60.                 NewReturn.Index = i ;
  61.                 NewReturn.InventoryItem = Row->ItemPickup->InventoryItem;
  62.                 NewReturn.Success = true;
  63.             }
  64.         }
  65.     }
  66.     return NewReturn;
  67. }
  68.  
  69. bool UInventoryComponent::DropItem(FName Data)
  70. {
  71.     if (Data == "NONE")
  72.     {
  73.         UE_LOG(LogTemp, Error,TEXT("%ls Is A Invalid Name \n"),*Data.ToString())
  74.         return false; // Invalid Name
  75.     }
  76.     FStaticItemInfo* Row = DataTable->FindRow<FStaticItemInfo>(Data,FString());
  77.     if (!Row)
  78.     {
  79.         //UE_LOG(LogTemp,Error,TEXT("%p Is A Invalid Row \n"),Row)
  80.         return false; //Row Not Found
  81.     }
  82.     if (Row)
  83.     {
  84.         if (int32 Index; (Index = FindItemInInventory(Data).Index) != INDEX_NONE)
  85.         {
  86.             if (Contents[Index].Quantity > 0 && Contents[Index].InventoryItem != nullptr)
  87.             {
  88.                 //Subtract Quantity
  89.                 Contents[Index].Quantity -= 1;
  90.  
  91.                 // Definitions
  92.                 UWorld* World = GetWorld();
  93.  
  94.                 //Get Players Current Location (Start Of LineTrace) And Look Direction
  95.                 FVector PlayerLocation;
  96.                 FRotator PlayerRotation;
  97.  
  98.                 World->GetFirstPlayerController()->GetPlayerViewPoint(PlayerLocation, PlayerRotation);
  99.  
  100.                 FVector LineTraceEnd = PlayerLocation + PlayerRotation.Vector() * 200.0f;
  101.                 FHitResult Hit;
  102.  
  103.                 //Perform LineTrace
  104.  
  105.                 if (const bool bHit = World->
  106.                     LineTraceSingleByChannel(Hit, PlayerLocation, LineTraceEnd, ECC_Visibility))
  107.                 {
  108.                     LineTraceEnd = Hit.Location;
  109.                 }
  110.  
  111.                 //Prepare Spawning Of Actor
  112.                 ABaseItemPickup* ItemPickup = Row->ItemPickup;
  113.                 UClass* ActorToSpawn = ItemPickup->GetClass();
  114.                 FActorSpawnParameters SpawnParameters;
  115.  
  116.                 SpawnParameters.SpawnCollisionHandlingOverride =
  117.                     ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
  118.  
  119.                 //Spawn Actor
  120.                 ABaseItemPickup* SpawnedActor = World->SpawnActor<ABaseItemPickup>(
  121.                     ActorToSpawn, LineTraceEnd, FRotator(0, 0, 0), SpawnParameters);
  122.  
  123.                 if (UStaticMeshComponent* MeshComponent = Cast<UStaticMeshComponent>(SpawnedActor->GetRootComponent()))
  124.                 {
  125.                     MeshComponent->SetSimulatePhysics(true);
  126.                 }
  127.                 return true;
  128.             }
  129.             return false;
  130.         }
  131.     }
  132.     UE_LOG(LogTemp, Error, TEXT("File: %s,\n Line: %d, Error: Invalid name %ls"),
  133.            TEXT(__FILE__), __LINE__, *Data.ToString());
  134.     return false;
  135. }
  136.  
  137. bool UInventoryComponent::AddItem(FName Data, int Quantity)
  138. {
  139.     FStaticItemInfo* Row = DataTable->FindRow<FStaticItemInfo>(Data,FString());
  140.     if (Data == "NONE" || Quantity <= 0)
  141.     {
  142.         return false; // Invalid Use
  143.     }
  144.     if (!Row)
  145.     {
  146.         return false; //Invalid Row
  147.     }
  148.     if (Row)
  149.     {
  150.         const auto [Success, Index, InventoryItem] = FindItemInInventory(Data);
  151.  
  152.         if (InventoryItem == nullptr)
  153.         {
  154.             return false; // Invalid Item
  155.         }
  156.         //Add Existing Item To Inventory
  157.         if (Success == true && Index != INDEX_NONE)
  158.         {
  159.             Contents[Index].Quantity += Quantity;
  160.             return true;
  161.         }
  162.         else if (Index == INDEX_NONE)
  163.         {
  164.             FInventoryInfo NewItem;
  165.             NewItem.Quantity = Quantity;
  166.             NewItem.InventoryItem = InventoryItem;
  167.  
  168.             Contents.Add(NewItem);
  169.             return true;
  170.         }
  171.     }
  172.     return false;
  173. }
  174.  
  175. /**
  176.  * UseItem method is used to use an item from the inventory.
  177.  *
  178.  * @param Data The name of the item to be used.
  179.  *
  180.  * @return Returns true if the item was successfully used, false otherwise.
  181.  */
  182. bool UInventoryComponent::UseItem(FName Data)
  183. {
  184.     if (Data == "NONE")
  185.     {
  186.         return false; // Invalid Name
  187.     }
  188.     FStaticItemInfo* Row = DataTable->FindRow<FStaticItemInfo>(Data,FString());
  189.     if (!Row)
  190.     {
  191.         return false; // Invalid Row
  192.     }
  193.     const auto [Success, Index, InventoryItem] = FindItemInInventory(Data);
  194.     if (Success)
  195.     {
  196.         AInventoryItem* UsedItem = InventoryItem;
  197.         Contents[Index].Quantity -= 1;
  198.         UsedItem->UseItem(Row,this,Index);
  199.         return true;
  200.     }
  201.     return false;
  202. }
  203.  
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement