AlphaStarX

Shotgun.cpp

Feb 28th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "Shotgun.h"
  5. #include "Engine/SkeletalMeshSocket.h"
  6. #include "Kismet/GameplayStatics.h"
  7. #include "Kismet/KismetMathLibrary.h"
  8. #include "Particles/ParticleSystemComponent.h"
  9. #include "Sound/SoundCue.h"
  10. #include "ShooterProject/Character/BlasterCharacter.h"
  11. #include "ShooterProject/BlasterComponent/LagCompensationComponent.h"
  12. #include "ShooterProject/PlayerController/BlasterController.h"
  13.  
  14. void AShotgun::FireShotgun(const TArray<FVector_NetQuantize>& HitTargets)
  15. {
  16. AWeapon::Fire(FVector());
  17. APawn* OwnerPawn = Cast<APawn>(GetOwner());
  18. if (OwnerPawn == nullptr) return;
  19. AController* InstigatorController = OwnerPawn->GetController();
  20.  
  21. const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash");
  22. if (MuzzleFlashSocket)
  23. {
  24. const FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
  25. const FVector Start = SocketTransform.GetLocation();
  26.  
  27. // Maps hit character to the number of time hit
  28. TMap<ABlasterCharacter*, uint32> HitMap;
  29. for(FVector_NetQuantize HitTarget : HitTargets)
  30. {
  31. FHitResult FireHit;
  32. WeaponTraceHit(Start, HitTarget, FireHit);
  33.  
  34. ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(FireHit.GetActor());
  35. if (BlasterCharacter)
  36. {
  37. if (HitMap.Contains(BlasterCharacter))
  38. {
  39. HitMap[BlasterCharacter]++;
  40. }
  41. else
  42. {
  43. HitMap.Emplace(BlasterCharacter, 1);
  44. }
  45. if (ImpactParticles)
  46. {
  47. UGameplayStatics::SpawnEmitterAtLocation(
  48. GetWorld(),
  49. ImpactParticles,
  50. FireHit.ImpactPoint,
  51. FireHit.ImpactNormal.Rotation()
  52. );
  53. }
  54. if (HitSound)
  55. {
  56. UGameplayStatics::PlaySoundAtLocation(
  57. this,
  58. HitSound,
  59. FireHit.ImpactPoint,
  60. 0.5f,
  61. FMath::RandRange(-0.5f,0.5f)
  62. );
  63. }
  64. }
  65. }
  66. TArray<ABlasterCharacter*> HitCharacters;
  67. for (auto HitPair : HitMap)
  68. {
  69. if (HitPair.Key && InstigatorController)
  70. {
  71. bool bCauseAuthDamage = !bUseServerSideRewind || OwnerPawn->IsLocallyControlled();
  72. if ( HasAuthority() && bCauseAuthDamage)
  73. {
  74. UGameplayStatics::ApplyDamage(
  75. HitPair.Key, // Character that was hit
  76. Damage * HitPair.Value, // Multiply damage with number of times hit
  77. InstigatorController,
  78. this,
  79. UDamageType::StaticClass()
  80. );
  81. }
  82. HitCharacters.Add(HitPair.Key);
  83. }
  84. }
  85. if (!HasAuthority() && bUseServerSideRewind)
  86. {
  87. BlasterOwnerCharacter = BlasterOwnerCharacter == nullptr ? Cast<ABlasterCharacter>(OwnerPawn) : BlasterOwnerCharacter;
  88. BlasterOwnerController = BlasterOwnerController == nullptr ? Cast<ABlasterController>(InstigatorController) : BlasterOwnerController;
  89. if (BlasterOwnerController && BlasterOwnerCharacter && BlasterOwnerCharacter->GetLagCompensation() && BlasterOwnerCharacter->IsLocallyControlled())
  90. {
  91. BlasterOwnerCharacter->GetLagCompensation()->ShotgunServerScoreRequest(
  92. HitCharacters,
  93. Start,
  94. HitTargets,
  95. BlasterOwnerController->GetServerTime() - BlasterOwnerController->SingleTripTime
  96. );
  97. }
  98. }
  99. }
  100. }
  101.  
  102. void AShotgun::ShotgunTraceEndWithScatter(const FVector& HitTarget, TArray<FVector_NetQuantize>& HitTargets)
  103. {
  104. const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash");
  105. if (MuzzleFlashSocket == nullptr) return;
  106.  
  107. const FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
  108. const FVector TraceStart = SocketTransform.GetLocation();
  109.  
  110. const FVector ToTargetNormalized = (HitTarget - TraceStart).GetSafeNormal();
  111. const FVector SphereCenter = TraceStart + ToTargetNormalized * DistanceToSphere;
  112.  
  113. for (int32 i = 0; i < NumberOfPallets; i++)
  114. {
  115. const FVector RandVec = UKismetMathLibrary::RandomUnitVector() * FMath::FRandRange(0.f, SphereRadius);
  116. const FVector EndLoc = SphereCenter + RandVec;
  117. FVector ToEndLoc = EndLoc - TraceStart;
  118. ToEndLoc = TraceStart + ToEndLoc * TRACE_LENGTH / ToEndLoc.Size();
  119.  
  120. HitTargets.Add(ToEndLoc);
  121. }
  122. }
  123.  
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment