Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #pragma once
- #include "ZombieController.h"
- #include "Runtime/AIModule/Classes/BehaviorTree/BlackboardComponent.h"
- #include "Runtime/Engine/Classes/AI/Navigation/NavigationSystem.h"
- #include "ZombieCharacter.h"
- #include "Engine.h"
- #include "random"
- #include "time.h"
- #include "Target.h"
- const FName AZombieController::randomLocationKey("RandomLocation");
- AZombieController::AZombieController()
- {
- blackboardComp = CreateDefaultSubobject<UBlackboardComponent>(TEXT("Blackboard component"));
- behaviorTreeComp = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("Behavior tree component"));
- }
- void AZombieController::Possess(APawn* pawn)
- {
- Super::Possess(pawn);
- AZombieCharacter* zombie = (AZombieCharacter*)pawn;
- blackboardComp->InitializeBlackboard(*zombie->behaviorTree->BlackboardAsset);
- zombie->SetController(this);
- blackboardComp->SetValueAsVector(randomLocationKey, GetRandomLocation(pawn->GetActorLocation(), 2000, GetWorld(), pawn));
- TArray<AActor*> v;
- UGameplayStatics::GetAllActorsOfClass(GetWorld(), ATarget::StaticClass(), v);
- blackboardComp->SetValueAsObject("Target", v[0]);
- auto res = blackboardComp->GetValueAsVector(randomLocationKey);
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("Random location set to: (%f, %f, %f)"), res.X, res.Y, res.Z));
- /*
- if (!navSystem->GetRandomReachablePointInRadius(FVector(0, 0, 0), 10000.0f, location))
- {
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Could not find a location"));
- behaviorTreeComp->StartTree(*zombie->behaviorTree);
- return;
- }
- if (!blackboardComp)
- {
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Could not find the blackboard"));
- }
- else
- blackboardComp->SetValueAsVector(randomLocationKey, location.Location);
- */
- behaviorTreeComp->StartTree(*zombie->behaviorTree);
- }
- void AZombieController::ChangeRandomLocation(APawn* pawn, UWorld* world)
- {
- /*
- FNavLocation location;
- auto navSystem = world->GetNavigationSystem();
- if (!navSystem->GetRandomReachablePointInRadius(FVector(0, 0, 0), 10000.0f, location))
- {
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Could not find a location"));
- return;
- }
- */
- if (!blackboardComp)
- {
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Could not find the blackboard"));
- return;
- }
- if (!behaviorTreeComp->GetActiveNode())
- {
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("The behavior tree is fucking broken"));
- }
- blackboardComp->SetValueAsVector(randomLocationKey, GetRandomLocation(pawn->GetActorLocation(), 2000, world, pawn));
- auto res = blackboardComp->GetValueAsVector(randomLocationKey);
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("Random location set to: (%f, %f, %f)"), res.X, res.Y, res.Z));
- }
- FVector AZombieController::GetRandomLocation(const FVector& origin, unsigned int radius, UWorld* world, AActor* character)
- {
- //Only generate X and Y
- //Z will be 0
- FVector result(0, 0, 130);
- srand(time(0));
- int Xdeviation = rand() % (radius * 2) - radius;
- result.X = Xdeviation + origin.X;
- int maxYdeviation = sqrt(radius*radius - Xdeviation * Xdeviation);
- result.Y = rand() % (maxYdeviation * 2) - maxYdeviation + origin.Y;
- /*
- do
- {
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Generating path"));
- //First generate X
- int Xdeviation = rand() % (radius * 2) - radius;
- result.X = Xdeviation + origin.X;
- int maxYdeviation = sqrt(radius*radius - Xdeviation * Xdeviation);
- result.Y = rand() % (maxYdeviation * 2) - maxYdeviation + origin.Y;
- } while (false);// (!UNavigationSystem::FindPathToLocationSynchronously(world, origin, result, character));
- */
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment