From d12dbb7c821867750707a4178a2fede2c1d21a3d Mon Sep 17 00:00:00 2001 From: TjgL Date: Thu, 23 Jan 2025 23:41:37 +0100 Subject: [PATCH] Added weapon firing speed --- .../LegumeMix/Private/Weapon/LMRevolver.cpp | 14 +++++++++++ Source/LegumeMix/Private/Weapon/LMShotgun.cpp | 20 ++++++++++++--- .../LegumeMix/Private/Weapon/LMWeaponBase.cpp | 24 +++++++++++++----- .../Private/Weapon/LMWeaponManager.cpp | 8 ++++-- Source/LegumeMix/Public/Weapon/LMWeaponBase.h | 25 ++++++++++++++----- 5 files changed, 73 insertions(+), 18 deletions(-) diff --git a/Source/LegumeMix/Private/Weapon/LMRevolver.cpp b/Source/LegumeMix/Private/Weapon/LMRevolver.cpp index 9e8027f..f7d379a 100644 --- a/Source/LegumeMix/Private/Weapon/LMRevolver.cpp +++ b/Source/LegumeMix/Private/Weapon/LMRevolver.cpp @@ -16,7 +16,20 @@ void ALMRevolver::PrimaryFire() { if (!Player) return; + + State = EWeaponState::EWS_Firing; + if (ClipAmmo <= 0) + { + if (!Reload()) + { + PlaySound(DryFireSound, true); + GetWorldTimerManager().SetTimer(FireTimer, this, &ALMRevolver::AllowRefire, RefireDelay / 2); + } + return; + } + + PlaySound(FireSound, true); PlayAnimation(PrimaryFireAnimation); @@ -28,5 +41,6 @@ void ALMRevolver::PrimaryFire() FLMBulletInfo ShotInfo = FLMBulletInfo(1, Origin, Direction, WeaponSpread, MaxDistance, DamageFalloff, AmmoType, Damage); Player->FireBullets(ShotInfo); + GetWorldTimerManager().SetTimer(FireTimer, this, &ALMRevolver::AllowRefire, RefireDelay); } diff --git a/Source/LegumeMix/Private/Weapon/LMShotgun.cpp b/Source/LegumeMix/Private/Weapon/LMShotgun.cpp index 85a9a6c..d6557ff 100644 --- a/Source/LegumeMix/Private/Weapon/LMShotgun.cpp +++ b/Source/LegumeMix/Private/Weapon/LMShotgun.cpp @@ -12,22 +12,34 @@ ALMShotgun::ALMShotgun() PrimaryActorTick.bCanEverTick = true; } - void ALMShotgun::PrimaryFire() { if (!Player) return; - + + State = EWeaponState::EWS_Firing; + + if (ClipAmmo <= 0) + { + if (!Reload()) + { + PlaySound(DryFireSound, true); + GetWorldTimerManager().SetTimer(FireTimer, this, &ALMShotgun::AllowRefire, RefireDelay / 2); + } + return; + } + PlaySound(FireSound, true); PlayAnimation(PrimaryFireAnimation); - FVector Origin = Player->GetWeaponFiringOrigin(); - FVector Direction = Player->GetAimVector(); + const FVector Origin = Player->GetWeaponFiringOrigin(); + const FVector Direction = Player->GetAimVector(); ClipAmmo--; FLMBulletInfo ShotInfo = FLMBulletInfo(PelletCount, Origin, Direction, WeaponSpread, MaxDistance, DamageFalloff, AmmoType, Damage); Player->FireBullets(ShotInfo); + GetWorldTimerManager().SetTimer(FireTimer, this, &ALMShotgun::AllowRefire, RefireDelay); } diff --git a/Source/LegumeMix/Private/Weapon/LMWeaponBase.cpp b/Source/LegumeMix/Private/Weapon/LMWeaponBase.cpp index 7229ad0..f07d3ef 100644 --- a/Source/LegumeMix/Private/Weapon/LMWeaponBase.cpp +++ b/Source/LegumeMix/Private/Weapon/LMWeaponBase.cpp @@ -40,9 +40,9 @@ void ALMWeaponBase::Setup(USkeletalMeshComponent* Mesh, AActor* CharOwner) Player = Cast(CharOwner); } -void ALMWeaponBase::Reload() +bool ALMWeaponBase::Reload() { - DefaultReload(); + return DefaultReload(); } void ALMWeaponBase::PrimaryFire() @@ -69,17 +69,17 @@ void ALMWeaponBase::PlayAnimation(UAnimMontage* Animation) } } -void ALMWeaponBase::DefaultReload() +bool ALMWeaponBase::DefaultReload() { if (State != EWeaponState::EWS_Idle) - return; + return false; const int AmmoCount = Player->GetAmmoCount(AmmoType); if (AmmoCount <= 0) - return; + return false; if (ClipAmmo >= MaxClip) - return; + return false; PlaySound(ReloadSound, true); PlayAnimation(ReloadAnimation); @@ -88,4 +88,16 @@ void ALMWeaponBase::DefaultReload() // TODO: Use Animations OnDefaultReload(); + return true; +} + +void ALMWeaponBase::AllowRefire() +{ + GetWorldTimerManager().ClearTimer(FireTimer); + State = EWeaponState::EWS_Idle; + + if (ClipAmmo <= 0) + { + Reload(); + } } diff --git a/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp b/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp index bc972f8..20e6b96 100644 --- a/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp +++ b/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp @@ -119,7 +119,9 @@ int ULMWeaponManager::GetAmmoCount(const EAmmoType AmmoType) void ULMWeaponManager::Fire() { ALMWeaponBase* Weapon = GetCurrentWeapon(); - Weapon->PrimaryFire(); + + if (Weapon->State == EWeaponState::EWS_Idle) + Weapon->PrimaryFire(); } void ULMWeaponManager::Reload() @@ -127,7 +129,9 @@ void ULMWeaponManager::Reload() GEngine->AddOnScreenDebugMessage(3, 1.f, FColor::Cyan, "Reloading"); ALMWeaponBase* Weapon = GetCurrentWeapon(); - Weapon->Reload(); + + if (Weapon->State == EWeaponState::EWS_Idle) + Weapon->Reload(); } void ULMWeaponManager::SwitchWeapon(const int Direction) diff --git a/Source/LegumeMix/Public/Weapon/LMWeaponBase.h b/Source/LegumeMix/Public/Weapon/LMWeaponBase.h index 4d1b0ca..ca0da1e 100644 --- a/Source/LegumeMix/Public/Weapon/LMWeaponBase.h +++ b/Source/LegumeMix/Public/Weapon/LMWeaponBase.h @@ -23,7 +23,7 @@ public: public: UFUNCTION(BlueprintCallable) - virtual void Reload(); + virtual bool Reload(); UFUNCTION(BlueprintCallable) virtual void PrimaryFire(); @@ -43,16 +43,29 @@ public: UFUNCTION(BlueprintCallable, BlueprintImplementableEvent) void OnDefaultReload(); +protected: + void AllowRefire(); + private: - void DefaultReload(); + bool DefaultReload(); +public: + /** The current state of the weapon. */ + UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category="Legumix|Weapon") + EWeaponState State; + protected: /* Weapon Data */ + FTimerHandle FireTimer; + /** The sound to play when firing. */ UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds") TObjectPtr FireSound; UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds") TObjectPtr ReloadSound; + + UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds") + TObjectPtr DryFireSound; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Legumix|Animations", meta=(AllowPrivateAccess=true)) TObjectPtr PrimaryFireAnimation; @@ -60,6 +73,10 @@ protected: /* Weapon Data */ UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Legumix|Animations", meta=(AllowPrivateAccess=true)) TObjectPtr ReloadAnimation; + /** The number of seconds before being able to fire again. */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Legumix|Weapon", meta=(AllowPrivateAccess=true)) + float RefireDelay = 0.5f; + /** The flat damage number of one bullet. */ UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Legumix|Weapon", meta=(AllowPrivateAccess=true)) float Damage = 10.f; @@ -90,10 +107,6 @@ protected: /* Weapon Data */ UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category=Legumix) TObjectPtr Player; - - /** The current state of the weapon. */ - UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category="Legumix|Weapon") - EWeaponState State; private: /** An optional socket to attach the weapon to. */