N0B0DY1x

PlayerCharacter.cpp - Replicated

Dec 2nd, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "PlayerCharacter.h"
  5. #include "Net/UnrealNetwork.h"
  6. #include "GameFramework/CharacterMovementComponent.h"
  7. #include "GameFramework/SpringArmComponent.h"
  8.  
  9. // Sets default values
  10. APlayerCharacter::APlayerCharacter()
  11. {
  12.     // Configure character movements
  13.     GetCharacterMovement()->bOrientRotationToMovement = false;
  14.     GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f);
  15.     GetCharacterMovement()->JumpZVelocity = 600.0f;
  16.     GetCharacterMovement()->AirControl = 0.75f;
  17.    
  18.     bUseControllerRotationPitch = false;
  19.  
  20.     // FPS Camera
  21.     FPSCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
  22.     FPSCamera->SetupAttachment(RootComponent);
  23.     FPSCamera->SetRelativeLocation(FVector(30.0f, 0.0f, BaseEyeHeight));
  24.     FPSCamera->bUsePawnControlRotation = true;
  25.    
  26.     // Create a camera boom (pulls in towards the player if there is a collision)
  27.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  28.     CameraBoom->SetupAttachment(RootComponent);
  29.     CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character  
  30.     CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
  31.    
  32.     // Create a follow camera
  33.     TPSCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  34.     TPSCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
  35.     // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  36.     TPSCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  37.     TPSCamera->bAutoActivate = false;
  38.  
  39.     // Base configuration for camera view
  40.     bThirdPerson = false;
  41.     bUseControllerRotationYaw = true;
  42.     bUseControllerRotationRoll = true;
  43. }
  44.  
  45. // Called when the game starts or when spawned
  46. void APlayerCharacter::BeginPlay()
  47. {
  48.     Super::BeginPlay();
  49. }
  50.  
  51. void APlayerCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty> & OutLifetimeProps) const {
  52.     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  53.  
  54.     DOREPLIFETIME(APlayerCharacter, bThirdPerson);
  55. }
  56.  
  57. // Called to bind functionality to input
  58. void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  59. {
  60.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  61.  
  62.     // Movements bindings
  63.     PlayerInputComponent->BindAxis("MoveForward", this, &APlayerCharacter::MoveForward);
  64.     PlayerInputComponent->BindAxis("MoveRight", this, &APlayerCharacter::MoveRight);
  65.  
  66.     PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  67.     PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  68.  
  69.     // Look around bindings
  70.     PlayerInputComponent->BindAxis("Turn", this, &APlayerCharacter::AddControllerYawInput);
  71.     PlayerInputComponent->BindAxis("LookUp", this, &APlayerCharacter::AddControllerPitchInput);
  72.  
  73.     // Change player view
  74.     PlayerInputComponent->BindAction("ChangeView", IE_Pressed, this, &APlayerCharacter::ChangeView);
  75. }
  76.  
  77. void APlayerCharacter::MoveForward(const float Value)
  78. {
  79.     if (Controller != nullptr && Value != 0.0f)
  80.     {
  81.         // find out which way is forward
  82.         const FRotator Rotation = Controller->GetControlRotation();
  83.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  84.  
  85.         // get forward vector
  86.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  87.         AddMovementInput(Direction, Value);
  88.     }
  89. }
  90.  
  91. void APlayerCharacter::MoveRight(const float Value)
  92. {
  93.     if (Controller != nullptr && Value != 0.0f)
  94.     {
  95.         // find out which way is right
  96.         const FRotator Rotation = Controller->GetControlRotation();
  97.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  98.  
  99.         // get right vector
  100.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  101.         // add movement in that direction
  102.         AddMovementInput(Direction, Value);
  103.     }
  104. }
  105.  
  106. void APlayerCharacter::StartJump()
  107. {
  108.     bPressedJump = true;
  109. }
  110.  
  111. void APlayerCharacter::StopJump()
  112. {
  113.     bPressedJump = false;
  114. }
  115.  
  116. void APlayerCharacter::ChangeView()
  117. {
  118.     bThirdPerson = !bThirdPerson;
  119.    
  120.     Server_SetThirdParty(bThirdPerson);
  121.     OnRep_ChangeView();
  122. }
  123.  
  124. // Switch from TPS to FPS
  125. void APlayerCharacter::OnRep_ChangeView()
  126. {
  127.     bUseControllerRotationYaw = !bThirdPerson;
  128.     bUseControllerRotationRoll = !bThirdPerson;
  129.     GetCharacterMovement()->bOrientRotationToMovement = bThirdPerson;
  130.    
  131.     if (bThirdPerson)
  132.     {
  133.         FPSCamera->Deactivate();
  134.         TPSCamera->Activate();
  135.     }
  136.     else
  137.     {
  138.         TPSCamera->Deactivate();
  139.         FPSCamera->Activate();
  140.     }
  141. }
  142.  
  143. void APlayerCharacter::Server_SetThirdParty_Implementation(bool IsThirdPerson)
  144. {
  145.     bThirdPerson = IsThirdPerson;
  146.    
  147.     OnRep_ChangeView();
  148. }
  149.  
Add Comment
Please, Sign In to add comment