From d2221d233426b9b0a3ea333334c7cdae5f8b158d Mon Sep 17 00:00:00 2001 From: charnet3d Date: Sat, 13 Jan 2024 20:37:03 +0100 Subject: [PATCH] Section 11: Multiple Weapon Types - Lecture 213 --- .../Gunshots/AR15_Generic_Shot_Cue.uasset | 3 +++ .../_Game/DataTable/WeaponDataTable.uasset | 4 +-- Source/Shooter/ShooterCharacter.cpp | 15 ++++++----- Source/Shooter/ShooterCharacter.h | 11 -------- Source/Shooter/Weapon.cpp | 4 +++ Source/Shooter/Weapon.h | 26 +++++++++++++++++++ 6 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 Content/_Game/Assets/Sounds/Gunshots/AR15_Generic_Shot_Cue.uasset diff --git a/Content/_Game/Assets/Sounds/Gunshots/AR15_Generic_Shot_Cue.uasset b/Content/_Game/Assets/Sounds/Gunshots/AR15_Generic_Shot_Cue.uasset new file mode 100644 index 00000000..a44b4502 --- /dev/null +++ b/Content/_Game/Assets/Sounds/Gunshots/AR15_Generic_Shot_Cue.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f7d6c17982d7163a6f71ea27449ca042c68363482282dd30f164d966ade65fd4 +size 4249 diff --git a/Content/_Game/DataTable/WeaponDataTable.uasset b/Content/_Game/DataTable/WeaponDataTable.uasset index 4ab91af7..090201b0 100644 --- a/Content/_Game/DataTable/WeaponDataTable.uasset +++ b/Content/_Game/DataTable/WeaponDataTable.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4c3f8c4cf28e5231d6f6efc400d5eb4137d21a25bb87b6f34bc736aee50f3f2f -size 7728 +oid sha256:21e619360fc9404bc63d13e169ffe6ca5f13f99bb788d04efb0aeecc910eb1bf +size 8502 diff --git a/Source/Shooter/ShooterCharacter.cpp b/Source/Shooter/ShooterCharacter.cpp index c6ef5983..60c80fa4 100644 --- a/Source/Shooter/ShooterCharacter.cpp +++ b/Source/Shooter/ShooterCharacter.cpp @@ -52,7 +52,6 @@ AShooterCharacter::AShooterCharacter() : // Automatic fire bFireButtonPressed(false), bShouldFire(true), - AutomaticFireRate(0.1f), // Item trace variables bShouldTraceForItems(false), // Camera interp location variables @@ -577,9 +576,11 @@ void AShooterCharacter::FireWeapon() void AShooterCharacter::StartFireTimer() { + if (!EquippedWeapon) return; + CombatState = ECombatState::ECS_FireTimerInProgress; GetWorldTimerManager().SetTimer(AutoFireTimer, this, &AShooterCharacter::AutoFireReset, - AutomaticFireRate); + EquippedWeapon->GetAutoFireRate()); } void AShooterCharacter::AutoFireReset() @@ -601,9 +602,10 @@ void AShooterCharacter::AutoFireReset() void AShooterCharacter::PlayFireSound() { // Play fire sound - if (FireSound) + if (!EquippedWeapon) return; + if (EquippedWeapon->GetFireSound()) { - UGameplayStatics::PlaySound2D(this, FireSound); + UGameplayStatics::PlaySound2D(this, EquippedWeapon->GetFireSound()); } } @@ -615,9 +617,10 @@ void AShooterCharacter::SendBullet() const FTransform SocketTransform = BarrelSocket->GetSocketTransform(EquippedWeapon->GetItemMesh()); - if (MuzzleFlash) + if (!EquippedWeapon) return; + if (EquippedWeapon->GetMuzzleFlash()) { - UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), MuzzleFlash, SocketTransform); + UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), EquippedWeapon->GetMuzzleFlash(), SocketTransform); } FVector BeamEnd; diff --git a/Source/Shooter/ShooterCharacter.h b/Source/Shooter/ShooterCharacter.h index 877789b4..63e61350 100644 --- a/Source/Shooter/ShooterCharacter.h +++ b/Source/Shooter/ShooterCharacter.h @@ -267,14 +267,6 @@ private: meta = (ClampMin = 0.0, ClampMax = 1.0, UIMin = 0.0, UIMax = 1.0)) float MouseAimingLookUpRate; - /* Randomized gunshot sound cue*/ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true)) - USoundCue* FireSound; - - /* Flash spawned at BarrelSocket */ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true)) - UParticleSystem* MuzzleFlash; - /* Montage for firing the weapon */ UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Combat, meta = (AllowPrivateAccess = true)) UAnimMontage* HipFireMontage; @@ -336,9 +328,6 @@ private: /* True when we cane fire. False when waiting for the timer */ bool bShouldFire; - /* Rate of automatic gun fire */ - float AutomaticFireRate; - /* Sets a timer between gunshots */ FTimerHandle AutoFireTimer; diff --git a/Source/Shooter/Weapon.cpp b/Source/Shooter/Weapon.cpp index 956fe94e..df9cb37c 100644 --- a/Source/Shooter/Weapon.cpp +++ b/Source/Shooter/Weapon.cpp @@ -126,6 +126,10 @@ void AWeapon::OnConstruction(const FTransform& Transform) CrosshairsRight = WeaponDataRow->CrosshairsRight; CrosshairsBottom = WeaponDataRow->CrosshairsBottom; CrosshairsTop = WeaponDataRow->CrosshairsTop; + + AutoFireRate = WeaponDataRow->AutoFireRate; + MuzzleFlash = WeaponDataRow->MuzzleFlash; + FireSound = WeaponDataRow->FireSound; } if (GetMaterialInstance()) diff --git a/Source/Shooter/Weapon.h b/Source/Shooter/Weapon.h index fdc363f0..67ab521a 100644 --- a/Source/Shooter/Weapon.h +++ b/Source/Shooter/Weapon.h @@ -12,6 +12,7 @@ class USoundCue; class UWidgetComponent; +class UParticleSystem; USTRUCT(BlueprintType) struct FWeaponDataTable : public FTableRowBase @@ -74,6 +75,15 @@ struct FWeaponDataTable : public FTableRowBase UPROPERTY(EditAnywhere, BlueprintReadWrite) UTexture2D* CrosshairsTop; + + UPROPERTY(EditAnywhere, BlueprintReadWrite) + float AutoFireRate; + + UPROPERTY(EditAnywhere, BlueprintReadWrite) + UParticleSystem* MuzzleFlash; + + UPROPERTY(EditAnywhere, BlueprintReadWrite) + USoundCue* FireSound; }; /** @@ -148,6 +158,19 @@ private: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = DataTable, meta = (AllowPrivateAccess = "true")) UTexture2D* CrosshairsTop; + + /** The speed at which automatic fire happens */ + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = DataTable, meta = (AllowPrivateAccess = "true")) + float AutoFireRate; + + /** Particle system spawned at the BarrelSocket */ + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = DataTable, meta = (AllowPrivateAccess = "true")) + UParticleSystem* MuzzleFlash; + + /** Sound played when the weapon is fired */ + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = DataTable, meta = (AllowPrivateAccess = "true")) + USoundCue* FireSound; + public: /** Adds an impulse to the weapon */ void ThrowWeapon(); @@ -162,6 +185,9 @@ public: FORCEINLINE int32 GetMagazineCapacity() const { return MagazineCapacity; } FORCEINLINE FName GetReloadMontageSection() const { return ReloadMontageSection; } FORCEINLINE FName GetClipBoneName() const { return ClipBoneName; } + FORCEINLINE float GetAutoFireRate() const { return AutoFireRate; } + FORCEINLINE UParticleSystem* GetMuzzleFlash() const { return MuzzleFlash; } + FORCEINLINE USoundCue* GetFireSound() const { return FireSound; } void ReloadAmmo(int32 Amount);