Advertisement
Guest User

ClientPawn.cpp

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