Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "Components/InventoryComponent.h"
- #include "Actors/BaseItemPickup.h"
- // 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;
- // ...
- }
- // Called when the game starts
- void UInventoryComponent::BeginPlay()
- {
- Super::BeginPlay();
- // ...
- }
- // Called every frame
- void UInventoryComponent::TickComponent(float DeltaTime, ELevelTick TickType,
- FActorComponentTickFunction* ThisTickFunction)
- {
- Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
- // ...
- }
- FReturnTypes UInventoryComponent::FindItemInInventory(FName Data)
- {
- FReturnTypes NewReturn;
- NewReturn.Index = INDEX_NONE;
- NewReturn.Success = false;
- NewReturn.InventoryItem = nullptr;
- if (Data == "NONE")
- {
- return NewReturn; // Invalid Name
- }
- FStaticItemInfo* Row = DataTable->FindRow<FStaticItemInfo>(Data,FString());
- if (!Row)
- {
- return NewReturn; // No Row Found
- }
- for (int32 i = 0; i < Contents.Num(); i++)
- {
- if (Contents[i].InventoryItem != nullptr && Contents[i].Quantity > 0)
- {
- if (Contents[i].InventoryItem == Row->ItemPickup->InventoryItem)
- {
- NewReturn.Index = i ;
- NewReturn.InventoryItem = Row->ItemPickup->InventoryItem;
- NewReturn.Success = true;
- }
- }
- }
- return NewReturn;
- }
- bool UInventoryComponent::DropItem(FName Data)
- {
- if (Data == "NONE")
- {
- UE_LOG(LogTemp, Error,TEXT("%ls Is A Invalid Name \n"),*Data.ToString())
- return false; // Invalid Name
- }
- FStaticItemInfo* Row = DataTable->FindRow<FStaticItemInfo>(Data,FString());
- if (!Row)
- {
- //UE_LOG(LogTemp,Error,TEXT("%p Is A Invalid Row \n"),Row)
- return false; //Row Not Found
- }
- if (Row)
- {
- if (int32 Index; (Index = FindItemInInventory(Data).Index) != INDEX_NONE)
- {
- if (Contents[Index].Quantity > 0 && Contents[Index].InventoryItem != nullptr)
- {
- //Subtract Quantity
- Contents[Index].Quantity -= 1;
- // Definitions
- UWorld* World = GetWorld();
- //Get Players Current Location (Start Of LineTrace) And Look Direction
- FVector PlayerLocation;
- FRotator PlayerRotation;
- World->GetFirstPlayerController()->GetPlayerViewPoint(PlayerLocation, PlayerRotation);
- FVector LineTraceEnd = PlayerLocation + PlayerRotation.Vector() * 200.0f;
- FHitResult Hit;
- //Perform LineTrace
- if (const bool bHit = World->
- LineTraceSingleByChannel(Hit, PlayerLocation, LineTraceEnd, ECC_Visibility))
- {
- LineTraceEnd = Hit.Location;
- }
- //Prepare Spawning Of Actor
- ABaseItemPickup* ItemPickup = Row->ItemPickup;
- UClass* ActorToSpawn = ItemPickup->GetClass();
- FActorSpawnParameters SpawnParameters;
- SpawnParameters.SpawnCollisionHandlingOverride =
- ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
- //Spawn Actor
- ABaseItemPickup* SpawnedActor = World->SpawnActor<ABaseItemPickup>(
- ActorToSpawn, LineTraceEnd, FRotator(0, 0, 0), SpawnParameters);
- if (UStaticMeshComponent* MeshComponent = Cast<UStaticMeshComponent>(SpawnedActor->GetRootComponent()))
- {
- MeshComponent->SetSimulatePhysics(true);
- }
- return true;
- }
- return false;
- }
- }
- UE_LOG(LogTemp, Error, TEXT("File: %s,\n Line: %d, Error: Invalid name %ls"),
- TEXT(__FILE__), __LINE__, *Data.ToString());
- return false;
- }
- bool UInventoryComponent::AddItem(FName Data, int Quantity)
- {
- FStaticItemInfo* Row = DataTable->FindRow<FStaticItemInfo>(Data,FString());
- if (Data == "NONE" || Quantity <= 0)
- {
- return false; // Invalid Use
- }
- if (!Row)
- {
- return false; //Invalid Row
- }
- if (Row)
- {
- const auto [Success, Index, InventoryItem] = FindItemInInventory(Data);
- if (InventoryItem == nullptr)
- {
- return false; // Invalid Item
- }
- //Add Existing Item To Inventory
- if (Success == true && Index != INDEX_NONE)
- {
- Contents[Index].Quantity += Quantity;
- return true;
- }
- else if (Index == INDEX_NONE)
- {
- FInventoryInfo NewItem;
- NewItem.Quantity = Quantity;
- NewItem.InventoryItem = InventoryItem;
- Contents.Add(NewItem);
- return true;
- }
- }
- return false;
- }
- /**
- * UseItem method is used to use an item from the inventory.
- *
- * @param Data The name of the item to be used.
- *
- * @return Returns true if the item was successfully used, false otherwise.
- */
- bool UInventoryComponent::UseItem(FName Data)
- {
- if (Data == "NONE")
- {
- return false; // Invalid Name
- }
- FStaticItemInfo* Row = DataTable->FindRow<FStaticItemInfo>(Data,FString());
- if (!Row)
- {
- return false; // Invalid Row
- }
- const auto [Success, Index, InventoryItem] = FindItemInInventory(Data);
- if (Success)
- {
- AInventoryItem* UsedItem = InventoryItem;
- Contents[Index].Quantity -= 1;
- UsedItem->UseItem(Row,this,Index);
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement