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)
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);
}

View File

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

View File

@ -40,9 +40,9 @@ void ALMWeaponBase::Setup(USkeletalMeshComponent* Mesh, AActor* CharOwner)
Player = Cast<ALMPlayer>(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();
}
}

View File

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

View File

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