Add extra checking for when an enemy can fire a weapon

This commit is contained in:
Louis Hobbs 2023-06-29 21:55:43 +01:00
parent 9d32a0a0e3
commit 4ab591e4b9
3 changed files with 16 additions and 1 deletions

View File

@ -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()

View File

@ -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;

View File

@ -10,7 +10,7 @@ EBTNodeResult::Type UTaskFireWeapon::ExecuteTask(UBehaviorTreeComponent& owner,
auto blackboardComponent = owner.GetBlackboardComponent();
auto playerCharacter = Cast<APlayerCharacter>(blackboardComponent->GetValueAsObject(TargetActor.SelectedKeyName));
if (enemyPawn && playerCharacter)
if (enemyPawn && playerCharacter && enemyPawn->GetCurrentWeapon()->GetCurrentWeaponStatus()->GetValue() == Idle)
{
enemyPawn->OnFire();
return EBTNodeResult::Succeeded;