daily pastebin goal
48%
SHARE
TWEET

Firearm.cpp

nbarber20 Dec 21st, 2018 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "FireArm.h"
  2. #include <Kismet/GameplayStatics.h>
  3. #include "Animation/AnimInstance.h"
  4. #include "Camera/CameraComponent.h"
  5. #include "Components/CapsuleComponent.h"
  6. #include "Components/InputComponent.h"
  7. #include "GameFramework/InputSettings.h"
  8. #include "HeadMountedDisplayFunctionLibrary.h"
  9. #include "Kismet/GameplayStatics.h"
  10. #include <DrawDebugHelpers.h>
  11. #include <GameFramework/Character.h>
  12. #include "Components/InputComponent.h"
  13. #include "GameFramework/Character.h"
  14. #include "GameFramework/CharacterMovementComponent.h"
  15. #include "TunicsCharacter.h"
  16. #include "Net/UnrealNetwork.h"
  17. #include <Particles/ParticleSystemComponent.h>
  18. #include <Engine/Engine.h>
  19. #include <Components/StaticMeshComponent.h>
  20.  
  21.  
  22.  
  23. // Sets default values
  24. AFireArm::AFireArm()
  25. {
  26.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  27.     PrimaryActorTick.bCanEverTick = true;
  28.  
  29.     RootLocation = CreateDefaultSubobject<USceneComponent>(TEXT("RootLocation"));
  30.     RootLocation->SetupAttachment(RootComponent);
  31.  
  32.     GunOffset = CreateDefaultSubobject<USceneComponent>(TEXT("GunOffset"));
  33.     GunOffset->SetupAttachment(RootLocation);
  34.  
  35.     FP_Gun = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("FP_Gun"));
  36.     FP_Gun->SetupAttachment(GunOffset);
  37.  
  38.     FP_IronSight= CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FP_IronSight"));
  39.     FP_IronSight->SetupAttachment(FP_Gun);
  40.     FP_IronSight->SetCollisionEnabled(ECollisionEnabled::NoCollision);
  41.  
  42.     FP_RedDot = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FP_RedDot"));
  43.     FP_RedDot->SetupAttachment(FP_Gun);
  44.     FP_RedDot->SetCollisionEnabled(ECollisionEnabled::NoCollision);
  45.  
  46.     FP_Scope = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FP_Scope"));
  47.     FP_Scope->SetupAttachment(FP_Gun);
  48.     FP_Scope->SetCollisionEnabled(ECollisionEnabled::NoCollision);
  49.  
  50.     FP_MuzzleLocation = CreateDefaultSubobject<USceneComponent>(TEXT("FP_MuzzleLocation"));
  51.     FP_MuzzleLocation->SetupAttachment(FP_Gun);
  52.     //FP_MuzzleLocation->SetRelativeLocation(FVector(0.2f, 48.4f, -10.6f));
  53.     BulletChambered = true;
  54.     CAmmoInClip = AmmoInClip;
  55.     MuzzleLocation = FP_MuzzleLocation;
  56.     SetReplicates(true);
  57. }
  58.  
  59. // Called when the game starts or when spawned
  60. void AFireArm::BeginPlay()
  61. {
  62.     Super::BeginPlay();
  63.     UpdateAttachment();
  64. }
  65.  
  66. // Called every frame
  67. void AFireArm::Tick(float DeltaTime)
  68. {
  69.     Super::Tick(DeltaTime);
  70.     if (CanPickup) {
  71.         FP_Gun->SetSimulatePhysics(true);
  72.     }
  73.     else {
  74.         FP_Gun->SetSimulatePhysics(false);
  75.         FP_Gun->AttachToComponent(GunOffset, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true));
  76.     }
  77. }
  78.  
  79.  
  80. //called from an anim notify in holding characters abp
  81. void AFireArm::OnFire() {
  82.  
  83.     if (IsFiring == true) {
  84.         if (BulletChambered) {
  85.             if (CAmmoInClip > 0) {
  86.                 CAmmoInClip--;
  87.                 FP_Gun->PlayAnimation(FireAnimation, false);
  88.                 for (int i = 0; i < numberOfBullets; i++) {
  89.                     SpawnBullet();
  90.                 }
  91.                 if (CAmmoInClip == 0) {
  92.                     BulletChambered = false;
  93.                 }
  94.             }
  95.             else {
  96.                 UGameplayStatics::PlaySoundAtLocation(this, NoAmmoSound, GetActorLocation());
  97.                 if (EmptyIdleAnimation) {
  98.                     FP_Gun->PlayAnimation(EmptyIdleAnimation, false);
  99.                 }
  100.             }
  101.         }
  102.         else {
  103.             UGameplayStatics::PlaySoundAtLocation(this, NoAmmoSound, GetActorLocation());
  104.             if (EmptyIdleAnimation) {
  105.                 FP_Gun->PlayAnimation(EmptyIdleAnimation, false);
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111. void AFireArm::OnChambered() {
  112.     if (CanOnlyChamberEmptyChamber) {
  113.         if (!BulletChambered) {
  114.  
  115.             FP_Gun->PlayAnimation(ChamberAnimation, false);
  116.         }
  117.     }
  118.     else {
  119.  
  120.         FP_Gun->PlayAnimation(ChamberAnimation, false);
  121.     }
  122. }
  123. void AFireArm::OnChamberedFinished() {
  124.     //DoubleChambered
  125.     if (BulletChambered) {
  126.         CAmmoInClip--;
  127.     }
  128.     BulletChambered = true;
  129. }
  130.  
  131. void AFireArm::OnReload()
  132. {
  133.     FP_Gun->PlayAnimation(ReloadAnimation, false);
  134.     UGameplayStatics::PlaySoundAtLocation(this, ReloadSound, GetActorLocation());
  135. }
  136.  
  137. void AFireArm::OnReloadFinished()
  138. {
  139.     CAmmoInClip = AmmoInClip;
  140.     BulletChambered = false;
  141. }
  142.  
  143.  
  144.  
  145. void AFireArm::SpawnBullet()
  146. {
  147.  
  148.     if (Role < ROLE_Authority)
  149.     {
  150.         ServerSpawnBullet();
  151.     }
  152.  
  153.     FHitResult hit;
  154.     FVector Offest = FVector(FMath::RandRange(-bulletSpread, bulletSpread), FMath::RandRange(-bulletSpread, bulletSpread), FMath::RandRange(-bulletSpread, bulletSpread));
  155.  
  156.     FVector ShotDir = MuzzleLocation->GetComponentRotation().Vector();
  157.     FVector ShotEnd;
  158.     FVector TraceEnd = MuzzleLocation->GetComponentLocation() + ((ShotDir+ Offest) * 10000);
  159.     FCollisionQueryParams queryparams;
  160.     queryparams.AddIgnoredActor(this);
  161.     queryparams.bTraceComplex = true;
  162.     ShotEnd = TraceEnd;
  163.  
  164.     if (GetWorld()->LineTraceSingleByChannel(hit, MuzzleLocation->GetComponentLocation(), TraceEnd, ECC_Visibility, queryparams)) {
  165.         AActor* HitActor = hit.GetActor();
  166.         UGameplayStatics::ApplyPointDamage(HitActor, 20, ShotDir, hit, GetInstigatorController(), this, DamageType);
  167.         ShotEnd = hit.Location;
  168.     }
  169.     //DrawDebugLine(GetWorld(), MuzzleLocation->GetComponentLocation(), TraceEnd, FColor::White, false, 1.0f, 0, 1.0f);
  170.     if (FireSound != NULL)
  171.     {
  172.         UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
  173.     }
  174.  
  175.    
  176.     UGameplayStatics::SpawnEmitterAttached(MuzzleEffect, FP_Gun, "Muzzle");
  177.     UParticleSystemComponent* tracercomp = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), TracerEffect, FP_Gun->GetSocketLocation("Muzzle"));
  178.     tracercomp->SetVectorParameter("BeamEnd", ShotEnd);
  179.    
  180.     if (Role == ROLE_Authority) {
  181.         HitScanTrace.TraceEnd = ShotEnd;
  182.     }
  183.  
  184. }
  185.  
  186. void AFireArm::OnRep_HitScanTrace()
  187. {
  188.     UGameplayStatics::SpawnEmitterAttached(MuzzleEffect, FP_Gun, "Muzzle");
  189.     UParticleSystemComponent* tracercomp = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), TracerEffect, FP_Gun->GetSocketLocation("Muzzle"));
  190.     tracercomp->SetVectorParameter("BeamEnd", HitScanTrace.TraceEnd);
  191. }
  192. void AFireArm::UpdateAttachment()
  193. {
  194.     FP_IronSight->SetHiddenInGame(true);
  195.     FP_RedDot->SetHiddenInGame(true);
  196.     FP_Scope->SetHiddenInGame(true);
  197.     switch (Attachment)
  198.     {
  199.     case EScopeEnum::SE_IronSight:
  200.         FP_IronSight->SetHiddenInGame(false);
  201.         break;
  202.     case EScopeEnum::SE_RedDot:
  203.         FP_RedDot->SetHiddenInGame(false);
  204.         break;
  205.     case EScopeEnum::SE_Scope:
  206.         FP_Scope->SetHiddenInGame(false);
  207.         break;
  208.     default:
  209.         break;
  210.     }
  211. }
  212.  
  213. void AFireArm::ServerSpawnBullet_Implementation()
  214. {
  215.     //GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Some debug message!"));
  216.     SpawnBullet();
  217. }
  218.  
  219. bool AFireArm::ServerSpawnBullet_Validate()
  220. {
  221.     return true;
  222. }
  223. void AFireArm::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const {
  224.     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  225.     DOREPLIFETIME_CONDITION(AFireArm, HitScanTrace,COND_SkipOwner);
  226. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top