RealtimeStyleTransferRuntime/Source/LyraGame/Inventory/LyraInventoryManagerCompone...

181 lines
5.2 KiB
C
Raw Permalink Normal View History

2022-05-23 18:41:30 +00:00
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
2022-09-13 07:18:28 +00:00
#include "Components/ActorComponent.h"
#include "Containers/Array.h"
#include "Containers/ArrayView.h"
#include "Containers/Map.h"
#include "Containers/Set.h"
#include "Containers/SparseArray.h"
#include "Containers/UnrealString.h"
#include "CoreTypes.h"
2022-05-23 18:41:30 +00:00
#include "Net/Serialization/FastArraySerializer.h"
2022-09-13 07:18:28 +00:00
#include "Templates/SubclassOf.h"
#include "Templates/UnrealTemplate.h"
#include "UObject/Class.h"
#include "UObject/UObjectGlobals.h"
2022-05-23 18:41:30 +00:00
#include "LyraInventoryManagerComponent.generated.h"
class ULyraInventoryItemDefinition;
class ULyraInventoryItemInstance;
class ULyraInventoryManagerComponent;
2022-09-13 07:18:28 +00:00
class UObject;
struct FFrame;
2022-05-23 18:41:30 +00:00
struct FLyraInventoryList;
2022-09-13 07:18:28 +00:00
struct FNetDeltaSerializeInfo;
struct FReplicationFlags;
2022-05-23 18:41:30 +00:00
/** A message when an item is added to the inventory */
USTRUCT(BlueprintType)
struct FLyraInventoryChangeMessage
{
GENERATED_BODY()
//@TODO: Tag based names+owning actors for inventories instead of directly exposing the component?
UPROPERTY(BlueprintReadOnly, Category=Inventory)
2022-09-13 07:18:28 +00:00
TObjectPtr<UActorComponent> InventoryOwner = nullptr;
2022-05-23 18:41:30 +00:00
UPROPERTY(BlueprintReadOnly, Category = Inventory)
2022-09-13 07:18:28 +00:00
TObjectPtr<ULyraInventoryItemInstance> Instance = nullptr;
2022-05-23 18:41:30 +00:00
UPROPERTY(BlueprintReadOnly, Category=Inventory)
int32 NewCount = 0;
UPROPERTY(BlueprintReadOnly, Category=Inventory)
int32 Delta = 0;
};
/** A single entry in an inventory */
USTRUCT(BlueprintType)
struct FLyraInventoryEntry : public FFastArraySerializerItem
{
GENERATED_BODY()
FLyraInventoryEntry()
{}
FString GetDebugString() const;
private:
friend FLyraInventoryList;
friend ULyraInventoryManagerComponent;
UPROPERTY()
2022-09-13 07:18:28 +00:00
TObjectPtr<ULyraInventoryItemInstance> Instance = nullptr;
2022-05-23 18:41:30 +00:00
UPROPERTY()
int32 StackCount = 0;
UPROPERTY(NotReplicated)
int32 LastObservedCount = INDEX_NONE;
};
/** List of inventory items */
USTRUCT(BlueprintType)
struct FLyraInventoryList : public FFastArraySerializer
{
GENERATED_BODY()
FLyraInventoryList()
: OwnerComponent(nullptr)
{
}
FLyraInventoryList(UActorComponent* InOwnerComponent)
: OwnerComponent(InOwnerComponent)
{
}
TArray<ULyraInventoryItemInstance*> GetAllItems() const;
public:
//~FFastArraySerializer contract
void PreReplicatedRemove(const TArrayView<int32> RemovedIndices, int32 FinalSize);
void PostReplicatedAdd(const TArrayView<int32> AddedIndices, int32 FinalSize);
void PostReplicatedChange(const TArrayView<int32> ChangedIndices, int32 FinalSize);
//~End of FFastArraySerializer contract
bool NetDeltaSerialize(FNetDeltaSerializeInfo& DeltaParms)
{
return FFastArraySerializer::FastArrayDeltaSerialize<FLyraInventoryEntry, FLyraInventoryList>(Entries, DeltaParms, *this);
}
ULyraInventoryItemInstance* AddEntry(TSubclassOf<ULyraInventoryItemDefinition> ItemClass, int32 StackCount);
void AddEntry(ULyraInventoryItemInstance* Instance);
void RemoveEntry(ULyraInventoryItemInstance* Instance);
private:
void BroadcastChangeMessage(FLyraInventoryEntry& Entry, int32 OldCount, int32 NewCount);
private:
friend ULyraInventoryManagerComponent;
private:
// Replicated list of items
UPROPERTY()
TArray<FLyraInventoryEntry> Entries;
2022-09-13 07:18:28 +00:00
UPROPERTY(NotReplicated)
TObjectPtr<UActorComponent> OwnerComponent;
2022-05-23 18:41:30 +00:00
};
template<>
struct TStructOpsTypeTraits<FLyraInventoryList> : public TStructOpsTypeTraitsBase2<FLyraInventoryList>
{
enum { WithNetDeltaSerializer = true };
};
/**
* Manages an inventory
*/
UCLASS(BlueprintType)
2022-09-13 07:18:28 +00:00
class LYRAGAME_API ULyraInventoryManagerComponent : public UActorComponent
2022-05-23 18:41:30 +00:00
{
GENERATED_BODY()
public:
ULyraInventoryManagerComponent(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly, Category=Inventory)
bool CanAddItemDefinition(TSubclassOf<ULyraInventoryItemDefinition> ItemDef, int32 StackCount = 1);
UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly, Category=Inventory)
ULyraInventoryItemInstance* AddItemDefinition(TSubclassOf<ULyraInventoryItemDefinition> ItemDef, int32 StackCount = 1);
UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly, Category=Inventory)
void AddItemInstance(ULyraInventoryItemInstance* ItemInstance);
UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly, Category=Inventory)
void RemoveItemInstance(ULyraInventoryItemInstance* ItemInstance);
UFUNCTION(BlueprintCallable, Category=Inventory, BlueprintPure=false)
TArray<ULyraInventoryItemInstance*> GetAllItems() const;
UFUNCTION(BlueprintCallable, Category=Inventory, BlueprintPure)
ULyraInventoryItemInstance* FindFirstItemStackByDefinition(TSubclassOf<ULyraInventoryItemDefinition> ItemDef) const;
int32 GetTotalItemCountByDefinition(TSubclassOf<ULyraInventoryItemDefinition> ItemDef) const;
bool ConsumeItemsByDefinition(TSubclassOf<ULyraInventoryItemDefinition> ItemDef, int32 NumToConsume);
//~UObject interface
virtual bool ReplicateSubobjects(class UActorChannel* Channel, class FOutBunch* Bunch, FReplicationFlags* RepFlags) override;
2022-09-13 07:18:28 +00:00
virtual void ReadyForReplication() override;
2022-05-23 18:41:30 +00:00
//~End of UObject interface
private:
UPROPERTY(Replicated)
FLyraInventoryList InventoryList;
};