Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.09 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "SCharacter.h"
  5. #include "Components/CapsuleComponent.h"
  6. #include "Camera/CameraComponent.h"
  7. #include "GameFramework/SpringArmComponent.h"
  8. #include "GameFramework/PawnMovementComponent.h"
  9. #include "Engine/SkeletalMesh.h"
  10. #include "Net/UnrealNetwork.h"
  11. #include "GameFramework/CharacterMovementComponent.h"
  12. #include "SWeaponBase.h"
  13.  
  14. //Done
  15. // Sets default values
  16. ASCharacter::ASCharacter()
  17. {
  18.     // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  19.     PrimaryActorTick.bCanEverTick = true;
  20.  
  21.  
  22.     //Setting variables for the spring arm component
  23.     SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
  24.     SpringArmComp->bUsePawnControlRotation = true;
  25.     SpringArmComp->SetupAttachment(RootComponent);
  26.  
  27.     //Setting the variables for the FPS hands mesh
  28.     MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
  29.     MeshComp->CastShadow = false;
  30.     MeshComp->AttachToComponent(SpringArmComp, FAttachmentTransformRules::KeepRelativeTransform);
  31.  
  32.     //Setup variables for the camera
  33.     CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
  34.     CameraComp->SetupAttachment(MeshComp, "CameraSocket");
  35.  
  36.     //Is not aiming on beginplay
  37.     IsAiming = false;
  38.     //Can aim on beginplay
  39.     CanAim = true;
  40.     //Is not crouching on beginplay
  41.     IsCrouching = false;
  42.  
  43.     //Crouch speed
  44.     CrouchSpeed = 10.0f;
  45.     //Crouch height
  46.     CrouchedCapsuleHalfHeight = 44.0f;
  47.     //Setting the default capsule height
  48.     DefaultCapsuleHalfHeight = GetCapsuleComponent()->GetScaledCapsuleHalfHeight();
  49.  
  50.     //Setting default values for Sprint, walk and crouch speeds
  51.     SprintSpeed = 800.0f;
  52.     WalkSpeed = 600.0f;
  53.     CrouchedSpeed = 150.f;
  54.  
  55.     //Allows the player to swap weapons
  56.     CanWeaponSwap = true;
  57. }
  58.  
  59. //Done
  60. void ASCharacter::PlayFireMontage()
  61. {
  62.     //Selects a random numberr, either 1 or 2
  63.     int Value = FMath::RandRange(1, 2);
  64.  
  65.     //Checks to see if the player is aiming
  66.     if (IsAiming)
  67.     {
  68.         //Checking between the 'Value' value, and playing the respective animation.
  69.         if (Value == 1)
  70.         {
  71.             MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->ADSFireMontage1, 1.0f);
  72.         }
  73.         if (Value == 2)
  74.         {
  75.             MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->ADSFireMontage2, 1.0f);
  76.         }
  77.     }
  78.     else
  79.     {
  80.         //Checking between the 'Value' value, and playing the respective animation.
  81.         if (Value == 1)
  82.         {
  83.             MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->FireMontage1, 1.0f);
  84.         }
  85.         if (Value == 2)
  86.         {
  87.             MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->FireMontage2, 1.0f);
  88.         }
  89.     }
  90.  
  91.     //Adds the recoil for the pitch
  92.     AddControllerPitchInput(FMath::RandRange(CurrentWeapon->PitchMin, CurrentWeapon->PitchMax));
  93.  
  94.     //Adds the recoil for the yaw
  95.     AddControllerYawInput(FMath::RandRange(CurrentWeapon->YawMin, CurrentWeapon->YawMax));
  96. }
  97.  
  98. //Done
  99. // Called when the game starts or when spawned
  100. void ASCharacter::BeginPlay()
  101. {
  102.     Super::BeginPlay();
  103.  
  104.     //Defining the spawn parameters for the weapon
  105.     FActorSpawnParameters SpawnParams;
  106.     SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
  107.  
  108.     //Spawning the weapon
  109.     CurrentWeapon = GetWorld()->SpawnActor<ASWeaponBase>(BasePrimaryWeaponClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
  110.     if (CurrentWeapon)
  111.     {
  112.         //Attaching the weapon to it's socket
  113.         CurrentWeapon->SetOwner(this);
  114.         CurrentWeapon->AttachToComponent(MeshComp, FAttachmentTransformRules::SnapToTargetNotIncludingScale, CurrentWeapon->WeaponAttachmentSocketName);
  115.  
  116.  
  117.         WeaponSelectedInt = CurrentWeapon->WeaponSwapInt;
  118.     }
  119.  
  120.  
  121.     MP40_AmmoArray.Init(32, 8);
  122.     P08_AmmoArray.Init(8, 8);
  123.     K98s_Ammo = 5;
  124.     K98s_TotalAmmo = 25;
  125. }
  126.  
  127. //Done
  128. void ASCharacter::MoveForward(float Value)
  129. {
  130.     //Moving forward/back
  131.     AddMovementInput(GetActorForwardVector() * Value);
  132. }
  133.  
  134. //Done
  135. void ASCharacter::MoveRight(float Value)
  136. {
  137.     //Moving left/right
  138.     AddMovementInput(GetActorRightVector() * Value);
  139. }
  140.  
  141. //Done
  142. void ASCharacter::LookUp(float Value)
  143. {  
  144.     //Adds vertical controller input
  145.     AddControllerPitchInput(Value);
  146. }
  147.  
  148. //Done
  149. void ASCharacter::LookRight(float Value)
  150. {
  151.     //Adds horizontal controller input
  152.     AddControllerYawInput(Value);
  153. }
  154.  
  155. //Done
  156. //Handles executing the crouch function
  157. void ASCharacter::ExecCrouch()
  158. {
  159.     //Checks to see if the player is already crouching
  160.     if (!IsCrouching)
  161.     {
  162.         //If the player is not crouching, checks to see if the player is sprinting
  163.         if (IsSprinting)
  164.         {
  165.             //If the player is sprinting, disables sprinting
  166.             IsSprinting = false;
  167.         }
  168.         //Toggles on crouch
  169.         IsCrouching = true;
  170.         //Sets the max movement speed to crouch
  171.         GetCharacterMovement()->MaxWalkSpeed = CrouchedSpeed;
  172.         //Allows the player to aim
  173.         CanAim = true;
  174.     }
  175.     else
  176.     {
  177.         //Since the player is already crouching, toggles off crouch
  178.         IsCrouching = false;
  179.         //Sets the movement speed to walk
  180.         GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
  181.         //Allows the player to aim
  182.         CanAim = true;
  183.     }
  184. }
  185.  
  186. //Done
  187. void ASCharacter::StartADS()
  188. {
  189.     //Sets wants to aim to true for use in the tick function
  190.     WantsToAim = true;
  191. }
  192.  
  193. //Done
  194. void ASCharacter::StopADS()
  195. {
  196.     //Sets wants to aim for false for use in the tick function
  197.     WantsToAim = false;
  198. }
  199.  
  200. //Done
  201. void ASCharacter::Sprint()
  202. {
  203.     //Checks to see if the character is crouching
  204.     if (IsCrouching == true)
  205.     {
  206.         //If is crouching, sets is crouching to false
  207.         IsCrouching = false;
  208.     }
  209.     //Toggles on sprint
  210.     IsSprinting = true;
  211.     //Sets the movement speed to sprint
  212.     GetCharacterMovement()->MaxWalkSpeed = SprintSpeed;
  213.     //Does not allow the player to aim
  214.     CanAim = false;
  215. }
  216.  
  217. //Done
  218. void ASCharacter::StopSprint()
  219. {
  220.     //Checks to see if the character has crouched
  221.     if (IsCrouching == true)
  222.     {
  223.         //If it is true, nothing happens as all the transitions are handled within the crouch function
  224.     }
  225.     else
  226.     {
  227.         //If the character has just stopped springing, changes variables to fit walk
  228.         //Sets is sprinting to false
  229.         IsSprinting = false;
  230.         //Sets the movement speed to walk
  231.         GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
  232.         //Allows the player to aim
  233.         CanAim = true;
  234.     }
  235. }
  236.  
  237. //Done
  238. void ASCharacter::LeanLeft()
  239. {
  240.     //Checks if the player is already leaning left
  241.     if (!IsLeaningLeft)
  242.     {
  243.         //Sets the lean amount
  244.         LeanAmount = 22.5f;
  245.         //Sets that the player is leaning left
  246.         IsLeaningLeft = true;
  247.         //Sets that the player is not leaning right
  248.         IsLeaningRight = false;
  249.         //Sets the crouched speed of the player
  250.         GetCharacterMovement()->MaxWalkSpeed = CrouchedSpeed;
  251.         //Casts to BP to finish off the lean function
  252.         BPLeanLeft();
  253.  
  254.     }
  255.     //Executes if the player is already leaning left
  256.     else
  257.     {
  258.         //Sets the lean amount to 0 (no lean)
  259.         LeanAmount = 0.0f;
  260.         //Sets that the player is not leaning left
  261.         IsLeaningLeft = false;
  262.         //Sets that the player is not leaning right
  263.         IsLeaningRight = false;
  264.         //Casts to BP to reset the lean
  265.         BPResetLean();
  266.         //Checks if the player is crouching
  267.         if (IsCrouching)
  268.         {
  269.             //If the player is crouching, nothing changes (Crouch speed is the same as lean speed)
  270.         }
  271.         else
  272.         {
  273.             //If the player is not crouching, the speed  is set to walk speed
  274.             GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
  275.         }
  276.     }
  277. }
  278.  
  279. //Done
  280. void ASCharacter::LeanRight()
  281. {
  282.     //Checks if the player is already leaning right
  283.     if (!IsLeaningRight)
  284.     {
  285.         //Sets the lean amount
  286.         LeanAmount = -22.5f;
  287.         //Sets that the player is not leaning left
  288.         IsLeaningLeft = false;
  289.         //Sets that the player is leaning right
  290.         IsLeaningRight = true;
  291.         //Sets the crouched speed of the player
  292.         GetCharacterMovement()->MaxWalkSpeed = CrouchedSpeed;
  293.         //Casts to BP to finish off the lean function
  294.         BPLeanRight();
  295.     }
  296.     //Executes if the player is already leaning right
  297.     else
  298.     {
  299.         //Sets the lean amount to 0 (no lean)
  300.         LeanAmount = 0.0f;
  301.         //Sets that the player is not leaning left
  302.         IsLeaningLeft = false;
  303.         //Sets that the player is not leaning right
  304.         IsLeaningRight = false;
  305.         //Casts to BP to reset the lean
  306.         BPResetLean();
  307.         //Checks if the player is crouching
  308.         if (IsCrouching)
  309.         {
  310.             //If the player is crouching, nothing changes (Crouch speed is the same as lean speed)
  311.         }
  312.         else
  313.         {
  314.             //If the player is not crouching, the speed is set to walk speed
  315.             GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
  316.         }
  317.     }
  318. }
  319.  
  320. //Done
  321. void ASCharacter::StartFire()
  322. {
  323.     //Starts fire on the current weapon
  324.     CurrentWeapon->StartFire();
  325. }
  326.  
  327. //Done
  328. void ASCharacter::StopFire()
  329. {
  330.     //Stops fire on the current weapon
  331.     CurrentWeapon->StopFire();
  332. }
  333.  
  334. //Done
  335. void ASCharacter::Reload()
  336. {
  337.     //Checks to see if the weapon is already reloading
  338.     if (!CurrentWeapon->IsReloading)
  339.     {
  340.         //Executes the reload function of the currently equipped weapon
  341.         CurrentWeapon->Reload();
  342.  
  343.         //Checks if the reloading montage is set
  344.         if (CurrentWeapon->ArmsReloadingMontage && CurrentWeapon->EmptyArmsReloadingMontage)
  345.         {
  346.             //Determines whether the weapon is using clip reloading or not
  347.             if (CurrentWeapon->ClipReloading)
  348.             {
  349.                 //Checks if the weapon needs to be reloaded, or is already fully reloaded
  350.                 if (CurrentWeapon->AmmoTestArray != CurrentWeapon->AmmoArray)
  351.                 {
  352.                     //Checks to see if the current magazine is empty (and thus, the bolt needs to be pulled back to chamber a fresh round)
  353.                     if (CurrentWeapon->AmmoArray[0] <= 0)
  354.                     {
  355.                         //Plays the empty reloading animation
  356.                         AnimTime = MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->EmptyArmsReloadingMontage, 1.0f);
  357.                     }
  358.                     else
  359.                     {
  360.                         //Plays the normal reloading animation
  361.                         AnimTime = MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->ArmsReloadingMontage, 1.0f);
  362.                     }
  363.  
  364.                     //Disables the player's ability to swap weapons
  365.                     CanWeaponSwap = false;
  366.                 }
  367.             }
  368.             else
  369.             {
  370.                 //Checks if the weapon needs to be reloaded, or is already fully reloaded
  371.                 if (CurrentWeapon->CurrentAmmo != CurrentWeapon->StrClipSize && CurrentWeapon->TotalAmmo > 0)
  372.                 {
  373.                     if (CurrentWeapon->CurrentAmmo <= 0)
  374.                     {
  375.                         //Plays the empty reloading animation
  376.                         AnimTime = MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->EmptyArmsReloadingMontage, 1.0f);
  377.                     }
  378.                     else
  379.                     {
  380.                         //Plays the reloading animation
  381.                         AnimTime = MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->ArmsReloadingMontage, 1.0f);
  382.                     }
  383.  
  384.                     //Disables the player's ability to swap weapons
  385.                     CanWeaponSwap = false;
  386.                 }
  387.             }
  388.  
  389.             //allows the player to swap weapons once they've finished reloading
  390.             GetWorldTimerManager().SetTimer(TimerHandle_ReloadAnimTime, this, &ASCharacter::AllowWeaponSwap, AnimTime, false, AnimTime);
  391.         }
  392.     }
  393. }
  394.  
  395.  
  396. void ASCharacter::AllowWeaponSwap()
  397. {
  398.     CanWeaponSwap = true;
  399. }
  400.  
  401. void ASCharacter::SetPrimaryWeapon()
  402. {
  403.     if (CanWeaponSwap)
  404.     {
  405.         //Defining the spawn parameters for the weapon
  406.         FActorSpawnParameters SpawnParams;
  407.         SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
  408.  
  409.         CurrentWeapon->UpdateCharacterAmmo();
  410.  
  411.         CurrentWeapon->K2_DestroyActor();
  412.         CurrentWeapon = GetWorld()->SpawnActor<ASWeaponBase>(BasePrimaryWeaponClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
  413.         if (CurrentWeapon)
  414.         {
  415.             //Attaching the weapon to it's socket
  416.             CurrentWeapon->SetOwner(this);
  417.             CurrentWeapon->AttachToComponent(MeshComp, FAttachmentTransformRules::SnapToTargetNotIncludingScale, CurrentWeapon->WeaponAttachmentSocketName);
  418.         }
  419.  
  420.         WeaponSelectedInt = CurrentWeapon->WeaponSwapInt;
  421.  
  422.         CurrentWeapon->MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->EquipMontage, 1.0f);
  423.  
  424.         CurrentWeapon->UpdateWeaponAmmo();
  425.     }
  426. }
  427.  
  428.  
  429. void ASCharacter::SetSecondaryWeapon()
  430. {
  431.     if (CanWeaponSwap)
  432.     {
  433.         //Defining the spawn parameters for the weapon
  434.         FActorSpawnParameters SpawnParams;
  435.         SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
  436.  
  437.         CurrentWeapon->UpdateCharacterAmmo();
  438.  
  439.         CurrentWeapon->MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->UnequipMontage, 1.0f);
  440.  
  441.         CurrentWeapon->K2_DestroyActor();
  442.         CurrentWeapon = GetWorld()->SpawnActor<ASWeaponBase>(BaseSecondaryWeaponClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
  443.         if (CurrentWeapon)
  444.         {
  445.             //Attaching the weapon to it's socket
  446.             CurrentWeapon->SetOwner(this);
  447.             CurrentWeapon->AttachToComponent(MeshComp, FAttachmentTransformRules::SnapToTargetNotIncludingScale, CurrentWeapon->WeaponAttachmentSocketName);
  448.         }
  449.  
  450.         WeaponSelectedInt = CurrentWeapon->WeaponSwapInt;
  451.  
  452.         CurrentWeapon->MeshComp->GetAnimInstance()->Montage_Play(CurrentWeapon->EquipMontage, 1.0f);
  453.  
  454.         CurrentWeapon->UpdateWeaponAmmo();
  455.     }
  456. }
  457.  
  458. //Done
  459. // Called every frame
  460. void ASCharacter::Tick(float DeltaTime)
  461. {
  462.     Super::Tick(DeltaTime);
  463.  
  464.    
  465.     //Crouching
  466.     //Sets the new Target Half Height based on whether the player is crouching or uncrouching
  467.     float TargetHalfHeight = IsCrouching? CrouchedCapsuleHalfHeight : DefaultCapsuleHalfHeight;
  468.     //Interpolates between the current height and the target height
  469.     float NewHalfHeight = FMath::FInterpTo(GetCapsuleComponent()->GetScaledCapsuleHalfHeight(), TargetHalfHeight, DeltaTime, CrouchSpeed);
  470.     //Sets the half height of the capsule component to the new interpolated half height
  471.     GetCapsuleComponent()->SetCapsuleHalfHeight(NewHalfHeight);
  472.    
  473.    
  474.     float TargetLeanAmountLeft = IsLeaningLeft ? 0.0f : LeanAmount;
  475.  
  476.     float NewLeanAmountLeft = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountLeft, DeltaTime, 8.0f);
  477.  
  478.     ViewRotation.Pitch = NewLeanAmountLeft;
  479.  
  480.  
  481.  
  482.     float TargetLeanAmountRight = IsLeaningRight ? 0.0f : LeanAmount;
  483.  
  484.     float NewLeanAmountRight = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountRight, DeltaTime, 8.0f);
  485.  
  486.     ViewRotation.Pitch = NewLeanAmountRight;
  487.  
  488.  
  489.  
  490.  
  491.     //Aiming
  492.     //Checks if the player wants to aim and is able to aim
  493.     if (WantsToAim == true && CanAim == true)
  494.     {
  495.         //If both conditions are met, the player is allowed to aim down sights (IsAiming is set to true)
  496.         IsAiming = true;
  497.         CrouchedCapsuleHalfHeight = 50.0f;
  498.     }
  499.     else
  500.     {
  501.         //If one or neither condition is met, the player cannot aim down sights
  502.         IsAiming = false;
  503.         CrouchedCapsuleHalfHeight = 44.0f;
  504.     }
  505. }
  506.  
  507. //Done
  508. // Called to bind functionality to input
  509. void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  510. {
  511.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  512.  
  513.     //Move forward/back + left/right inputs
  514.     PlayerInputComponent->BindAxis("MoveForward", this, &ASCharacter::MoveForward);
  515.     PlayerInputComponent->BindAxis("MoveRight", this, &ASCharacter::MoveRight);
  516.  
  517.     //Look up/down + left/right inputs
  518.     PlayerInputComponent->BindAxis("LookUp", this, &ASCharacter::LookUp);
  519.     PlayerInputComponent->BindAxis("Turn", this, &ASCharacter::LookRight);
  520.  
  521.     //Crouch input
  522.     PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ASCharacter::ExecCrouch);
  523.  
  524.     //Jump input
  525.     PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ASCharacter::Jump);
  526.  
  527.     //ADS input (start/stop)
  528.     PlayerInputComponent->BindAction("ADS", IE_Pressed, this, &ASCharacter::StartADS);
  529.     PlayerInputComponent->BindAction("ADS", IE_Released, this, &ASCharacter::StopADS);
  530.  
  531.     //Leaning input (Left + Right)
  532.     PlayerInputComponent->BindAction("LeanLeft", IE_Pressed, this, &ASCharacter::LeanLeft);
  533.     PlayerInputComponent->BindAction("LeanRight", IE_Pressed, this, &ASCharacter::LeanRight);
  534.  
  535.     //Sprint input (start/stop)
  536.     PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &ASCharacter::Sprint);
  537.     PlayerInputComponent->BindAction("Sprint", IE_Released, this, &ASCharacter::StopSprint);
  538.  
  539.     //Fire inout (start/stop)
  540.     PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ASCharacter::StartFire);
  541.     PlayerInputComponent->BindAction("Fire", IE_Released, this, &ASCharacter::StopFire);
  542.  
  543.     //Reload input
  544.     PlayerInputComponent->BindAction("Reload", IE_Pressed, this, &ASCharacter::Reload);
  545.  
  546.     //Weapon swapping
  547.     PlayerInputComponent->BindAction("SelectPrimary", IE_Pressed, this, &ASCharacter::SetPrimaryWeapon);
  548.     PlayerInputComponent->BindAction("SelectSecondary", IE_Pressed, this, &ASCharacter::SetSecondaryWeapon);
  549. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement