|
|
|
|
| #include "LyraPlayerState.h"
|
|
|
| #include "AbilitySystem/Attributes/LyraCombatSet.h"
|
| #include "AbilitySystem/Attributes/LyraHealthSet.h"
|
| #include "AbilitySystem/LyraAbilitySet.h"
|
| #include "AbilitySystem/LyraAbilitySystemComponent.h"
|
| #include "Character/LyraPawnData.h"
|
| #include "Character/LyraPawnExtensionComponent.h"
|
| #include "Components/GameFrameworkComponentManager.h"
|
| #include "Engine/World.h"
|
| #include "GameFramework/GameplayMessageSubsystem.h"
|
| #include "GameModes/LyraExperienceManagerComponent.h"
|
|
|
| #include "GameModes/LyraGameMode.h"
|
| #include "LyraLogChannels.h"
|
| #include "LyraPlayerController.h"
|
| #include "Messages/LyraVerbMessage.h"
|
| #include "Net/UnrealNetwork.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraPlayerState)
|
|
|
| class AController;
|
| class APlayerState;
|
| class FLifetimeProperty;
|
|
|
| const FName ALyraPlayerState::NAME_LyraAbilityReady("LyraAbilitiesReady");
|
|
|
| ALyraPlayerState::ALyraPlayerState(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| , MyPlayerConnectionType(ELyraPlayerConnectionType::Player)
|
| {
|
| AbilitySystemComponent = ObjectInitializer.CreateDefaultSubobject<ULyraAbilitySystemComponent>(this, TEXT("AbilitySystemComponent"));
|
| AbilitySystemComponent->SetIsReplicated(true);
|
| AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed);
|
|
|
|
|
| HealthSet = CreateDefaultSubobject<ULyraHealthSet>(TEXT("HealthSet"));
|
| CombatSet = CreateDefaultSubobject<ULyraCombatSet>(TEXT("CombatSet"));
|
|
|
|
|
| SetNetUpdateFrequency(100.0f);
|
|
|
| MyTeamID = FGenericTeamId::NoTeam;
|
| MySquadID = INDEX_NONE;
|
| }
|
|
|
| void ALyraPlayerState::PreInitializeComponents()
|
| {
|
| Super::PreInitializeComponents();
|
| }
|
|
|
| void ALyraPlayerState::Reset()
|
| {
|
| Super::Reset();
|
| }
|
|
|
| void ALyraPlayerState::ClientInitialize(AController* C)
|
| {
|
| Super::ClientInitialize(C);
|
|
|
| if (ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(GetPawn()))
|
| {
|
| PawnExtComp->CheckDefaultInitialization();
|
| }
|
| }
|
|
|
| void ALyraPlayerState::CopyProperties(APlayerState* PlayerState)
|
| {
|
| Super::CopyProperties(PlayerState);
|
|
|
|
|
| }
|
|
|
| void ALyraPlayerState::OnDeactivated()
|
| {
|
| bool bDestroyDeactivatedPlayerState = false;
|
|
|
| switch (GetPlayerConnectionType())
|
| {
|
| case ELyraPlayerConnectionType::Player:
|
| case ELyraPlayerConnectionType::InactivePlayer:
|
|
|
|
|
| bDestroyDeactivatedPlayerState = true;
|
| break;
|
| default:
|
| bDestroyDeactivatedPlayerState = true;
|
| break;
|
| }
|
|
|
| SetPlayerConnectionType(ELyraPlayerConnectionType::InactivePlayer);
|
|
|
| if (bDestroyDeactivatedPlayerState)
|
| {
|
| Destroy();
|
| }
|
| }
|
|
|
| void ALyraPlayerState::OnReactivated()
|
| {
|
| if (GetPlayerConnectionType() == ELyraPlayerConnectionType::InactivePlayer)
|
| {
|
| SetPlayerConnectionType(ELyraPlayerConnectionType::Player);
|
| }
|
| }
|
|
|
| void ALyraPlayerState::OnExperienceLoaded(const ULyraExperienceDefinition* )
|
| {
|
| if (ALyraGameMode* LyraGameMode = GetWorld()->GetAuthGameMode<ALyraGameMode>())
|
| {
|
| if (const ULyraPawnData* NewPawnData = LyraGameMode->GetPawnDataForController(GetOwningController()))
|
| {
|
| SetPawnData(NewPawnData);
|
| }
|
| else
|
| {
|
| UE_LOG(LogLyra, Error, TEXT("ALyraPlayerState::OnExperienceLoaded(): Unable to find PawnData to initialize player state [%s]!"), *GetNameSafe(this));
|
| }
|
| }
|
| }
|
|
|
| void ALyraPlayerState::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
|
| {
|
| Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
|
|
| FDoRepLifetimeParams SharedParams;
|
| SharedParams.bIsPushBased = true;
|
|
|
| DOREPLIFETIME_WITH_PARAMS_FAST(ThisClass, PawnData, SharedParams);
|
| DOREPLIFETIME_WITH_PARAMS_FAST(ThisClass, MyPlayerConnectionType, SharedParams)
|
| DOREPLIFETIME_WITH_PARAMS_FAST(ThisClass, MyTeamID, SharedParams);
|
| DOREPLIFETIME_WITH_PARAMS_FAST(ThisClass, MySquadID, SharedParams);
|
|
|
| SharedParams.Condition = ELifetimeCondition::COND_SkipOwner;
|
| DOREPLIFETIME_WITH_PARAMS_FAST(ThisClass, ReplicatedViewRotation, SharedParams);
|
|
|
| DOREPLIFETIME(ThisClass, StatTags);
|
| }
|
|
|
| FRotator ALyraPlayerState::GetReplicatedViewRotation() const
|
| {
|
|
|
| return ReplicatedViewRotation;
|
| }
|
|
|
| void ALyraPlayerState::SetReplicatedViewRotation(const FRotator& NewRotation)
|
| {
|
| if (NewRotation != ReplicatedViewRotation)
|
| {
|
| MARK_PROPERTY_DIRTY_FROM_NAME(ThisClass, ReplicatedViewRotation, this);
|
| ReplicatedViewRotation = NewRotation;
|
| }
|
| }
|
|
|
| ALyraPlayerController* ALyraPlayerState::GetLyraPlayerController() const
|
| {
|
| return Cast<ALyraPlayerController>(GetOwner());
|
| }
|
|
|
| UAbilitySystemComponent* ALyraPlayerState::GetAbilitySystemComponent() const
|
| {
|
| return GetLyraAbilitySystemComponent();
|
| }
|
|
|
| void ALyraPlayerState::PostInitializeComponents()
|
| {
|
| Super::PostInitializeComponents();
|
|
|
| check(AbilitySystemComponent);
|
| AbilitySystemComponent->InitAbilityActorInfo(this, GetPawn());
|
|
|
| UWorld* World = GetWorld();
|
| if (World && World->IsGameWorld() && World->GetNetMode() != NM_Client)
|
| {
|
| AGameStateBase* GameState = GetWorld()->GetGameState();
|
| check(GameState);
|
| ULyraExperienceManagerComponent* ExperienceComponent = GameState->FindComponentByClass<ULyraExperienceManagerComponent>();
|
| check(ExperienceComponent);
|
| ExperienceComponent->CallOrRegister_OnExperienceLoaded(FOnLyraExperienceLoaded::FDelegate::CreateUObject(this, &ThisClass::OnExperienceLoaded));
|
| }
|
| }
|
|
|
| void ALyraPlayerState::SetPawnData(const ULyraPawnData* InPawnData)
|
| {
|
| check(InPawnData);
|
|
|
| if (GetLocalRole() != ROLE_Authority)
|
| {
|
| return;
|
| }
|
|
|
| if (PawnData)
|
| {
|
| UE_LOG(LogLyra, Error, TEXT("Trying to set PawnData [%s] on player state [%s] that already has valid PawnData [%s]."), *GetNameSafe(InPawnData), *GetNameSafe(this), *GetNameSafe(PawnData));
|
| return;
|
| }
|
|
|
| MARK_PROPERTY_DIRTY_FROM_NAME(ThisClass, PawnData, this);
|
| PawnData = InPawnData;
|
|
|
| for (const ULyraAbilitySet* AbilitySet : PawnData->AbilitySets)
|
| {
|
| if (AbilitySet)
|
| {
|
| AbilitySet->GiveToAbilitySystem(AbilitySystemComponent, nullptr);
|
| }
|
| }
|
|
|
| UGameFrameworkComponentManager::SendGameFrameworkComponentExtensionEvent(this, NAME_LyraAbilityReady);
|
|
|
| ForceNetUpdate();
|
| }
|
|
|
| void ALyraPlayerState::OnRep_PawnData()
|
| {
|
| }
|
|
|
| void ALyraPlayerState::SetPlayerConnectionType(ELyraPlayerConnectionType NewType)
|
| {
|
| MARK_PROPERTY_DIRTY_FROM_NAME(ThisClass, MyPlayerConnectionType, this);
|
| MyPlayerConnectionType = NewType;
|
| }
|
|
|
| void ALyraPlayerState::SetSquadID(int32 NewSquadId)
|
| {
|
| if (HasAuthority())
|
| {
|
| MARK_PROPERTY_DIRTY_FROM_NAME(ThisClass, MySquadID, this);
|
|
|
| MySquadID = NewSquadId;
|
| }
|
| }
|
|
|
| void ALyraPlayerState::SetGenericTeamId(const FGenericTeamId& NewTeamID)
|
| {
|
| if (HasAuthority())
|
| {
|
| const FGenericTeamId OldTeamID = MyTeamID;
|
|
|
| MARK_PROPERTY_DIRTY_FROM_NAME(ThisClass, MyTeamID, this);
|
| MyTeamID = NewTeamID;
|
| ConditionalBroadcastTeamChanged(this, OldTeamID, NewTeamID);
|
| }
|
| else
|
| {
|
| UE_LOG(LogLyraTeams, Error, TEXT("Cannot set team for %s on non-authority"), *GetPathName(this));
|
| }
|
| }
|
|
|
| FGenericTeamId ALyraPlayerState::GetGenericTeamId() const
|
| {
|
| return MyTeamID;
|
| }
|
|
|
| FOnLyraTeamIndexChangedDelegate* ALyraPlayerState::GetOnTeamIndexChangedDelegate()
|
| {
|
| return &OnTeamChangedDelegate;
|
| }
|
|
|
| void ALyraPlayerState::OnRep_MyTeamID(FGenericTeamId OldTeamID)
|
| {
|
| ConditionalBroadcastTeamChanged(this, OldTeamID, MyTeamID);
|
| }
|
|
|
| void ALyraPlayerState::OnRep_MySquadID()
|
| {
|
|
|
| }
|
|
|
| void ALyraPlayerState::AddStatTagStack(FGameplayTag Tag, int32 StackCount)
|
| {
|
| StatTags.AddStack(Tag, StackCount);
|
| }
|
|
|
| void ALyraPlayerState::RemoveStatTagStack(FGameplayTag Tag, int32 StackCount)
|
| {
|
| StatTags.RemoveStack(Tag, StackCount);
|
| }
|
|
|
| int32 ALyraPlayerState::GetStatTagStackCount(FGameplayTag Tag) const
|
| {
|
| return StatTags.GetStackCount(Tag);
|
| }
|
|
|
| bool ALyraPlayerState::HasStatTag(FGameplayTag Tag) const
|
| {
|
| return StatTags.ContainsTag(Tag);
|
| }
|
|
|
| void ALyraPlayerState::ClientBroadcastMessage_Implementation(const FLyraVerbMessage Message)
|
| {
|
|
|
| if (GetNetMode() == NM_Client)
|
| {
|
| UGameplayMessageSubsystem::Get(this).BroadcastMessage(Message.Verb, Message);
|
| }
|
| }
|
|
|
|
|