Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "ClientPawn.h"
- // Sets default values
- AClientPawn::AClientPawn()
- {
- // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- mainMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("RootComponent"));
- headMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BodyMeshComponent"));
- cannonMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CannonMeshComponent"));
- springArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
- camera = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
- RootComponent = mainMesh;
- headMesh->SetupAttachment(RootComponent);
- cannonMesh->SetupAttachment(headMesh);
- springArm->SetupAttachment(RootComponent);
- springArm->TargetArmLength = 300.0f;
- springArm->TargetOffset = FVector(0.0f, 0.0f, 50.0f);
- springArm->SocketOffset = FVector(0.0f, 0.0f, 150.0f);
- camera->SetupAttachment(springArm, USpringArmComponent::SocketName);
- walkSpeed = 100.0f;
- tankFuel = 100.0f;
- isCombatMode = false;
- turnSpeed = 45.0f;
- sensitivityX = 100.0f;
- sensitivityY = 100.0f;
- }
- // Called when the game starts or when spawned
- void AClientPawn::BeginPlay()
- {
- Super::BeginPlay();
- }
- // Called every frame
- void AClientPawn::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- CameraMovement(sensitivityX, sensitivityY);
- PlayerMovement();
- HeadMovement();
- }
- void AClientPawn::MoveRightServer_Implementation(float Value)
- {
- moveRight = Value;
- }
- void AClientPawn::MoveForwardServer_Implementation(float Value)
- {
- moveForward = Value;
- }
- void AClientPawn::CameraMovement(float senX, float senY)
- {
- // CAMERA MOVEMENT
- FRotator NewRotation = springArm->GetComponentRotation();
- NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y * GetWorld()->GetDeltaSeconds() * senY, -60.0f, 15.0f);
- NewRotation.Yaw = NewRotation.Yaw + CameraInput.X * GetWorld()->GetDeltaSeconds() * senX;
- NewRotation.Roll = 0.0f;
- springArm->SetWorldRotation(NewRotation);
- }
- void AClientPawn::PlayerMovement()
- {
- AddActorWorldRotation(FRotator(0.0f, moveRight * turnSpeed * GetWorld()->GetDeltaSeconds(), 0.0f));
- AddActorWorldOffset(GetActorForwardVector() * moveForward * walkSpeed * GetWorld()->GetDeltaSeconds());
- }
- void AClientPawn::HeadMovement()
- {
- FRotator PreviousRotation = headMesh->GetRelativeRotation();
- FRotator NextRotation = FRotator(0.0f, springArm->GetRelativeRotation().Yaw, 0.0f) - FRotator(0.0f, GetActorRotation().Yaw, 0.0f);
- headMesh->SetRelativeRotation(FMath::Lerp(PreviousRotation, NextRotation, GetWorld()->GetDeltaSeconds() * 3.0f));
- }
- void AClientPawn::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
- {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME(AClientPawn ,moveForward);
- DOREPLIFETIME(AClientPawn, moveRight);
- DOREPLIFETIME(AClientPawn, walkSpeed);
- DOREPLIFETIME(AClientPawn, tankFuel);
- DOREPLIFETIME(AClientPawn, isCombatMode);
- }
- // Called to bind functionality to input
- void AClientPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- PlayerInputComponent->BindAxis(TEXT("MouseX"), this, &AClientPawn::YawCamera);
- PlayerInputComponent->BindAxis(TEXT("MouseY"), this, &AClientPawn::PitchCamera);
- PlayerInputComponent->BindAxis(TEXT("Forward"), this, &AClientPawn::MoveForward);
- PlayerInputComponent->BindAxis(TEXT("Right"), this, &AClientPawn::MoveRight);
- PlayerInputComponent->BindAction(TEXT("Test"), IE_Pressed, this, &AClientPawn::TestInput);
- }
- void AClientPawn::PitchCamera(float Value) {
- CameraInput.Y = Value;
- }
- void AClientPawn::YawCamera(float Value) {
- CameraInput.X = Value;
- }
- void AClientPawn::MoveForward(float Value)
- {
- MoveForwardServer(Value);
- }
- void AClientPawn::MoveRight(float Value)
- {
- MoveRightServer(Value);
- }
- void AClientPawn::TestInput()
- {
- ChangeWalkSpeed();
- }
- void AClientPawn::ChangeWalkSpeed_Implementation()
- {
- walkSpeed = 300.0f;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement