RealtimeStyleTransferRuntime/Source/LyraGame/Inventory/IPickupable.cpp

52 lines
1.6 KiB
C++
Raw Normal View History

2022-05-23 18:41:30 +00:00
// Copyright Epic Games, Inc. All Rights Reserved.
#include "IPickupable.h"
2022-09-13 07:18:28 +00:00
2022-05-23 18:41:30 +00:00
#include "GameFramework/Actor.h"
#include "LyraInventoryManagerComponent.h"
2022-09-13 07:18:28 +00:00
#include "UObject/ScriptInterface.h"
class UActorComponent;
2022-05-23 18:41:30 +00:00
UPickupableStatics::UPickupableStatics()
: Super(FObjectInitializer::Get())
{
}
2022-09-13 07:18:28 +00:00
TScriptInterface<IPickupable> UPickupableStatics::GetFirstPickupableFromActor(AActor* Actor)
2022-05-23 18:41:30 +00:00
{
// If the actor is directly pickupable, return that.
2022-09-13 07:18:28 +00:00
TScriptInterface<IPickupable> PickupableActor(Actor);
2022-05-23 18:41:30 +00:00
if (PickupableActor)
{
return PickupableActor;
}
// If the actor isn't pickupable, it might have a component that has a pickupable interface.
2022-09-13 07:18:28 +00:00
TArray<UActorComponent*> PickupableComponents = Actor ? Actor->GetComponentsByInterface(UPickupable::StaticClass()) : TArray<UActorComponent*>();
2022-05-23 18:41:30 +00:00
if (PickupableComponents.Num() > 0)
{
2022-09-13 07:18:28 +00:00
// Get first pickupable, if the user needs more sophisticated pickup distinction, will need to be solved elsewhere.
2022-05-23 18:41:30 +00:00
return TScriptInterface<IPickupable>(PickupableComponents[0]);
}
return TScriptInterface<IPickupable>();
}
2022-09-13 07:18:28 +00:00
void UPickupableStatics::AddPickupToInventory(ULyraInventoryManagerComponent* InventoryComponent, TScriptInterface<IPickupable> Pickup)
2022-05-23 18:41:30 +00:00
{
2022-09-13 07:18:28 +00:00
if (InventoryComponent && Pickup)
2022-05-23 18:41:30 +00:00
{
2022-09-13 07:18:28 +00:00
const FInventoryPickup& PickupInventory = Pickup->GetPickupInventory();
2022-05-23 18:41:30 +00:00
for (const FPickupTemplate& Template : PickupInventory.Templates)
{
InventoryComponent->AddItemDefinition(Template.ItemDef, Template.StackCount);
}
for (const FPickupInstance& Instance : PickupInventory.Instances)
{
InventoryComponent->AddItemInstance(Instance.Item);
}
}
}