|
|
|
|
| #include "LyraBotCreationComponent.h"
|
| #include "LyraGameMode.h"
|
| #include "Engine/World.h"
|
| #include "GameFramework/PlayerState.h"
|
| #include "GameModes/LyraExperienceManagerComponent.h"
|
| #include "Development/LyraDeveloperSettings.h"
|
| #include "Character/LyraPawnExtensionComponent.h"
|
| #include "AIController.h"
|
| #include "Kismet/GameplayStatics.h"
|
| #include "Character/LyraHealthComponent.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraBotCreationComponent)
|
|
|
| ULyraBotCreationComponent::ULyraBotCreationComponent(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| {
|
| }
|
|
|
| void ULyraBotCreationComponent::BeginPlay()
|
| {
|
| Super::BeginPlay();
|
|
|
|
|
| AGameStateBase* GameState = GetGameStateChecked<AGameStateBase>();
|
| ULyraExperienceManagerComponent* ExperienceComponent = GameState->FindComponentByClass<ULyraExperienceManagerComponent>();
|
| check(ExperienceComponent);
|
| ExperienceComponent->CallOrRegister_OnExperienceLoaded_LowPriority(FOnLyraExperienceLoaded::FDelegate::CreateUObject(this, &ThisClass::OnExperienceLoaded));
|
| }
|
|
|
| void ULyraBotCreationComponent::OnExperienceLoaded(const ULyraExperienceDefinition* Experience)
|
| {
|
| #if WITH_SERVER_CODE
|
| if (HasAuthority())
|
| {
|
| ServerCreateBots();
|
| }
|
| #endif
|
| }
|
|
|
| #if WITH_SERVER_CODE
|
|
|
| void ULyraBotCreationComponent::ServerCreateBots_Implementation()
|
| {
|
| if (BotControllerClass == nullptr)
|
| {
|
| return;
|
| }
|
|
|
| RemainingBotNames = RandomBotNames;
|
|
|
|
|
| int32 EffectiveBotCount = NumBotsToCreate;
|
|
|
|
|
| if (GIsEditor)
|
| {
|
| const ULyraDeveloperSettings* DeveloperSettings = GetDefault<ULyraDeveloperSettings>();
|
|
|
| if (DeveloperSettings->bOverrideBotCount)
|
| {
|
| EffectiveBotCount = DeveloperSettings->OverrideNumPlayerBotsToSpawn;
|
| }
|
| }
|
|
|
|
|
| if (AGameModeBase* GameModeBase = GetGameMode<AGameModeBase>())
|
| {
|
| EffectiveBotCount = UGameplayStatics::GetIntOption(GameModeBase->OptionsString, TEXT("NumBots"), EffectiveBotCount);
|
| }
|
|
|
|
|
| for (int32 Count = 0; Count < EffectiveBotCount; ++Count)
|
| {
|
| SpawnOneBot();
|
| }
|
| }
|
|
|
| FString ULyraBotCreationComponent::CreateBotName(int32 PlayerIndex)
|
| {
|
| FString Result;
|
| if (RemainingBotNames.Num() > 0)
|
| {
|
| const int32 NameIndex = FMath::RandRange(0, RemainingBotNames.Num() - 1);
|
| Result = RemainingBotNames[NameIndex];
|
| RemainingBotNames.RemoveAtSwap(NameIndex);
|
| }
|
| else
|
| {
|
|
|
| PlayerIndex = FMath::RandRange(260, 260+100);
|
| Result = FString::Printf(TEXT("Tinplate %d"), PlayerIndex);
|
| }
|
| return Result;
|
| }
|
|
|
| void ULyraBotCreationComponent::SpawnOneBot()
|
| {
|
| FActorSpawnParameters SpawnInfo;
|
| SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
|
| SpawnInfo.OverrideLevel = GetComponentLevel();
|
| SpawnInfo.ObjectFlags |= RF_Transient;
|
| AAIController* NewController = GetWorld()->SpawnActor<AAIController>(BotControllerClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnInfo);
|
|
|
| if (NewController != nullptr)
|
| {
|
| ALyraGameMode* GameMode = GetGameMode<ALyraGameMode>();
|
| check(GameMode);
|
|
|
| if (NewController->PlayerState != nullptr)
|
| {
|
| NewController->PlayerState->SetPlayerName(CreateBotName(NewController->PlayerState->GetPlayerId()));
|
| }
|
|
|
| GameMode->GenericPlayerInitialization(NewController);
|
| GameMode->RestartPlayer(NewController);
|
|
|
| if (NewController->GetPawn() != nullptr)
|
| {
|
| if (ULyraPawnExtensionComponent* PawnExtComponent = NewController->GetPawn()->FindComponentByClass<ULyraPawnExtensionComponent>())
|
| {
|
| PawnExtComponent->CheckDefaultInitialization();
|
| }
|
| }
|
|
|
| SpawnedBotList.Add(NewController);
|
| }
|
| }
|
|
|
| void ULyraBotCreationComponent::RemoveOneBot()
|
| {
|
| if (SpawnedBotList.Num() > 0)
|
| {
|
|
|
|
|
| const int32 BotToRemoveIndex = FMath::RandRange(0, SpawnedBotList.Num() - 1);
|
|
|
| AAIController* BotToRemove = SpawnedBotList[BotToRemoveIndex];
|
| SpawnedBotList.RemoveAtSwap(BotToRemoveIndex);
|
|
|
| if (BotToRemove)
|
| {
|
|
|
| if (APawn* ControlledPawn = BotToRemove->GetPawn())
|
| {
|
| if (ULyraHealthComponent* HealthComponent = ULyraHealthComponent::FindHealthComponent(ControlledPawn))
|
| {
|
|
|
|
|
| HealthComponent->DamageSelfDestruct();
|
| }
|
| else
|
| {
|
| ControlledPawn->Destroy();
|
| }
|
| }
|
|
|
|
|
| BotToRemove->Destroy();
|
| }
|
| }
|
| }
|
|
|
| #else
|
|
|
| void ULyraBotCreationComponent::ServerCreateBots_Implementation()
|
| {
|
| ensureMsgf(0, TEXT("Bot functions do not exist in LyraClient!"));
|
| }
|
|
|
| void ULyraBotCreationComponent::SpawnOneBot()
|
| {
|
| ensureMsgf(0, TEXT("Bot functions do not exist in LyraClient!"));
|
| }
|
|
|
| void ULyraBotCreationComponent::RemoveOneBot()
|
| {
|
| ensureMsgf(0, TEXT("Bot functions do not exist in LyraClient!"));
|
| }
|
|
|
| #endif
|
|
|