Advertisement
Guest User

Untitled

a guest
May 26th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.31 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.  
  13. // Sets default values
  14. ASCharacter::ASCharacter()
  15. {
  16.     // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  17.     PrimaryActorTick.bCanEverTick = true;
  18.  
  19.     SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
  20.     SpringArmComp->bEnableCameraRotationLag = true;
  21.     SpringArmComp->bUsePawnControlRotation = false;
  22.     SpringArmComp->SetupAttachment(RootComponent);
  23.  
  24.     MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
  25.     MeshComp->CastShadow = false;
  26.     MeshComp->bOnlyOwnerSee = true;
  27.     MeshComp->AttachToComponent(SpringArmComp, FAttachmentTransformRules::KeepRelativeTransform);
  28.  
  29.     CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
  30.     CameraComp->SetupAttachment(MeshComp, CameraAttachmentSocketName = "camera_mount");
  31.  
  32.     //Default Values for Variables
  33.  
  34.     IsAiming = false;
  35.     WantsToAim = false;
  36.     CanAim = true;
  37.     PickA = true;
  38.  
  39.     //Variables for crouch
  40.  
  41.     IsCrouching = false;
  42.  
  43.     CrouchSpeed = 20.0f;
  44.  
  45.     CrouchedCapsuleHalfHeight = 44.0f;
  46.     DefaultCapsuleHalfHeight = GetCapsuleComponent()->GetScaledCapsuleHalfHeight();
  47.  
  48.     CrouchToggle = false;
  49.  
  50.     //Variables for Sprint
  51.  
  52.     SprintSpeed = 800.0f;
  53.     WalkSpeed = 600.0f;
  54.  
  55.     //Variables for Lean
  56.  
  57.     LeanSpeed = 8.0f;
  58. }
  59.  
  60. // Called when the game starts or when spawned
  61. void ASCharacter::BeginPlay()
  62. {
  63.     Super::BeginPlay();
  64. }
  65.  
  66. void ASCharacter::MoveForward(float Value)
  67. {
  68.     AddMovementInput(GetActorForwardVector() * Value);
  69. }
  70.  
  71. void ASCharacter::MoveRight(float Value)
  72. {
  73.     AddMovementInput(GetActorRightVector() * Value);
  74. }
  75.  
  76.  
  77. void ASCharacter::ExecCrouch()
  78. {
  79.     if (CrouchToggle == false)
  80.     {
  81.         if (IsSprinting == true)
  82.         {
  83.             IsSprinting = false;
  84.         }
  85.         IsCrouching = true;
  86.         CrouchToggle = true;
  87.         GetCharacterMovement()->MaxWalkSpeed = 150.0f;
  88.         CanAim = true;
  89.     }
  90.     else
  91.     {
  92.         IsCrouching = false;
  93.         CrouchToggle = false;
  94.         GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  95.         CanAim = true;
  96.     }
  97.  
  98.     if (Role < ROLE_Authority)
  99.     {
  100.         ServerCrouch();
  101.     }
  102.     else
  103.     {
  104.  
  105.     }
  106. }
  107.  
  108. void ASCharacter::StartADS()
  109. {
  110.     WantsToAim = true;
  111.  
  112.     if (Role < ROLE_Authority)
  113.     {
  114.         ServerWantsToAimTrue();
  115.     }
  116.     else
  117.     {
  118.  
  119.     }
  120. }
  121.  
  122. void ASCharacter::StopADS()
  123. {
  124.     WantsToAim = false;
  125.  
  126.     if (Role < ROLE_Authority)
  127.     {
  128.         ServerWantsToAimFalse();
  129.     }
  130.     else
  131.     {
  132.  
  133.     }
  134. }
  135.  
  136.  
  137. void ASCharacter::Sprint()
  138. {
  139.     if (IsCrouching == true)
  140.     {
  141.         IsCrouching = false;
  142.         CrouchToggle = false;
  143.     }
  144.  
  145.     IsSprinting = true;
  146.     GetCharacterMovement()->MaxWalkSpeed = 600.0f;
  147.     CanAim = false;
  148.  
  149.     if (Role < ROLE_Authority)
  150.     {
  151.         ServerSprintStart();
  152.     }
  153.     else
  154.     {
  155.  
  156.     }
  157. }
  158.  
  159. void ASCharacter::StopSprint()
  160. {
  161.     if (!IsCrouching)
  162.     {
  163.         if (IsCrouching == true)
  164.         {
  165.             CanAim = true;
  166.             GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  167.         }
  168.         else
  169.         {
  170.             IsSprinting = false;
  171.             GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  172.             CanAim = true;
  173.         }
  174.  
  175.         if (Role < ROLE_Authority)
  176.         {
  177.             ServerSprintStop();
  178.         }
  179.         else
  180.         {
  181.  
  182.         }
  183.     }
  184.     else
  185.     {
  186.            
  187.     }
  188.  
  189. }
  190.  
  191. void ASCharacter::SelectFloat()
  192. {
  193.  
  194.  
  195.     if (Role < ROLE_Authority)
  196.     {
  197.         ServerSelectFloat();
  198.     }
  199.     else
  200.     {
  201.         if (PickA == true)
  202.         {
  203.             CalculationValue = GetControlRotation().Pitch;
  204.  
  205.             SelectFloatReturn = 360.0f - CalculationValue;
  206.         }
  207.         if (PickA != true)
  208.         {
  209.             CalculationValue = GetControlRotation().Pitch;
  210.  
  211.             SelectFloatReturn = CalculationValue * -1.0f;
  212.         }
  213.     }
  214. }
  215.  
  216. void ASCharacter::LeanLeft()
  217. {
  218.     if (!IsLeaningLeft)
  219.     {
  220.         LeanAmount = 22.5f;
  221.         IsLeaningLeft = true;
  222.         IsLeaningRight = false;
  223.         GetCharacterMovement()->MaxWalkSpeed = 150.0f;
  224.     }
  225.     else
  226.     {
  227.         LeanAmount = 0.0f;
  228.         IsLeaningLeft = false;
  229.         IsLeaningRight = false;
  230.         if (IsCrouching)
  231.         {
  232.  
  233.         }
  234.         else
  235.         {
  236.             GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  237.         }
  238.     }
  239.  
  240.     if (Role < ROLE_Authority)
  241.     {
  242.         ServerLeanLeft();
  243.     }
  244.     else
  245.     {
  246.  
  247.     }
  248. }
  249.  
  250. void ASCharacter::LeanRight()
  251. {
  252.     if (!IsLeaningRight)
  253.     {
  254.         LeanAmount = -22.5f;
  255.         IsLeaningLeft = false;
  256.         IsLeaningRight = true;
  257.         GetCharacterMovement()->MaxWalkSpeed = 150.0f;
  258.     }
  259.     else
  260.     {
  261.         LeanAmount = 0.0f;
  262.         IsLeaningLeft = false;
  263.         IsLeaningRight = false;
  264.         if (IsCrouching)
  265.         {
  266.  
  267.         }
  268.         else
  269.         {
  270.             GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  271.         }
  272.     }
  273.    
  274.     if (Role < ROLE_Authority)
  275.     {
  276.         ServerLeanRight();
  277.     }
  278.     else
  279.     {
  280.  
  281.     }
  282. }
  283.  
  284. void ASCharacter::SetViewRotation()
  285. {
  286.     if (Role < ROLE_Authority) //Client
  287.     {
  288.         ServerSetViewRotation();
  289.         ViewRotation.Roll = SelectFloatReturn / 3.0f;
  290.     }
  291.     else //Server
  292.     {
  293.         ServerSetViewRotation();
  294.         ViewRotation.Roll = SelectFloatReturn / 3.0f;
  295.     }
  296. }
  297.  
  298. void ASCharacter::DetermineIsA()
  299. {
  300.     if (Role < ROLE_Authority)
  301.     {
  302. //      if (GetControlRotation().Pitch > 180)
  303. //      {
  304. //          PickA = true;
  305. //      }
  306. //      if (GetControlRotation().Pitch < 180)
  307. //      {
  308. //          PickA = false;
  309. //      }
  310.  
  311.         ServerDetermineIsA();
  312.     }
  313.     else
  314.     {
  315.         if (GetControlRotation().Pitch > 180)
  316.         {
  317.             PickA = true;
  318.         }
  319.         if (GetControlRotation().Pitch < 180)
  320.         {
  321.             PickA = false;
  322.         }
  323.     }
  324. }
  325.  
  326. // Called every frame
  327. void ASCharacter::Tick(float DeltaTime)
  328. {
  329.     Super::Tick(DeltaTime);
  330.  
  331.     //Crouching
  332.     float TargetHalfHeight = IsCrouching? CrouchedCapsuleHalfHeight : DefaultCapsuleHalfHeight;
  333.  
  334.     float NewHalfHeight = FMath::FInterpTo(GetCapsuleComponent()->GetScaledCapsuleHalfHeight(), TargetHalfHeight, DeltaTime, CrouchSpeed);
  335.  
  336.     GetCapsuleComponent()->SetCapsuleHalfHeight(NewHalfHeight);
  337.    
  338.     //Leaning Left
  339.     if (Role < ROLE_Authority)
  340.     {
  341.         ServerExecLeanLeft(DeltaTime);
  342.     }
  343.     else
  344.     {
  345.         float TargetLeanAmountLeft = IsLeaningLeft ? 0.0f : LeanAmount;
  346.  
  347.         float NewLeanAmountLeft = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountLeft, DeltaTime, LeanSpeed);
  348.  
  349.         ViewRotation.Pitch = NewLeanAmountLeft;
  350.     }
  351.  
  352.  
  353.  
  354.     //Leaning Right
  355.     if (Role < ROLE_Authority)
  356.     {
  357.         ServerExecLeanRight(DeltaTime);
  358.     }
  359.     else
  360.     {
  361.         float TargetLeanAmountRight = IsLeaningRight ? 0.0f : LeanAmount;
  362.  
  363.         float NewLeanAmountRight = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountRight, DeltaTime, LeanSpeed);
  364.  
  365.         ViewRotation.Pitch = NewLeanAmountRight;
  366.     }
  367.  
  368.  
  369.  
  370.  
  371.  
  372.     //Aiming
  373.     if (WantsToAim == true && CanAim == true)
  374.     {
  375.         if (Role < ROLE_Authority)
  376.         {
  377.             ServerSetAimingTrue();
  378.         }
  379.         else
  380.         {
  381.             IsAiming = true;
  382.         }
  383.     }
  384.     else
  385.     {
  386.         if (Role < ROLE_Authority)
  387.         {
  388.             ServerSetAimingFalse();
  389.         }
  390.         else
  391.         {
  392.             IsAiming = false;
  393.         }
  394.     }
  395.  
  396.  
  397.     //Replicate Look Rotation
  398.  
  399.     DetermineIsA();
  400.     SelectFloat();
  401.     SetViewRotation();
  402.  
  403. }
  404.  
  405. // Called to bind functionality to input
  406. void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  407. {
  408.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  409.  
  410.     PlayerInputComponent->BindAxis("MoveForward", this, &ASCharacter::MoveForward);
  411.     PlayerInputComponent->BindAxis("MoveRight", this, &ASCharacter::MoveRight);
  412.  
  413.     PlayerInputComponent->BindAxis("LookUp", this, &ASCharacter::AddControllerPitchInput);
  414.     PlayerInputComponent->BindAxis("Turn", this, &ASCharacter::AddControllerYawInput);
  415.  
  416.     PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ASCharacter::ExecCrouch);
  417.  
  418.     PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ASCharacter::Jump);
  419.  
  420.     PlayerInputComponent->BindAction("ADS", IE_Pressed, this, &ASCharacter::StartADS);
  421.     PlayerInputComponent->BindAction("ADS", IE_Released, this, &ASCharacter::StopADS);
  422.  
  423.     PlayerInputComponent->BindAction("LeanLeft", IE_Pressed, this, &ASCharacter::LeanLeft);
  424.     PlayerInputComponent->BindAction("LeanRight", IE_Pressed, this, &ASCharacter::LeanRight);
  425.  
  426.     PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &ASCharacter::Sprint);
  427.     PlayerInputComponent->BindAction("Sprint", IE_Released, this, &ASCharacter::StopSprint);
  428. }
  429.  
  430.  
  431. void ASCharacter::ServerDetermineIsA_Implementation()
  432. {
  433.     if (GetControlRotation().Pitch > 180)
  434.     {
  435.         PickA = true;
  436.     }
  437.     if (GetControlRotation().Pitch < 180)
  438.     {
  439.         PickA = false;
  440.     }
  441. }
  442.  
  443. bool ASCharacter::ServerDetermineIsA_Validate()
  444. {
  445.     return true;
  446. }
  447.  
  448. void ASCharacter::ServerExecLeanLeft_Implementation(float DeltaTime)
  449. {
  450.     float TargetLeanAmountLeft = IsLeaningLeft ? 0.0f : LeanAmount;
  451.  
  452.     float NewLeanAmountLeft = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountLeft, DeltaTime, LeanSpeed);
  453.  
  454.     ViewRotation.Pitch = NewLeanAmountLeft;
  455. }
  456.  
  457. bool ASCharacter::ServerExecLeanLeft_Validate(float DeltaTime)
  458. {
  459.     return true;
  460. }
  461.  
  462. void ASCharacter::ServerExecLeanRight_Implementation(float DeltaTime)
  463. {
  464.     float TargetLeanAmountRight = IsLeaningRight ? 0.0f : LeanAmount;
  465.  
  466.     float NewLeanAmountRight = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountRight, DeltaTime, LeanSpeed);
  467.  
  468.     ViewRotation.Pitch = NewLeanAmountRight;
  469. }
  470.  
  471. bool ASCharacter::ServerExecLeanRight_Validate(float DeltaTime)
  472. {
  473.     return true;
  474. }
  475.  
  476. void ASCharacter::ServerLeanLeft_Implementation()
  477. {
  478.     if (!IsLeaningLeft)
  479.     {
  480.         LeanAmount = 22.5f;
  481.         IsLeaningLeft = true;
  482.         IsLeaningRight = false;
  483.         GetCharacterMovement()->MaxWalkSpeed = 150.0f;
  484.     }
  485.     else
  486.     {
  487.         LeanAmount = 0.0f;
  488.         IsLeaningLeft = false;
  489.         IsLeaningRight = false;
  490.         if (IsCrouching)
  491.         {
  492.  
  493.         }
  494.         else
  495.         {
  496.             GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  497.         }
  498.     }
  499. }
  500.  
  501. bool ASCharacter::ServerLeanLeft_Validate()
  502. {
  503.     return true;
  504. }
  505.  
  506. void ASCharacter::ServerLeanRight_Implementation()
  507. {
  508.     if (!IsLeaningRight)
  509.     {
  510.         LeanAmount = -22.5f;
  511.         IsLeaningLeft = false;
  512.         IsLeaningRight = true;
  513.         GetCharacterMovement()->MaxWalkSpeed = 150.0f;
  514.     }
  515.     else
  516.     {
  517.         LeanAmount = 0.0f;
  518.         IsLeaningLeft = false;
  519.         IsLeaningRight = false;
  520.         if (IsCrouching)
  521.         {
  522.  
  523.         }
  524.         else
  525.         {
  526.             GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  527.         }
  528.     }
  529. }
  530.  
  531. bool ASCharacter::ServerLeanRight_Validate()
  532. {
  533.     return true;
  534. }
  535.  
  536. void ASCharacter::ServerReplicateMuzzleLocation_Implementation()
  537. {
  538.     if (!IsLocallyControlled())
  539.     {
  540.         FRotator NewRot = MeshComp->RelativeRotation;
  541.         NewRot.Pitch = RemoteViewPitch * 360.0f / 255.0f;
  542.  
  543.         MeshComp->SetRelativeRotation(NewRot);
  544.     }
  545. }
  546.  
  547. bool ASCharacter::ServerReplicateMuzzleLocation_Validate()
  548. {
  549.     return true;
  550. }
  551.  
  552. void ASCharacter::ServerSetViewRotation_Implementation()
  553. {
  554.     ViewRotation.Roll = SelectFloatReturn / 3.0f;
  555. }
  556.  
  557. bool ASCharacter::ServerSetViewRotation_Validate()
  558. {
  559.     return true;
  560. }
  561.  
  562. void ASCharacter::ServerSelectFloat_Implementation()
  563. {
  564.     if (PickA == true)
  565.     {
  566.         CalculationValue = GetControlRotation().Pitch;
  567.  
  568.         SelectFloatReturn = 360.0f - CalculationValue;
  569.     }
  570.     if (PickA != true)
  571.     {
  572.         CalculationValue = GetControlRotation().Pitch;
  573.  
  574.         SelectFloatReturn = CalculationValue * -1.0f;
  575.     }
  576. }
  577.  
  578. bool ASCharacter::ServerSelectFloat_Validate()
  579. {
  580.     return true;
  581. }
  582.  
  583. void ASCharacter::ServerWantsToAimTrue_Implementation()
  584. {
  585.     WantsToAim = true;
  586. }
  587.  
  588. bool ASCharacter::ServerWantsToAimTrue_Validate()
  589. {
  590.     return true;
  591. }
  592.  
  593.  
  594. void ASCharacter::ServerWantsToAimFalse_Implementation()
  595. {
  596.     WantsToAim = false;
  597. }
  598.  
  599. bool ASCharacter::ServerWantsToAimFalse_Validate()
  600. {
  601.     return true;
  602. }
  603.  
  604. void ASCharacter::ServerSetAimingTrue_Implementation()
  605. {
  606.     IsAiming = true;
  607. }
  608.  
  609. bool ASCharacter::ServerSetAimingTrue_Validate()
  610. {
  611.     return true;
  612. }
  613.  
  614. void ASCharacter::ServerSetAimingFalse_Implementation()
  615. {
  616.     IsAiming = false;
  617. }
  618.  
  619. bool ASCharacter::ServerSetAimingFalse_Validate()
  620. {
  621.     return true;
  622. }
  623.  
  624. void ASCharacter::ServerSprintStop_Implementation()
  625. {
  626.     if (!IsCrouching)
  627.     {
  628.         if (IsCrouching == true)
  629.         {
  630.             CanAim = true;
  631.             GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  632.         }
  633.         else
  634.         {
  635.             IsSprinting = false;
  636.             GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  637.             CanAim = true;
  638.         }
  639.     }
  640.     else
  641.     {
  642.  
  643.     }
  644. }
  645.  
  646. bool ASCharacter::ServerSprintStop_Validate()
  647. {
  648.     return true;
  649. }
  650.  
  651. void ASCharacter::ServerSprintStart_Implementation()
  652. {
  653.     if (IsCrouching == true)
  654.     {
  655.         IsCrouching = false;
  656.         CrouchToggle = false;
  657.     }
  658.  
  659.     IsSprinting = true;
  660.     GetCharacterMovement()->MaxWalkSpeed = 600.0f;
  661.     CanAim = false;
  662. }
  663.  
  664. bool ASCharacter::ServerSprintStart_Validate()
  665. {
  666.     return true;
  667. }
  668.  
  669. void ASCharacter::ServerCrouch_Implementation()
  670. {
  671.     if (CrouchToggle == false)
  672.     {
  673.         if (IsSprinting == true)
  674.         {
  675.             IsSprinting = false;
  676.         }
  677.         IsCrouching = true;
  678.         CrouchToggle = true;
  679.         GetCharacterMovement()->MaxWalkSpeed = 150.0f;
  680.         CanAim = true;
  681.     }
  682.     else
  683.     {
  684.         IsCrouching = false;
  685.         CrouchToggle = false;
  686.         GetCharacterMovement()->MaxWalkSpeed = 400.0f;
  687.         CanAim = true;
  688.     }
  689. }
  690.  
  691. bool ASCharacter::ServerCrouch_Validate()
  692. {
  693.     return true;
  694. }
  695.  
  696. void ASCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  697. {
  698.     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  699.  
  700.     DOREPLIFETIME(ASCharacter, IsCrouching);
  701.     DOREPLIFETIME(ASCharacter, IsSprinting);
  702.     DOREPLIFETIME(ASCharacter, WantsToAim);
  703.     DOREPLIFETIME(ASCharacter, CanAim);
  704.     DOREPLIFETIME(ASCharacter, IsAiming);
  705.     DOREPLIFETIME(ASCharacter, ViewRotation);
  706.     DOREPLIFETIME(ASCharacter, IsLeaningLeft);
  707.     DOREPLIFETIME(ASCharacter, IsLeaningRight);
  708.     DOREPLIFETIME(ASCharacter, LeanAmount);
  709.     DOREPLIFETIME(ASCharacter, PickA);
  710.     DOREPLIFETIME(ASCharacter, SelectFloatReturn);
  711.     DOREPLIFETIME(ASCharacter, CalculationValue);
  712. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement