57 lines
1.0 KiB
C++
57 lines
1.0 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Tower.h"
|
|
#include "Tank.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "TimerManager.h"
|
|
|
|
void ATower::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
if (InFireRange())
|
|
{
|
|
// if in range, rotate turret toward the tank
|
|
RotateTurret(Tank->GetActorLocation());
|
|
}
|
|
}
|
|
|
|
void ATower::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));
|
|
|
|
GetWorldTimerManager().SetTimer(
|
|
FireRateTimerHandle,
|
|
this,
|
|
&ATower::CheckFireCondition,
|
|
FireRate,
|
|
true);
|
|
}
|
|
|
|
void ATower::CheckFireCondition()
|
|
{
|
|
// Find the distance to the tank
|
|
if (InFireRange())
|
|
{
|
|
//UE_LOG(LogTemp, Warning, TEXT("Shot Fired!"));
|
|
Fire();
|
|
}
|
|
}
|
|
|
|
bool ATower::InFireRange()
|
|
{
|
|
// Find the distance to the tank
|
|
if (Tank)
|
|
{
|
|
float Distance = FVector::Dist(GetActorLocation(), Tank->GetActorLocation());
|
|
// check to see if the tank is in range
|
|
if (Distance <= FireRange)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
} |