Add extra weapon firing logic

This commit is contained in:
Louis Hobbs 2023-01-13 21:11:07 +00:00
parent 90fcf30ce5
commit a90c447ce9
2 changed files with 80 additions and 7 deletions

View File

@ -95,7 +95,8 @@ void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCom
if (FireAction)
{
Input->BindAction(FireAction, ETriggerEvent::Started, this, &APlayerCharacter::FireCallback);
Input->BindAction(FireAction, ETriggerEvent::Started, this, &APlayerCharacter::BeginFireCallback);
Input->BindAction(FireAction, ETriggerEvent::Completed, this, &APlayerCharacter::EndFireCallback);
}
if (QuitAction)
@ -143,13 +144,32 @@ void APlayerCharacter::JumpCallback(const FInputActionInstance& Instance)
Jump();
}
void APlayerCharacter::FireCallback(const FInputActionInstance& Instance)
void APlayerCharacter::BeginFireCallback(const FInputActionInstance& Instance)
{
// TODO: Add extra gun fire logic
if (CurrentWeapon == nullptr || CurrentWeapon->GetCurrentWeaponStatus()->GetValue() != WeaponState::Idle)
{
return;
}
// Calculate hits from hitscan
TArray<FHitResult> Hits = TArray<FHitResult>();
CalculateHits(&Hits);
IsFiring = true;
OnFire();
if (CurrentWeapon->GetWeaponProperties()->IsAutomatic)
{
GetWorldTimerManager().SetTimer(CooldownTimerHandle, this, &APlayerCharacter::WeaponCooldownHandler, CurrentWeapon->GetWeaponProperties()->WeaponCooldown, true);
GetWorldTimerManager().SetTimer(FireTimerHandle, this, &APlayerCharacter::OnFire, CurrentWeapon->GetWeaponProperties()->WeaponCooldown, true);
}
else
{
GetWorldTimerManager().SetTimer(CooldownTimerHandle, this, &APlayerCharacter::WeaponCooldownHandler, CurrentWeapon->GetWeaponProperties()->WeaponCooldown, true);
}
}
void APlayerCharacter::EndFireCallback(const FInputActionInstance& Instance)
{
IsFiring = false;
}
void APlayerCharacter::QuitCallback(const FInputActionInstance& Instance)
@ -342,3 +362,42 @@ void APlayerCharacter::RemoveCurrentWeaponFromInventory()
// TODO: Add more checking here
}
void APlayerCharacter::OnFire()
{
if (!IsFiring)
{
return;
}
CurrentWeapon->SetCurrentWeaponStatus(WeaponState::Firing);
TArray<FHitResult> Hits = TArray<FHitResult>();
CalculateHits(&Hits);
// TODO: Decrement ammo count
// TODO: Play sound effect
CurrentWeapon->SetCurrentWeaponStatus(WeaponState::Cooldown);
}
void APlayerCharacter::WeaponCooldownHandler()
{
if (CurrentWeapon->GetCurrentWeaponStatus()->GetValue() != WeaponState::Idle)
{
CurrentWeapon->SetCurrentWeaponStatus(WeaponState::Idle);
}
if (!IsFiring)
{
GetWorldTimerManager().ClearTimer(FireTimerHandle);
GetWorldTimerManager().ClearTimer(CooldownTimerHandle);
}
}
void APlayerCharacter::ClearAllTimers()
{
GetWorldTimerManager().ClearTimer(FireTimerHandle);
GetWorldTimerManager().ClearTimer(CooldownTimerHandle);
}

View File

@ -78,6 +78,12 @@ private:
UPROPERTY()
AWeapon* CurrentWeapon = nullptr;
FTimerHandle FireTimerHandle;
FTimerHandle CooldownTimerHandle;
bool IsFiring = false;
public:
// Sets default values for this character's properties
APlayerCharacter();
@ -99,7 +105,9 @@ public:
void JumpCallback(const FInputActionInstance& Instance);
void FireCallback(const FInputActionInstance& Instance);
void BeginFireCallback(const FInputActionInstance& Instance);
void EndFireCallback(const FInputActionInstance& Instance);
void QuitCallback(const FInputActionInstance& Instance);
@ -130,4 +138,10 @@ public:
void RemoveWeaponFromInventory(int i);
void RemoveCurrentWeaponFromInventory();
void OnFire();
void WeaponCooldownHandler();
void ClearAllTimers();
};