Added weapon firing speed

This commit is contained in:
TjgL 2025-01-23 23:41:37 +01:00
parent 452789d657
commit d12dbb7c82
5 changed files with 73 additions and 18 deletions

View File

@ -17,6 +17,19 @@ void ALMRevolver::PrimaryFire()
if (!Player) if (!Player)
return; 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); PlaySound(FireSound, true);
PlayAnimation(PrimaryFireAnimation); PlayAnimation(PrimaryFireAnimation);
@ -28,5 +41,6 @@ void ALMRevolver::PrimaryFire()
FLMBulletInfo ShotInfo = FLMBulletInfo(1, Origin, Direction, WeaponSpread, MaxDistance, DamageFalloff, AmmoType, Damage); FLMBulletInfo ShotInfo = FLMBulletInfo(1, Origin, Direction, WeaponSpread, MaxDistance, DamageFalloff, AmmoType, Damage);
Player->FireBullets(ShotInfo); Player->FireBullets(ShotInfo);
GetWorldTimerManager().SetTimer(FireTimer, this, &ALMRevolver::AllowRefire, RefireDelay);
} }

View File

@ -12,22 +12,34 @@ ALMShotgun::ALMShotgun()
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
} }
void ALMShotgun::PrimaryFire() void ALMShotgun::PrimaryFire()
{ {
if (!Player) if (!Player)
return; 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); PlaySound(FireSound, true);
PlayAnimation(PrimaryFireAnimation); PlayAnimation(PrimaryFireAnimation);
FVector Origin = Player->GetWeaponFiringOrigin(); const FVector Origin = Player->GetWeaponFiringOrigin();
FVector Direction = Player->GetAimVector(); const FVector Direction = Player->GetAimVector();
ClipAmmo--; ClipAmmo--;
FLMBulletInfo ShotInfo = FLMBulletInfo(PelletCount, Origin, Direction, WeaponSpread, MaxDistance, DamageFalloff, AmmoType, Damage); FLMBulletInfo ShotInfo = FLMBulletInfo(PelletCount, Origin, Direction, WeaponSpread, MaxDistance, DamageFalloff, AmmoType, Damage);
Player->FireBullets(ShotInfo); Player->FireBullets(ShotInfo);
GetWorldTimerManager().SetTimer(FireTimer, this, &ALMShotgun::AllowRefire, RefireDelay);
} }

View File

@ -40,9 +40,9 @@ void ALMWeaponBase::Setup(USkeletalMeshComponent* Mesh, AActor* CharOwner)
Player = Cast<ALMPlayer>(CharOwner); Player = Cast<ALMPlayer>(CharOwner);
} }
void ALMWeaponBase::Reload() bool ALMWeaponBase::Reload()
{ {
DefaultReload(); return DefaultReload();
} }
void ALMWeaponBase::PrimaryFire() void ALMWeaponBase::PrimaryFire()
@ -69,17 +69,17 @@ void ALMWeaponBase::PlayAnimation(UAnimMontage* Animation)
} }
} }
void ALMWeaponBase::DefaultReload() bool ALMWeaponBase::DefaultReload()
{ {
if (State != EWeaponState::EWS_Idle) if (State != EWeaponState::EWS_Idle)
return; return false;
const int AmmoCount = Player->GetAmmoCount(AmmoType); const int AmmoCount = Player->GetAmmoCount(AmmoType);
if (AmmoCount <= 0) if (AmmoCount <= 0)
return; return false;
if (ClipAmmo >= MaxClip) if (ClipAmmo >= MaxClip)
return; return false;
PlaySound(ReloadSound, true); PlaySound(ReloadSound, true);
PlayAnimation(ReloadAnimation); PlayAnimation(ReloadAnimation);
@ -88,4 +88,16 @@ void ALMWeaponBase::DefaultReload()
// TODO: Use Animations // TODO: Use Animations
OnDefaultReload(); OnDefaultReload();
return true;
}
void ALMWeaponBase::AllowRefire()
{
GetWorldTimerManager().ClearTimer(FireTimer);
State = EWeaponState::EWS_Idle;
if (ClipAmmo <= 0)
{
Reload();
}
} }

View File

@ -119,6 +119,8 @@ int ULMWeaponManager::GetAmmoCount(const EAmmoType AmmoType)
void ULMWeaponManager::Fire() void ULMWeaponManager::Fire()
{ {
ALMWeaponBase* Weapon = GetCurrentWeapon(); ALMWeaponBase* Weapon = GetCurrentWeapon();
if (Weapon->State == EWeaponState::EWS_Idle)
Weapon->PrimaryFire(); Weapon->PrimaryFire();
} }
@ -127,6 +129,8 @@ void ULMWeaponManager::Reload()
GEngine->AddOnScreenDebugMessage(3, 1.f, FColor::Cyan, "Reloading"); GEngine->AddOnScreenDebugMessage(3, 1.f, FColor::Cyan, "Reloading");
ALMWeaponBase* Weapon = GetCurrentWeapon(); ALMWeaponBase* Weapon = GetCurrentWeapon();
if (Weapon->State == EWeaponState::EWS_Idle)
Weapon->Reload(); Weapon->Reload();
} }

View File

@ -23,7 +23,7 @@ public:
public: public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
virtual void Reload(); virtual bool Reload();
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
virtual void PrimaryFire(); virtual void PrimaryFire();
@ -43,10 +43,20 @@ public:
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent) UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
void OnDefaultReload(); void OnDefaultReload();
protected:
void AllowRefire();
private: private:
void DefaultReload(); bool DefaultReload();
public:
/** The current state of the weapon. */
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category="Legumix|Weapon")
EWeaponState State;
protected: /* Weapon Data */ protected: /* Weapon Data */
FTimerHandle FireTimer;
/** The sound to play when firing. */ /** The sound to play when firing. */
UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds") UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds")
TObjectPtr<USoundWave> FireSound; TObjectPtr<USoundWave> FireSound;
@ -54,12 +64,19 @@ protected: /* Weapon Data */
UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds") UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds")
TObjectPtr<USoundWave> ReloadSound; TObjectPtr<USoundWave> ReloadSound;
UPROPERTY(EditAnywhere, BlueprintReadWrite,Category="Legumix|Sounds")
TObjectPtr<USoundWave> DryFireSound;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Legumix|Animations", meta=(AllowPrivateAccess=true)) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Legumix|Animations", meta=(AllowPrivateAccess=true))
TObjectPtr<UAnimMontage> PrimaryFireAnimation; TObjectPtr<UAnimMontage> PrimaryFireAnimation;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Legumix|Animations", meta=(AllowPrivateAccess=true)) UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Legumix|Animations", meta=(AllowPrivateAccess=true))
TObjectPtr<UAnimMontage> ReloadAnimation; TObjectPtr<UAnimMontage> 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. */ /** The flat damage number of one bullet. */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Legumix|Weapon", meta=(AllowPrivateAccess=true)) UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Legumix|Weapon", meta=(AllowPrivateAccess=true))
float Damage = 10.f; float Damage = 10.f;
@ -91,10 +108,6 @@ protected: /* Weapon Data */
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category=Legumix) UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category=Legumix)
TObjectPtr<ALMPlayer> Player; TObjectPtr<ALMPlayer> Player;
/** The current state of the weapon. */
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category="Legumix|Weapon")
EWeaponState State;
private: private:
/** An optional socket to attach the weapon to. */ /** An optional socket to attach the weapon to. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta=(AllowPrivateAccess=true)) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta=(AllowPrivateAccess=true))