Advertisement
Guest User

SWEAPON FILES

a guest
Jun 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.42 KB | None | 0 0
  1. SWeapon.h HEADER FILE BELOW THIS!!
  2.  
  3.  // Fill out your copyright notice in the Description page of Project Settings.
  4.  
  5. #pragma once
  6.  
  7. #include "CoreMinimal.h"
  8. #include "GameFramework/Actor.h"
  9. #include "SWeapon.generated.h"
  10.  
  11. class USkeletalMeshComponent;
  12. class UDamageType;
  13. class UParticleSystem;
  14.  
  15. // Contains information of a single hitscan weapon linetrace.
  16. USTRUCT()
  17. struct FHitScanTrace
  18. {
  19.     GENERATED_BODY()
  20.  
  21. public:
  22.  
  23.     UPROPERTY()
  24.     TEnumAsByte<EPhysicalSurface> SurfaceType;
  25.  
  26.     UPROPERTY()
  27.     FVector_NetQuantize TraceTo;
  28. };
  29.  
  30.  
  31. UCLASS()
  32. class COOPGAME_API ASWeapon : public AActor
  33. {
  34.     GENERATED_BODY()
  35.  
  36. public:
  37.     // Sets default values for this actor's properties
  38.     ASWeapon();
  39.  
  40. protected:
  41.  
  42.     virtual void BeginPlay() override;
  43.  
  44.     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
  45.     USkeletalMeshComponent* MeshComp;
  46.  
  47.     void PlayFireEffects(FVector TraceEnd);
  48.  
  49.     void PlayImpactEffects(EPhysicalSurface SurfaceType, FVector ImpactPoint);
  50.  
  51.     UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  52.     TSubclassOf<UDamageType> DamageType;
  53.  
  54.     UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  55.     FName MuzzleSocketName;
  56.  
  57.     UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  58.     FName TracerTargetName;
  59.  
  60.     UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  61.     UParticleSystem* MuzzleEffect;
  62.  
  63.     UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  64.     UParticleSystem* DefaultImpactEffect;
  65.  
  66.     UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  67.     UParticleSystem* FleshImpactEffect;
  68.  
  69.     UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
  70.     UParticleSystem* TracerEffect;
  71.  
  72.     UPROPERTY(EditDefaultsOnly, Category = "Weapon")
  73.     TSubclassOf<UCameraShake> FireCamShake;
  74.  
  75.     UPROPERTY(EditDefaultsOnly, Category = "Weapon")
  76.     float BaseDamage;
  77.  
  78.     void Fire();
  79.  
  80.     UFUNCTION(Server, Reliable, WithValidation)
  81.     void ServerFire();
  82.  
  83.     FTimerHandle TimerHandle_TimeBetweenShots;
  84.  
  85.     float LastFireTime;
  86.  
  87.     /* RPM - Bullets per minute fired by weapon */
  88.     UPROPERTY(EditDefaultsOnly, Category = "Weapon")
  89.     float RateOfFire;
  90.  
  91.     // Derived from ROF
  92.     float TimeBetweenShots;
  93.  
  94.     UPROPERTY(ReplicatedUsing = OnRep_HitScanTrace)
  95.     FHitScanTrace HitScanTrace;
  96.  
  97.     UFUNCTION()
  98.     void OnRep_HitScanTrace();
  99.  
  100. public:
  101.  
  102.     void StartFire();
  103.  
  104.     void StopFire();
  105.  
  106. };
  107.  
  108. .
  109. .
  110. .
  111. .
  112. .
  113. SWeapon.h STOPS HERE !! SWeapon.cpp IS BELOW HERE !!
  114. .
  115. .
  116. .
  117. .
  118. .
  119.  
  120. // Fill out your copyright notice in the Description page of Project Settings.
  121.  
  122.  
  123. #include "SWeapon.h"
  124. #include "DrawDebugHelpers.h"
  125. #include "Kismet/GameplayStatics.h"
  126. #include "Particles/ParticleSystem.h"
  127. #include "Components/SkeletalMeshComponent.h"
  128. #include "Particles/ParticleSystemComponent.h"
  129. #include "PhysicalMaterials/PhysicalMaterial.h"
  130. #include "CoopGame.h"
  131. #include "TimerManager.h"
  132. #include "Net/UnrealNetwork.h"
  133.  
  134. static int32 DebugWeaponDrawing = 0;
  135. FAutoConsoleVariableRef CVARDebugWeaponDrawing(TEXT("COOP.DebugWeapons"), DebugWeaponDrawing, TEXT("Draw Debug Lines for Weapons"), ECVF_Cheat);
  136.  
  137.  
  138. // Sets default values
  139. ASWeapon::ASWeapon()
  140. {
  141.     MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
  142.     RootComponent = MeshComp;
  143.  
  144.     MuzzleSocketName = "MuzzleSocket";
  145.     TracerTargetName = "Target";
  146.  
  147.     BaseDamage = 20.0f;
  148.  
  149.     RateOfFire = 600;
  150.  
  151.     SetReplicates(true);
  152. }
  153.  
  154.  
  155. void ASWeapon::BeginPlay()
  156. {
  157.     Super::BeginPlay();
  158.  
  159.     TimeBetweenShots = 60 / RateOfFire;
  160. }
  161.  
  162.  
  163. void ASWeapon::Fire()
  164. {
  165.     // Trace the world, from pawn eyes to crosshair location
  166.  
  167.     if (Role < ROLE_Authority)
  168.     {
  169.         ServerFire();
  170.         return;
  171.     }
  172.  
  173.     AActor* MyOwner = GetOwner();
  174.     if (MyOwner)
  175.     {
  176.         FVector EyeLocation;
  177.         FRotator EyeRotation;
  178.         MyOwner->GetActorEyesViewPoint(EyeLocation, EyeRotation);
  179.  
  180.         FVector ShotDirection = EyeRotation.Vector();
  181.         FVector TraceEnd = EyeLocation + (ShotDirection * 10000);
  182.  
  183.         FCollisionQueryParams QueryParams;
  184.         QueryParams.AddIgnoredActor(MyOwner);
  185.         QueryParams.AddIgnoredActor(this);
  186.         QueryParams.bTraceComplex = true;
  187.         QueryParams.bReturnPhysicalMaterial = true;
  188.  
  189.         // Particle "Target" Parameter
  190.         FVector TracerEndPoint = TraceEnd;
  191.  
  192.         EPhysicalSurface SurfaceType = SurfaceType_Default;
  193.  
  194.         FHitResult Hit;
  195.         if (GetWorld()->LineTraceSingleByChannel(Hit, EyeLocation, TraceEnd, COLLISION_WEAPON, QueryParams))
  196.         {
  197.             // Blocking hit! Process damage
  198.  
  199.             AActor * HitActor = Hit.GetActor();
  200.  
  201.             SurfaceType = UPhysicalMaterial::DetermineSurfaceType(Hit.PhysMaterial.Get());
  202.  
  203.             float ActualDamage = BaseDamage;
  204.             if (SurfaceType == SURFACE_FLESHVULNERABLE)
  205.             {
  206.                 ActualDamage *= 4.0f;
  207.             }
  208.  
  209.             UGameplayStatics::ApplyPointDamage(HitActor, ActualDamage, ShotDirection, Hit, MyOwner->GetInstigatorController(), this, DamageType);
  210.  
  211.             PlayImpactEffects(SurfaceType, Hit.ImpactPoint);
  212.  
  213.             TracerEndPoint = Hit.ImpactPoint;
  214.  
  215.         }
  216.  
  217.         if (DebugWeaponDrawing > 0)
  218.         {
  219.             DrawDebugLine(GetWorld(), EyeLocation, TraceEnd, FColor::White, false, 1.0f, 0, 1.0f);
  220.         }
  221.  
  222.         PlayFireEffects(TracerEndPoint);
  223.  
  224.         if (Role == ROLE_Authority)
  225.         {
  226.             HitScanTrace.TraceTo = TracerEndPoint;
  227.             HitScanTrace.SurfaceType = SurfaceType;
  228.         }
  229.  
  230.         LastFireTime = GetWorld()->TimeSeconds;
  231.     }
  232.  
  233. }
  234.  
  235.  
  236. void ASWeapon::ServerFire_Implementation()
  237. {
  238.     Fire();
  239. }
  240.  
  241.  
  242. bool ASWeapon::ServerFire_Validate()
  243. {
  244.     return true;
  245. }
  246.  
  247.  
  248. void ASWeapon::OnRep_HitScanTrace()
  249. {
  250.     // Play cosmetic FX
  251.     PlayFireEffects(HitScanTrace.TraceTo);
  252.  
  253.     PlayImpactEffects(HitScanTrace.SurfaceType, HitScanTrace.TraceTo);
  254. }
  255.  
  256.  
  257. void ASWeapon::StartFire()
  258. {
  259.     float FirstDelay = FMath::Max(LastFireTime + TimeBetweenShots - GetWorld()->TimeSeconds, 0.0f);
  260.  
  261.     GetWorldTimerManager().SetTimer(TimerHandle_TimeBetweenShots, this, &ASWeapon::Fire, TimeBetweenShots, true, FirstDelay);
  262. }
  263.  
  264.  
  265. void ASWeapon::StopFire()
  266. {
  267.     GetWorldTimerManager().ClearTimer(TimerHandle_TimeBetweenShots);
  268. }
  269.  
  270.  
  271. void ASWeapon::PlayFireEffects(FVector TraceEnd)
  272. {
  273.     if (MuzzleEffect)
  274.     {
  275.         UGameplayStatics::SpawnEmitterAttached(MuzzleEffect, MeshComp, MuzzleSocketName);
  276.     }
  277.  
  278.     if (TracerEffect)
  279.     {
  280.         FVector MuzzleLocation = MeshComp->GetSocketLocation(MuzzleSocketName);
  281.  
  282.         UParticleSystemComponent* TracerComp = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), TracerEffect, MuzzleLocation);
  283.         if (TracerComp)
  284.         {
  285.             TracerComp->SetVectorParameter(TracerTargetName, TraceEnd);
  286.         }
  287.  
  288.     }
  289.  
  290.     APawn* MyOwner = Cast<APawn>(GetOwner());
  291.     if (MyOwner)
  292.     {
  293.         APlayerController* PC = Cast<APlayerController>(MyOwner->GetController());
  294.         if (PC)
  295.         {
  296.             PC->ClientPlayCameraShake(FireCamShake);
  297.         }
  298.     }
  299. }
  300.  
  301.  
  302. void ASWeapon::PlayImpactEffects(EPhysicalSurface SurfaceType, FVector ImpactPoint)
  303. {
  304.     UParticleSystem* SelectedEffect = nullptr;
  305.     switch (SurfaceType)
  306.     {
  307.     case SURFACE_FLESHDEFAULT:
  308.     case SURFACE_FLESHVULNERABLE:
  309.         SelectedEffect = FleshImpactEffect;
  310.         break;
  311.     default:
  312.         SelectedEffect = DefaultImpactEffect;
  313.         break;
  314.     }
  315.  
  316.      if (SelectedEffect)
  317.     {
  318.         FVector MuzzleLocation = MeshComp->GetSocketLocation(MuzzleSocketName);
  319.  
  320.         FVector ShotDirection = ImpactPoint - MuzzleLocation;
  321.         ShotDirection.Normalize();
  322.  
  323.         UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), SelectedEffect, ImpactPoint, ShotDirection.Rotation());
  324.     }
  325. }
  326.  
  327.  
  328. void ASWeapon::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  329. {
  330.     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  331.  
  332.     DOREPLIFETIME_CONDITION(ASWeapon, HitScanTrace, COND_SkipOwner);
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement