diff --git a/Source/Blaster/Components/CombatComponent.cpp b/Source/Blaster/Components/CombatComponent.cpp index 599fe22..a078be5 100644 --- a/Source/Blaster/Components/CombatComponent.cpp +++ b/Source/Blaster/Components/CombatComponent.cpp @@ -5,6 +5,7 @@ #include "Blaster/Character/BlasterCharacter.h" #include "Blaster/PlayerController/BlasterPlayerController.h" +#include "Blaster/Weapon/Shotgun.h" #include "Blaster/Weapon/Weapon.h" #include "Camera/CameraComponent.h" #include "Engine/SkeletalMeshSocket.h" @@ -124,8 +125,12 @@ void UCombatComponent::Fire() void UCombatComponent::FireProjectileWeapon() { - LocalFire(HitTarget); - ServerFire(HitTarget); + if (PrimaryWeapon) + { + HitTarget = PrimaryWeapon->bUseScatter ? PrimaryWeapon->TraceEndWithScatter(HitTarget) : HitTarget; + LocalFire(HitTarget); + ServerFire(HitTarget); + } } void UCombatComponent::FireHitScanWeapon() @@ -140,6 +145,11 @@ void UCombatComponent::FireHitScanWeapon() void UCombatComponent::FireShotgun() { + if (AShotgun* Shotgun = Cast(PrimaryWeapon)) + { + TArray HitTargets; + Shotgun->ShotgunTraceEndWithScatter(HitTarget, HitTargets); + } } diff --git a/Source/Blaster/Weapon/Shotgun.cpp b/Source/Blaster/Weapon/Shotgun.cpp index aeac566..f731459 100644 --- a/Source/Blaster/Weapon/Shotgun.cpp +++ b/Source/Blaster/Weapon/Shotgun.cpp @@ -6,6 +6,7 @@ #include "Blaster/Character/BlasterCharacter.h" #include "Engine/SkeletalMeshSocket.h" #include "Kismet/GameplayStatics.h" +#include "Kismet/KismetMathLibrary.h" void AShotgun::Fire(const FVector& HitTarget) { @@ -75,3 +76,26 @@ void AShotgun::Fire(const FVector& HitTarget) } } } + +void AShotgun::ShotgunTraceEndWithScatter(const FVector& HitTarget, TArray& HitTargets) +{ + const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash"); + if (MuzzleFlashSocket == nullptr) return; + + const FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh()); + const FVector TraceStart = SocketTransform.GetLocation(); + + const FVector ToTargetNormalized = (HitTarget - TraceStart).GetSafeNormal(); + const FVector SphereCenter = TraceStart + ToTargetNormalized * DistanceToSphere; + + + for (uint32 i = 0; i < NumberOfPellets; i++) + { + const FVector RandVec = UKismetMathLibrary::RandomUnitVector() * FMath::FRandRange(0.f, SphereRadius); + const FVector EndLoc = SphereCenter + RandVec; + FVector ToEndLoc = EndLoc - TraceStart; + ToEndLoc = FVector(TraceStart + ToEndLoc * TRACE_LENGTH / ToEndLoc.Size()) + + HitTargets.Add(ToEndLoc); + } +} diff --git a/Source/Blaster/Weapon/Shotgun.h b/Source/Blaster/Weapon/Shotgun.h index f4d8701..df5ba56 100644 --- a/Source/Blaster/Weapon/Shotgun.h +++ b/Source/Blaster/Weapon/Shotgun.h @@ -17,6 +17,7 @@ class BLASTER_API AShotgun : public AHitScanWeapon public: virtual void Fire(const FVector& HitTarget) override; + void ShotgunTraceEndWithScatter(const FVector& HitTarget, TArray& HitTargets); private: diff --git a/Source/Blaster/Weapon/Weapon.cpp b/Source/Blaster/Weapon/Weapon.cpp index 1e39f7d..dbe9188 100644 --- a/Source/Blaster/Weapon/Weapon.cpp +++ b/Source/Blaster/Weapon/Weapon.cpp @@ -298,15 +298,15 @@ FVector AWeapon::TraceEndWithScatter(const FVector& HitTarget) { const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash"); if (MuzzleFlashSocket == nullptr) return FVector(); - - FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh()); - FVector TraceStart = SocketTransform.GetLocation(); - - FVector ToTargetNormalized = (HitTarget - TraceStart).GetSafeNormal(); - FVector SphereCenter = TraceStart + ToTargetNormalized * DistanceToSphere; - FVector RandVec = UKismetMathLibrary::RandomUnitVector() * FMath::FRandRange(0.f, SphereRadius); - FVector EndLoc = SphereCenter + RandVec; - FVector ToEndLoc = EndLoc - TraceStart; + + const FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh()); + const FVector TraceStart = SocketTransform.GetLocation(); + + const FVector ToTargetNormalized = (HitTarget - TraceStart).GetSafeNormal(); + const FVector SphereCenter = TraceStart + ToTargetNormalized * DistanceToSphere; + const FVector RandVec = UKismetMathLibrary::RandomUnitVector() * FMath::FRandRange(0.f, SphereRadius); + const FVector EndLoc = SphereCenter + RandVec; + const FVector ToEndLoc = EndLoc - TraceStart; /* DrawDebugSphere(GetWorld(), SphereCenter, SphereRadius, 12, FColor::Red, true); diff --git a/Source/Blaster/Weapon/Weapon.h b/Source/Blaster/Weapon/Weapon.h index 0a22f45..092c66e 100644 --- a/Source/Blaster/Weapon/Weapon.h +++ b/Source/Blaster/Weapon/Weapon.h @@ -116,6 +116,14 @@ protected: int32 OtherBodyIndex ); + // Trace end with scatter + + UPROPERTY(EditAnywhere, Category = "Weapon Scatter") + float DistanceToSphere = 800.f; + + UPROPERTY(EditAnywhere, Category = "Weapon Scatter") + float SphereRadius = 75.f; + private: UPROPERTY(VisibleAnywhere, Category = "Weapon Properties") USkeletalMeshComponent* WeaponMesh; @@ -157,14 +165,6 @@ private: UPROPERTY(EditAnywhere) EWeaponType WeaponType; - - // Trace end with scatter - - UPROPERTY(EditAnywhere, Category = "Weapon Scatter") - float DistanceToSphere = 800.f; - - UPROPERTY(EditAnywhere, Category = "Weapon Scatter") - float SphereRadius = 75.f; public: void SetWeaponState(EWeaponState State);