// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #include "AbilitySystem/LyraAbilitySet.h" #include "Components/PawnComponent.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 "HAL/Platform.h" #include "Net/Serialization/FastArraySerializer.h" #include "Templates/SubclassOf.h" #include "Templates/UnrealTemplate.h" #include "UObject/Class.h" #include "UObject/UObjectGlobals.h" #include "LyraEquipmentManagerComponent.generated.h" class UActorComponent; class ULyraAbilitySystemComponent; class ULyraEquipmentDefinition; class ULyraEquipmentInstance; class ULyraEquipmentManagerComponent; class UObject; struct FFrame; struct FLyraEquipmentList; struct FNetDeltaSerializeInfo; struct FReplicationFlags; /** A single piece of applied equipment */ USTRUCT(BlueprintType) struct FLyraAppliedEquipmentEntry : public FFastArraySerializerItem { GENERATED_BODY() FLyraAppliedEquipmentEntry() {} FString GetDebugString() const; private: friend FLyraEquipmentList; friend ULyraEquipmentManagerComponent; // The equipment class that got equipped UPROPERTY() TSubclassOf EquipmentDefinition; UPROPERTY() TObjectPtr Instance = nullptr; // Authority-only list of granted handles UPROPERTY(NotReplicated) FLyraAbilitySet_GrantedHandles GrantedHandles; }; /** List of applied equipment */ USTRUCT(BlueprintType) struct FLyraEquipmentList : public FFastArraySerializer { GENERATED_BODY() FLyraEquipmentList() : OwnerComponent(nullptr) { } FLyraEquipmentList(UActorComponent* InOwnerComponent) : OwnerComponent(InOwnerComponent) { } public: //~FFastArraySerializer contract void PreReplicatedRemove(const TArrayView RemovedIndices, int32 FinalSize); void PostReplicatedAdd(const TArrayView AddedIndices, int32 FinalSize); void PostReplicatedChange(const TArrayView ChangedIndices, int32 FinalSize); //~End of FFastArraySerializer contract bool NetDeltaSerialize(FNetDeltaSerializeInfo& DeltaParms) { return FFastArraySerializer::FastArrayDeltaSerialize(Entries, DeltaParms, *this); } ULyraEquipmentInstance* AddEntry(TSubclassOf EquipmentDefinition); void RemoveEntry(ULyraEquipmentInstance* Instance); private: ULyraAbilitySystemComponent* GetAbilitySystemComponent() const; friend ULyraEquipmentManagerComponent; private: // Replicated list of equipment entries UPROPERTY() TArray Entries; UPROPERTY(NotReplicated) TObjectPtr OwnerComponent; }; template<> struct TStructOpsTypeTraits : public TStructOpsTypeTraitsBase2 { enum { WithNetDeltaSerializer = true }; }; /** * Manages equipment applied to a pawn */ UCLASS(BlueprintType, Const) class ULyraEquipmentManagerComponent : public UPawnComponent { GENERATED_BODY() public: ULyraEquipmentManagerComponent(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get()); UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly) ULyraEquipmentInstance* EquipItem(TSubclassOf EquipmentDefinition); UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly) void UnequipItem(ULyraEquipmentInstance* ItemInstance); //~UObject interface virtual bool ReplicateSubobjects(class UActorChannel* Channel, class FOutBunch* Bunch, FReplicationFlags* RepFlags) override; //~End of UObject interface //~UActorComponent interface //virtual void EndPlay() override; virtual void InitializeComponent() override; virtual void UninitializeComponent() override; virtual void ReadyForReplication() override; //~End of UActorComponent interface /** Returns the first equipped instance of a given type, or nullptr if none are found */ UFUNCTION(BlueprintCallable, BlueprintPure) ULyraEquipmentInstance* GetFirstInstanceOfType(TSubclassOf InstanceType); /** Returns all equipped instances of a given type, or an empty array if none are found */ UFUNCTION(BlueprintCallable, BlueprintPure) TArray GetEquipmentInstancesOfType(TSubclassOf InstanceType) const; template T* GetFirstInstanceOfType() { return (T*)GetFirstInstanceOfType(T::StaticClass()); } private: UPROPERTY(Replicated) FLyraEquipmentList EquipmentList; };