// © 2024 Long Ly. All rights reserved. Any unauthorized use, reproduction, or distribution of this trademark is strictly prohibited and may result in legal action. #pragma once #include "CoreMinimal.h" #include "Components/GameFrameworkInitStateInterface.h" #include "Components/PawnComponent.h" #include "OLSPawnExtensionComponent.generated.h" DECLARE_LOG_CATEGORY_EXTERN(LogOLSPawnExtensionComponent, Verbose, All); namespace EEndPlayReason { enum Type : int; } /** * Component that adds functionality to all Pawn classes so it can be used for characters/vehicles/etc. * This coordinates the initialization of other components. */ UCLASS() class OLS_API UOLSPawnExtensionComponent : public UPawnComponent, public IGameFrameworkInitStateInterface { GENERATED_BODY() public: // Sets default values for this component's properties UOLSPawnExtensionComponent(const FObjectInitializer& objectInitializer); /** The name of this overall feature, this one depends on the other named component features */ static const FName NAME_ActorFeatureName; //~ Begin UActorComponent interface virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override; //~ End UActorComponent interface //~ Begin IGameFrameworkInitStateInterface interface virtual FName GetFeatureName() const override; virtual bool CanChangeInitState(UGameFrameworkComponentManager* manager, FGameplayTag currentState, FGameplayTag desiredState) const override; virtual void HandleChangeInitState(UGameFrameworkComponentManager* manager, FGameplayTag currentState, FGameplayTag desiredState) override; virtual void OnActorInitStateChanged(const FActorInitStateChangedParams& Params) override; virtual void CheckDefaultInitialization() override; //~ End IGameFrameworkInitStateInterface interface /** Returns the pawn extension component if one exists on the specified actor. */ UFUNCTION(BlueprintPure, Category = "OLS|Pawn") static class UOLSPawnExtensionComponent* FindPawnExtensionComponent(const AActor* actor); /** Gets the current ability system component, which may be owned by a different actor */ UFUNCTION(BlueprintPure, Category = "OLS|Pawn") class UOLSAbilitySystemComponent* GetOLSAbilitySystemComponent() const ; public: /** Gets the pawn data, which is used to specify pawn properties in data */ template const T* GetPawnData() const { return Cast(PawnData); } /** Sets the current pawn data */ void SetPawnData(const class UOLSPawnDataAsset* pawnData); /** Should be called by the owning pawn to become the avatar of the ability system. */ void InitializeAbilitySystem(class UOLSAbilitySystemComponent* asc, AActor* ownerActor); /** Should be called by the owning pawn to remove itself as the avatar of the ability system. */ void UninitializeAbilitySystem(); /** Should be called by the owning pawn when the pawn's controller changes. */ void HandleControllerChanged(); /** Should be called by the owning pawn when the player state has been replicated. */ void HandlePlayerStateReplicated(); /** Should be called by the owning pawn when the input component is setup. */ void SetupPlayerInputComponent(); /** Register with the OnAbilitySystemInitialized delegate and broadcast if our pawn has been registered with the ability system component */ void OnAbilitySystemInitialized_RegisterAndCall(FSimpleMulticastDelegate::FDelegate delegate); /** Register with the OnAbilitySystemUninitialized delegate fired when our pawn is removed as the ability system's avatar actor */ void OnAbilitySystemUninitialized_Register(FSimpleMulticastDelegate::FDelegate delegate); protected: UFUNCTION() void OnRep_PawnData(); protected: /** Delegate fired when our pawn becomes the ability system's avatar actor */ FSimpleMulticastDelegate OnAbilitySystemInitialized; /** Delegate fired when our pawn is removed as the ability system's avatar actor */ FSimpleMulticastDelegate OnAbilitySystemUninitialized; protected: /** Pawn data used to create the pawn. Specified from a spawn function or on a placed instance. */ UPROPERTY(EditInstanceOnly, ReplicatedUsing = OnRep_PawnData, Category = "Lyra|Pawn") TObjectPtr PawnData = nullptr; /** Pointer to the ability system component that is cached for convenience. */ UPROPERTY(Transient) TObjectPtr AbilitySystemComponent = nullptr; };