2025-05-13 23:02:57 +09:00

44 lines
828 B
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Tower.h"
#include "Tank.h"
#include "Kismet/GameplayStatics.h"
void ATower::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (InFireRange())
RotateTurret(Tank->GetActorLocation());
}
void ATower::HandleDestruction()
{
Super::HandleDestruction();
Destroy();
}
void ATower::BeginPlay()
{
Super::BeginPlay();
Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));
GetWorldTimerManager().SetTimer(FireRateTimerHandle, this, &ATower::TryFire, FireRate, true);
}
void ATower::TryFire()
{
if (InFireRange())
Fire();
}
bool ATower::InFireRange() const
{
if (Tank)
{
float Distance = FVector::Dist(this->GetActorLocation(), Tank->GetActorLocation());
if (Distance <= FireRange)
return true;
}
return false;
}