diff --git a/Source/ToonTanks/Tower.cpp b/Source/ToonTanks/Tower.cpp index 35cca05..6c8c297 100644 --- a/Source/ToonTanks/Tower.cpp +++ b/Source/ToonTanks/Tower.cpp @@ -8,12 +8,7 @@ void ATower::Tick(float DeltaTime) { Super::Tick(DeltaTime); - if (Tank == nullptr) - { - return; - } - float Distance = FVector::Dist(this->GetActorLocation(), Tank->GetActorLocation()); - if (Distance <= FireRange) + if (InFireRange()) { RotateTurret(Tank->GetActorLocation()); } @@ -23,4 +18,26 @@ void ATower::BeginPlay() { Super::BeginPlay(); Tank = Cast(UGameplayStatics::GetPlayerPawn(this, 0)); -} \ No newline at end of file + GetWorldTimerManager().SetTimer(FireRateTimerHandle, this, &ATower::CheckFireCondition, FireRate, true); +} + +void ATower::CheckFireCondition() +{ + if (InFireRange()) + { + Fire(); + } +} + +bool ATower::InFireRange() +{ + if (Tank) + { + float Distance = FVector::Dist(this->GetActorLocation(), Tank->GetActorLocation()); + if (Distance <= FireRange) + { + return true; + } + } + return false; +} diff --git a/Source/ToonTanks/Tower.h b/Source/ToonTanks/Tower.h index ea5d80c..543d242 100644 --- a/Source/ToonTanks/Tower.h +++ b/Source/ToonTanks/Tower.h @@ -25,5 +25,12 @@ private: UPROPERTY(EditAnywhere, Category = "Rotate") float FireRange = 400; + + FTimerHandle FireRateTimerHandle; + float FireRate = 2.0f; + + void CheckFireCondition(); + + bool InFireRange(); };