RealtimeStyleTransferRuntime/Source/LyraGame/Equipment/LyraEquipmentManagerCompone...

166 lines
4.6 KiB
C
Raw Normal View History

2022-05-23 18:41:30 +00:00
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "AbilitySystem/LyraAbilitySet.h"
#include "Components/PawnComponent.h"
2022-09-13 07:18:28 +00:00
#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"
2022-05-23 18:41:30 +00:00
#include "LyraEquipmentManagerComponent.generated.h"
2022-09-13 07:18:28 +00:00
class UActorComponent;
class ULyraAbilitySystemComponent;
2022-05-23 18:41:30 +00:00
class ULyraEquipmentDefinition;
class ULyraEquipmentInstance;
class ULyraEquipmentManagerComponent;
2022-09-13 07:18:28 +00:00
class UObject;
struct FFrame;
struct FLyraEquipmentList;
struct FNetDeltaSerializeInfo;
struct FReplicationFlags;
2022-05-23 18:41:30 +00:00
/** 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<ULyraEquipmentDefinition> EquipmentDefinition;
UPROPERTY()
2022-09-13 07:18:28 +00:00
TObjectPtr<ULyraEquipmentInstance> Instance = nullptr;
2022-05-23 18:41:30 +00:00
// 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<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<FLyraAppliedEquipmentEntry, FLyraEquipmentList>(Entries, DeltaParms, *this);
}
ULyraEquipmentInstance* AddEntry(TSubclassOf<ULyraEquipmentDefinition> EquipmentDefinition);
void RemoveEntry(ULyraEquipmentInstance* Instance);
private:
ULyraAbilitySystemComponent* GetAbilitySystemComponent() const;
friend ULyraEquipmentManagerComponent;
private:
// Replicated list of equipment entries
UPROPERTY()
TArray<FLyraAppliedEquipmentEntry> 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<FLyraEquipmentList> : public TStructOpsTypeTraitsBase2<FLyraEquipmentList>
{
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<ULyraEquipmentDefinition> 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;
2022-09-13 07:18:28 +00:00
virtual void ReadyForReplication() override;
2022-05-23 18:41:30 +00:00
//~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<ULyraEquipmentInstance> InstanceType);
/** Returns all equipped instances of a given type, or an empty array if none are found */
UFUNCTION(BlueprintCallable, BlueprintPure)
TArray<ULyraEquipmentInstance*> GetEquipmentInstancesOfType(TSubclassOf<ULyraEquipmentInstance> InstanceType) const;
template <typename T>
T* GetFirstInstanceOfType()
{
return (T*)GetFirstInstanceOfType(T::StaticClass());
}
private:
UPROPERTY(Replicated)
FLyraEquipmentList EquipmentList;
};