File size: 5,009 Bytes
7fd553e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | // Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraPlayerBotController.h"
#include "AbilitySystemComponent.h"
#include "AbilitySystemGlobals.h"
#include "Engine/World.h"
#include "GameFramework/PlayerState.h"
#include "GameModes/LyraGameMode.h"
#include "LyraLogChannels.h"
#include "Perception/AIPerceptionComponent.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraPlayerBotController)
class UObject;
ALyraPlayerBotController::ALyraPlayerBotController(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
bWantsPlayerState = true;
bStopAILogicOnUnposses = false;
}
void ALyraPlayerBotController::OnPlayerStateChangedTeam(UObject* TeamAgent, int32 OldTeam, int32 NewTeam)
{
ConditionalBroadcastTeamChanged(this, IntegerToGenericTeamId(OldTeam), IntegerToGenericTeamId(NewTeam));
}
void ALyraPlayerBotController::OnPlayerStateChanged()
{
// Empty, place for derived classes to implement without having to hook all the other events
}
void ALyraPlayerBotController::BroadcastOnPlayerStateChanged()
{
OnPlayerStateChanged();
// Unbind from the old player state, if any
FGenericTeamId OldTeamID = FGenericTeamId::NoTeam;
if (LastSeenPlayerState != nullptr)
{
if (ILyraTeamAgentInterface* PlayerStateTeamInterface = Cast<ILyraTeamAgentInterface>(LastSeenPlayerState))
{
OldTeamID = PlayerStateTeamInterface->GetGenericTeamId();
PlayerStateTeamInterface->GetTeamChangedDelegateChecked().RemoveAll(this);
}
}
// Bind to the new player state, if any
FGenericTeamId NewTeamID = FGenericTeamId::NoTeam;
if (PlayerState != nullptr)
{
if (ILyraTeamAgentInterface* PlayerStateTeamInterface = Cast<ILyraTeamAgentInterface>(PlayerState))
{
NewTeamID = PlayerStateTeamInterface->GetGenericTeamId();
PlayerStateTeamInterface->GetTeamChangedDelegateChecked().AddDynamic(this, &ThisClass::OnPlayerStateChangedTeam);
}
}
// Broadcast the team change (if it really has)
ConditionalBroadcastTeamChanged(this, OldTeamID, NewTeamID);
LastSeenPlayerState = PlayerState;
}
void ALyraPlayerBotController::InitPlayerState()
{
Super::InitPlayerState();
BroadcastOnPlayerStateChanged();
}
void ALyraPlayerBotController::CleanupPlayerState()
{
Super::CleanupPlayerState();
BroadcastOnPlayerStateChanged();
}
void ALyraPlayerBotController::OnRep_PlayerState()
{
Super::OnRep_PlayerState();
BroadcastOnPlayerStateChanged();
}
void ALyraPlayerBotController::SetGenericTeamId(const FGenericTeamId& NewTeamID)
{
UE_LOG(LogLyraTeams, Error, TEXT("You can't set the team ID on a player bot controller (%s); it's driven by the associated player state"), *GetPathNameSafe(this));
}
FGenericTeamId ALyraPlayerBotController::GetGenericTeamId() const
{
if (ILyraTeamAgentInterface* PSWithTeamInterface = Cast<ILyraTeamAgentInterface>(PlayerState))
{
return PSWithTeamInterface->GetGenericTeamId();
}
return FGenericTeamId::NoTeam;
}
FOnLyraTeamIndexChangedDelegate* ALyraPlayerBotController::GetOnTeamIndexChangedDelegate()
{
return &OnTeamChangedDelegate;
}
void ALyraPlayerBotController::ServerRestartController()
{
if (GetNetMode() == NM_Client)
{
return;
}
ensure((GetPawn() == nullptr) && IsInState(NAME_Inactive));
if (IsInState(NAME_Inactive) || (IsInState(NAME_Spectating)))
{
ALyraGameMode* const GameMode = GetWorld()->GetAuthGameMode<ALyraGameMode>();
if ((GameMode == nullptr) || !GameMode->ControllerCanRestart(this))
{
return;
}
// If we're still attached to a Pawn, leave it
if (GetPawn() != nullptr)
{
UnPossess();
}
// Re-enable input, similar to code in ClientRestart
ResetIgnoreInputFlags();
GameMode->RestartPlayer(this);
}
}
ETeamAttitude::Type ALyraPlayerBotController::GetTeamAttitudeTowards(const AActor& Other) const
{
if (const APawn* OtherPawn = Cast<APawn>(&Other)) {
if (const ILyraTeamAgentInterface* TeamAgent = Cast<ILyraTeamAgentInterface>(OtherPawn->GetController()))
{
FGenericTeamId OtherTeamID = TeamAgent->GetGenericTeamId();
//Checking Other pawn ID to define Attitude
if (OtherTeamID.GetId() != GetGenericTeamId().GetId())
{
return ETeamAttitude::Hostile;
}
else
{
return ETeamAttitude::Friendly;
}
}
}
return ETeamAttitude::Neutral;
}
void ALyraPlayerBotController::UpdateTeamAttitude(UAIPerceptionComponent* AIPerception)
{
if (AIPerception)
{
AIPerception->RequestStimuliListenerUpdate();
}
}
void ALyraPlayerBotController::OnUnPossess()
{
// Make sure the pawn that is being unpossessed doesn't remain our ASC's avatar actor
if (APawn* PawnBeingUnpossessed = GetPawn())
{
if (UAbilitySystemComponent* ASC = UAbilitySystemGlobals::GetAbilitySystemComponentFromActor(PlayerState))
{
if (ASC->GetAvatarActor() == PawnBeingUnpossessed)
{
ASC->SetAvatarActor(nullptr);
}
}
}
Super::OnUnPossess();
}
|