From 4ab591e4b9b1380589694ecd5948c708415d22db Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Thu, 29 Jun 2023 21:55:43 +0100 Subject: [PATCH] Add extra checking for when an enemy can fire a weapon --- Source/Nakatomi/EnemyCharacter.cpp | 11 +++++++++++ Source/Nakatomi/EnemyCharacter.h | 4 ++++ Source/Nakatomi/TaskFireWeapon.cpp | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Source/Nakatomi/EnemyCharacter.cpp b/Source/Nakatomi/EnemyCharacter.cpp index 2872d6d..e849be4 100644 --- a/Source/Nakatomi/EnemyCharacter.cpp +++ b/Source/Nakatomi/EnemyCharacter.cpp @@ -51,6 +51,17 @@ void AEnemyCharacter::OnFire() // TODO: Play some animation here CurrentWeapon->SetCurrentWeaponStatus(Cooldown); + + GetWorldTimerManager().SetTimer(CooldownTimerHandle, this, &AEnemyCharacter::WeaponCooldownHandler, + CurrentWeapon->GetWeaponProperties()->WeaponCooldown, true); +} + +void AEnemyCharacter::WeaponCooldownHandler() +{ + if (CurrentWeapon->GetCurrentWeaponStatus()->GetValue() != Idle) + { + CurrentWeapon->SetCurrentWeaponStatus(Idle); + } } void AEnemyCharacter::BeginPlay() diff --git a/Source/Nakatomi/EnemyCharacter.h b/Source/Nakatomi/EnemyCharacter.h index 73e088f..3f50307 100644 --- a/Source/Nakatomi/EnemyCharacter.h +++ b/Source/Nakatomi/EnemyCharacter.h @@ -31,6 +31,8 @@ private: UPROPERTY(VisibleAnywhere) URandomWeaponParameters* RandomWeaponParameters; + FTimerHandle CooldownTimerHandle; + public: AEnemyCharacter(); @@ -40,6 +42,8 @@ public: virtual void OnFire() override; + void WeaponCooldownHandler(); + protected: virtual void BeginPlay() override; diff --git a/Source/Nakatomi/TaskFireWeapon.cpp b/Source/Nakatomi/TaskFireWeapon.cpp index 54671f3..01ef159 100644 --- a/Source/Nakatomi/TaskFireWeapon.cpp +++ b/Source/Nakatomi/TaskFireWeapon.cpp @@ -10,7 +10,7 @@ EBTNodeResult::Type UTaskFireWeapon::ExecuteTask(UBehaviorTreeComponent& owner, auto blackboardComponent = owner.GetBlackboardComponent(); auto playerCharacter = Cast(blackboardComponent->GetValueAsObject(TargetActor.SelectedKeyName)); - if (enemyPawn && playerCharacter) + if (enemyPawn && playerCharacter && enemyPawn->GetCurrentWeapon()->GetCurrentWeaponStatus()->GetValue() == Idle) { enemyPawn->OnFire(); return EBTNodeResult::Succeeded;