diff --git a/Content/Blueprints/Weapon/BP_AssaultRifle.uasset b/Content/Blueprints/Weapon/BP_AssaultRifle.uasset index 6d0a000..dd3fde4 100644 Binary files a/Content/Blueprints/Weapon/BP_AssaultRifle.uasset and b/Content/Blueprints/Weapon/BP_AssaultRifle.uasset differ diff --git a/Content/Maps/BlasterMap.umap b/Content/Maps/BlasterMap.umap index 659ab9c..fe26872 100644 Binary files a/Content/Maps/BlasterMap.umap and b/Content/Maps/BlasterMap.umap differ diff --git a/Source/Blaster/Components/CombatComponent.cpp b/Source/Blaster/Components/CombatComponent.cpp index ca2039d..729a38f 100644 --- a/Source/Blaster/Components/CombatComponent.cpp +++ b/Source/Blaster/Components/CombatComponent.cpp @@ -173,20 +173,63 @@ void UCombatComponent::OnRep_EquippedWeapon() } } -void UCombatComponent::FireButtonPressed(bool bPressed) +void UCombatComponent::Fire() { - bFireButtonPressed = bPressed; - - if (bFireButtonPressed) + if (bCanFire) { - FHitResult HitResult; - TraceUnderCrosshairs(HitResult); - ServerFire(HitResult.ImpactPoint); + bCanFire = false; + ServerFire(HitTarget); if (EquippedWeapon) { CrosshairShootingFactor = 0.75f; } + + StartFireTimer(); + } + +} + +void UCombatComponent::FireButtonPressed(bool bPressed) +{ + bFireButtonPressed = bPressed; + + if (bFireButtonPressed && EquippedWeapon) + { + Fire(); + } +} + +void UCombatComponent::StartFireTimer() +{ + if (EquippedWeapon == nullptr || Character == nullptr) return; + + Character->GetWorldTimerManager().SetTimer(FireTimer, this, &UCombatComponent::FireTimerFinished, EquippedWeapon->FireDelay); +} + +void UCombatComponent::FireTimerFinished() +{ + if (EquippedWeapon == nullptr) return; + + bCanFire = true; + if (bFireButtonPressed && EquippedWeapon->bAutomatic) + { + Fire(); + } +} + +void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize& TraceHitTarget) +{ + MulticastFire(TraceHitTarget); +} + +void UCombatComponent::MulticastFire_Implementation(const FVector_NetQuantize& TraceHitTarget) +{ + if (EquippedWeapon == nullptr) return; + if (Character) + { + Character->PlayFireMontage(bAiming); + EquippedWeapon->Fire(TraceHitTarget); } } @@ -237,21 +280,6 @@ void UCombatComponent::TraceUnderCrosshairs(FHitResult& TraceHitResult) } } -void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize& TraceHitTarget) -{ - MulticastFire(TraceHitTarget); -} - -void UCombatComponent::MulticastFire_Implementation(const FVector_NetQuantize& TraceHitTarget) -{ - if (EquippedWeapon == nullptr) return; - if (Character) - { - Character->PlayFireMontage(bAiming); - EquippedWeapon->Fire(TraceHitTarget); - } -} - void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip) { if (Character == nullptr || WeaponToEquip == nullptr) return; diff --git a/Source/Blaster/Components/CombatComponent.h b/Source/Blaster/Components/CombatComponent.h index 51a0b12..c2387cc 100644 --- a/Source/Blaster/Components/CombatComponent.h +++ b/Source/Blaster/Components/CombatComponent.h @@ -32,6 +32,7 @@ protected: UFUNCTION() void OnRep_EquippedWeapon(); + void Fire(); void FireButtonPressed(bool bPressed); @@ -87,4 +88,11 @@ private: void InterpFOV(float DeltaTime); FVector HitTarget; + + // Automatic fire + FTimerHandle FireTimer; + bool bCanFire = true; + + void StartFireTimer(); + void FireTimerFinished(); }; diff --git a/Source/Blaster/Weapon/Weapon.h b/Source/Blaster/Weapon/Weapon.h index 5678664..2222918 100644 --- a/Source/Blaster/Weapon/Weapon.h +++ b/Source/Blaster/Weapon/Weapon.h @@ -50,6 +50,14 @@ public: UPROPERTY(EditAnywhere) float ZoomInterpSpeed = 20.f; + + // Automatic fire + + UPROPERTY(EditAnywhere, Category = Combat) + bool bAutomatic = true; + + UPROPERTY(EditAnywhere, Category = Combat) + float FireDelay = .15f; protected: virtual void BeginPlay() override;