-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAICharacter2.cpp
95 lines (67 loc) · 2.57 KB
/
AICharacter2.cpp
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
// Fill out your copyright notice in the Description page of Project Settings.
#include "HideAndSeekCharacter.h"
#include "AICharacter2.h"
#include "AIController.h"
#include "NavigationSystem.h"
#include "Perception/PawnSensingComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
AAICharacter2::AAICharacter2()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
PawnSensing2 = CreateDefaultSubobject<UPawnSensingComponent>(TEXT("PawnSensing2"));
}
// Called when the game starts or when spawned
void AAICharacter2::BeginPlay()
{
Super::BeginPlay();
PawnSensing2->OnSeePawn.AddDynamic(this, &AAICharacter2::SeePawn);
PawnSensing2->OnHearNoise.AddDynamic(this, &AAICharacter2::OnHearNoise);
UNavigationSystemV1* NavSys = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
FNavLocation NavLoc;
NavSys->GetRandomReachablePointInRadius(GetActorLocation(), 10000.f, NavLoc);
AIC_Ref = Cast<AAIController>(GetController());
if (AIC_Ref)
{
AIC_Ref->MoveToLocation(FVector(1320.f,-3780.f,160.f));
GetWorldTimerManager().SetTimer(Timer, this, &AAICharacter2::NewMovement, 4.f);
GetCharacterMovement()->MaxWalkSpeed = 1500;
}
}
void AAICharacter2::NewMovement()
{
AIC_Ref = Cast<AAIController>(GetController());
if (AIC_Ref)
{
AIC_Ref->MoveToLocation(FVector(-5780.f,-3700.f,218.f));
GetWorldTimerManager().SetTimer(Timer, this, &AAICharacter2::BeginPlay, 4.f);
}
}
// Called every frame
void AAICharacter2::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AAICharacter2::SeePawn(APawn* Pawn)
{
AHideAndSeekCharacter* AISee2 = Cast<AHideAndSeekCharacter>(Pawn);
if (AISee2)
{
GetWorldTimerManager().ClearTimer(Timer);
AIC_Ref->MoveToLocation(GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation());
GetWorldTimerManager().SetTimer(Timer, this, &AAICharacter2::BeginPlay, 2.0f);
GetCharacterMovement()->MaxWalkSpeed = 2500;
}
}
void AAICharacter2::OnHearNoise(APawn* OtherActor, const FVector& Location, float Volume)
{
AHideAndSeekCharacter* AIHear2 = Cast<AHideAndSeekCharacter>(OtherActor);
if (AIHear2)
{
GetWorldTimerManager().ClearTimer(Timer);
AIC_Ref->MoveToLocation(GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation(), -1);
GetWorldTimerManager().SetTimer(Timer, this, &AAICharacter2::BeginPlay, 2.0f);
GetCharacterMovement()->MaxWalkSpeed = 2000;
}
}