Advertisement
Guest User

ClientPawn.cpp

a guest
Apr 30th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "ClientPawn.h"
  5.  
  6. // Sets default values
  7. AClientPawn::AClientPawn()
  8. {
  9.     // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  10.     PrimaryActorTick.bCanEverTick = true;
  11.  
  12.     mainMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("RootComponent"));
  13.     headMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BodyMeshComponent"));
  14.     cannonMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CannonMeshComponent"));
  15.     springArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
  16.     camera = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
  17.  
  18.     RootComponent = mainMesh;
  19.  
  20.     headMesh->SetupAttachment(RootComponent);
  21.     cannonMesh->SetupAttachment(headMesh);
  22.  
  23.     springArm->SetupAttachment(RootComponent);
  24.     springArm->TargetArmLength = 300.0f;
  25.     springArm->TargetOffset = FVector(0.0f, 0.0f, 50.0f);
  26.     springArm->SocketOffset = FVector(0.0f, 0.0f, 150.0f);
  27.  
  28.     camera->SetupAttachment(springArm, USpringArmComponent::SocketName);
  29.  
  30.     walkSpeed = 100.0f;
  31.     tankFuel = 100.0f;
  32.     isCombatMode = false;
  33.     turnSpeed = 45.0f;
  34.  
  35.     sensitivityX = 100.0f;
  36.     sensitivityY = 100.0f;
  37. }
  38.  
  39. // Called when the game starts or when spawned
  40. void AClientPawn::BeginPlay()
  41. {
  42.     Super::BeginPlay();
  43. }
  44.  
  45. // Called every frame
  46. void AClientPawn::Tick(float DeltaTime)
  47. {
  48.     Super::Tick(DeltaTime);
  49.  
  50.     CameraMovement(sensitivityX, sensitivityY);
  51.  
  52.         PlayerMovement();
  53.         HeadMovement();
  54. }
  55.  
  56. void AClientPawn::MoveRightServer_Implementation(float Value)
  57. {
  58.     moveRight = Value;
  59.    
  60. }
  61. void AClientPawn::MoveForwardServer_Implementation(float Value)
  62. {
  63.     moveForward = Value;
  64. }
  65.  
  66.  
  67. void AClientPawn::CameraMovement(float senX, float senY)
  68. {
  69.     // CAMERA MOVEMENT
  70.     FRotator NewRotation = springArm->GetComponentRotation();
  71.     NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y * GetWorld()->GetDeltaSeconds() * senY, -60.0f, 15.0f);
  72.     NewRotation.Yaw = NewRotation.Yaw + CameraInput.X * GetWorld()->GetDeltaSeconds() * senX;
  73.     NewRotation.Roll = 0.0f;
  74.     springArm->SetWorldRotation(NewRotation);
  75. }
  76.  
  77. void AClientPawn::PlayerMovement()
  78. {
  79.     AddActorWorldRotation(FRotator(0.0f, moveRight * turnSpeed * GetWorld()->GetDeltaSeconds(), 0.0f));
  80.     AddActorWorldOffset(GetActorForwardVector() * moveForward * walkSpeed * GetWorld()->GetDeltaSeconds());
  81. }
  82.  
  83. void AClientPawn::HeadMovement()
  84. {
  85.     FRotator PreviousRotation = headMesh->GetRelativeRotation();
  86.     FRotator NextRotation = FRotator(0.0f, springArm->GetRelativeRotation().Yaw, 0.0f) - FRotator(0.0f, GetActorRotation().Yaw, 0.0f);
  87.     headMesh->SetRelativeRotation(FMath::Lerp(PreviousRotation, NextRotation, GetWorld()->GetDeltaSeconds() * 3.0f));
  88. }
  89.  
  90. void AClientPawn::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  91. {
  92.     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  93.  
  94.     DOREPLIFETIME(AClientPawn ,moveForward);
  95.     DOREPLIFETIME(AClientPawn, moveRight);
  96.     DOREPLIFETIME(AClientPawn, walkSpeed);
  97.     DOREPLIFETIME(AClientPawn, tankFuel);
  98.     DOREPLIFETIME(AClientPawn, isCombatMode);
  99. }
  100.  
  101. // Called to bind functionality to input
  102. void AClientPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  103. {
  104.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  105.  
  106.     PlayerInputComponent->BindAxis(TEXT("MouseX"), this, &AClientPawn::YawCamera);
  107.     PlayerInputComponent->BindAxis(TEXT("MouseY"), this, &AClientPawn::PitchCamera);
  108.     PlayerInputComponent->BindAxis(TEXT("Forward"), this, &AClientPawn::MoveForward);
  109.     PlayerInputComponent->BindAxis(TEXT("Right"), this, &AClientPawn::MoveRight);
  110.  
  111.     PlayerInputComponent->BindAction(TEXT("Test"), IE_Pressed, this, &AClientPawn::TestInput);
  112. }
  113.  
  114. void AClientPawn::PitchCamera(float Value) {
  115.     CameraInput.Y = Value;
  116. }
  117.  
  118.  
  119. void AClientPawn::YawCamera(float Value) {
  120.     CameraInput.X = Value;
  121. }
  122.  
  123. void AClientPawn::MoveForward(float Value)
  124. {
  125.     MoveForwardServer(Value);
  126. }
  127.  
  128. void AClientPawn::MoveRight(float Value)
  129. {
  130.     MoveRightServer(Value);
  131. }
  132.  
  133. void AClientPawn::TestInput()
  134. {
  135.     ChangeWalkSpeed();
  136. }
  137.  
  138. void AClientPawn::ChangeWalkSpeed_Implementation()
  139. {
  140.     walkSpeed = 300.0f;
  141. }
  142.  
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement