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 "SCharacter.h"
- #include "Components/CapsuleComponent.h"
- #include "Camera/CameraComponent.h"
- #include "GameFramework/SpringArmComponent.h"
- #include "GameFramework/PawnMovementComponent.h"
- #include "Engine/SkeletalMesh.h"
- #include "Net/UnrealNetwork.h"
- #include "GameFramework/CharacterMovementComponent.h"
- // Sets default values
- ASCharacter::ASCharacter()
- {
- // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
- SpringArmComp->bEnableCameraRotationLag = true;
- SpringArmComp->bUsePawnControlRotation = false;
- SpringArmComp->SetupAttachment(RootComponent);
- MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
- MeshComp->CastShadow = false;
- MeshComp->bOnlyOwnerSee = true;
- MeshComp->AttachToComponent(SpringArmComp, FAttachmentTransformRules::KeepRelativeTransform);
- CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
- CameraComp->SetupAttachment(MeshComp, CameraAttachmentSocketName = "camera_mount");
- //Default Values for Variables
- IsAiming = false;
- WantsToAim = false;
- CanAim = true;
- PickA = true;
- //Variables for crouch
- IsCrouching = false;
- CrouchSpeed = 20.0f;
- CrouchedCapsuleHalfHeight = 44.0f;
- DefaultCapsuleHalfHeight = GetCapsuleComponent()->GetScaledCapsuleHalfHeight();
- CrouchToggle = false;
- //Variables for Sprint
- SprintSpeed = 800.0f;
- WalkSpeed = 600.0f;
- //Variables for Lean
- LeanSpeed = 8.0f;
- }
- // Called when the game starts or when spawned
- void ASCharacter::BeginPlay()
- {
- Super::BeginPlay();
- }
- void ASCharacter::MoveForward(float Value)
- {
- AddMovementInput(GetActorForwardVector() * Value);
- }
- void ASCharacter::MoveRight(float Value)
- {
- AddMovementInput(GetActorRightVector() * Value);
- }
- void ASCharacter::ExecCrouch()
- {
- if (CrouchToggle == false)
- {
- if (IsSprinting == true)
- {
- IsSprinting = false;
- }
- IsCrouching = true;
- CrouchToggle = true;
- GetCharacterMovement()->MaxWalkSpeed = 150.0f;
- CanAim = true;
- }
- else
- {
- IsCrouching = false;
- CrouchToggle = false;
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- CanAim = true;
- }
- if (Role < ROLE_Authority)
- {
- ServerCrouch();
- }
- else
- {
- }
- }
- void ASCharacter::StartADS()
- {
- WantsToAim = true;
- if (Role < ROLE_Authority)
- {
- ServerWantsToAimTrue();
- }
- else
- {
- }
- }
- void ASCharacter::StopADS()
- {
- WantsToAim = false;
- if (Role < ROLE_Authority)
- {
- ServerWantsToAimFalse();
- }
- else
- {
- }
- }
- void ASCharacter::Sprint()
- {
- if (IsCrouching == true)
- {
- IsCrouching = false;
- CrouchToggle = false;
- }
- IsSprinting = true;
- GetCharacterMovement()->MaxWalkSpeed = 600.0f;
- CanAim = false;
- if (Role < ROLE_Authority)
- {
- ServerSprintStart();
- }
- else
- {
- }
- }
- void ASCharacter::StopSprint()
- {
- if (!IsCrouching)
- {
- if (IsCrouching == true)
- {
- CanAim = true;
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- }
- else
- {
- IsSprinting = false;
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- CanAim = true;
- }
- if (Role < ROLE_Authority)
- {
- ServerSprintStop();
- }
- else
- {
- }
- }
- else
- {
- }
- }
- void ASCharacter::SelectFloat()
- {
- if (Role < ROLE_Authority)
- {
- ServerSelectFloat();
- }
- else
- {
- if (PickA == true)
- {
- CalculationValue = GetControlRotation().Pitch;
- SelectFloatReturn = 360.0f - CalculationValue;
- }
- if (PickA != true)
- {
- CalculationValue = GetControlRotation().Pitch;
- SelectFloatReturn = CalculationValue * -1.0f;
- }
- }
- }
- void ASCharacter::LeanLeft()
- {
- if (!IsLeaningLeft)
- {
- LeanAmount = 22.5f;
- IsLeaningLeft = true;
- IsLeaningRight = false;
- GetCharacterMovement()->MaxWalkSpeed = 150.0f;
- }
- else
- {
- LeanAmount = 0.0f;
- IsLeaningLeft = false;
- IsLeaningRight = false;
- if (IsCrouching)
- {
- }
- else
- {
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- }
- }
- if (Role < ROLE_Authority)
- {
- ServerLeanLeft();
- }
- else
- {
- }
- }
- void ASCharacter::LeanRight()
- {
- if (!IsLeaningRight)
- {
- LeanAmount = -22.5f;
- IsLeaningLeft = false;
- IsLeaningRight = true;
- GetCharacterMovement()->MaxWalkSpeed = 150.0f;
- }
- else
- {
- LeanAmount = 0.0f;
- IsLeaningLeft = false;
- IsLeaningRight = false;
- if (IsCrouching)
- {
- }
- else
- {
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- }
- }
- if (Role < ROLE_Authority)
- {
- ServerLeanRight();
- }
- else
- {
- }
- }
- void ASCharacter::SetViewRotation()
- {
- if (Role < ROLE_Authority) //Client
- {
- ServerSetViewRotation();
- ViewRotation.Roll = SelectFloatReturn / 3.0f;
- }
- else //Server
- {
- ServerSetViewRotation();
- ViewRotation.Roll = SelectFloatReturn / 3.0f;
- }
- }
- void ASCharacter::DetermineIsA()
- {
- if (Role < ROLE_Authority)
- {
- // if (GetControlRotation().Pitch > 180)
- // {
- // PickA = true;
- // }
- // if (GetControlRotation().Pitch < 180)
- // {
- // PickA = false;
- // }
- ServerDetermineIsA();
- }
- else
- {
- if (GetControlRotation().Pitch > 180)
- {
- PickA = true;
- }
- if (GetControlRotation().Pitch < 180)
- {
- PickA = false;
- }
- }
- }
- // Called every frame
- void ASCharacter::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- //Crouching
- float TargetHalfHeight = IsCrouching? CrouchedCapsuleHalfHeight : DefaultCapsuleHalfHeight;
- float NewHalfHeight = FMath::FInterpTo(GetCapsuleComponent()->GetScaledCapsuleHalfHeight(), TargetHalfHeight, DeltaTime, CrouchSpeed);
- GetCapsuleComponent()->SetCapsuleHalfHeight(NewHalfHeight);
- //Leaning Left
- if (Role < ROLE_Authority)
- {
- ServerExecLeanLeft(DeltaTime);
- }
- else
- {
- float TargetLeanAmountLeft = IsLeaningLeft ? 0.0f : LeanAmount;
- float NewLeanAmountLeft = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountLeft, DeltaTime, LeanSpeed);
- ViewRotation.Pitch = NewLeanAmountLeft;
- }
- //Leaning Right
- if (Role < ROLE_Authority)
- {
- ServerExecLeanRight(DeltaTime);
- }
- else
- {
- float TargetLeanAmountRight = IsLeaningRight ? 0.0f : LeanAmount;
- float NewLeanAmountRight = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountRight, DeltaTime, LeanSpeed);
- ViewRotation.Pitch = NewLeanAmountRight;
- }
- //Aiming
- if (WantsToAim == true && CanAim == true)
- {
- if (Role < ROLE_Authority)
- {
- ServerSetAimingTrue();
- }
- else
- {
- IsAiming = true;
- }
- }
- else
- {
- if (Role < ROLE_Authority)
- {
- ServerSetAimingFalse();
- }
- else
- {
- IsAiming = false;
- }
- }
- //Replicate Look Rotation
- DetermineIsA();
- SelectFloat();
- SetViewRotation();
- }
- // Called to bind functionality to input
- void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- PlayerInputComponent->BindAxis("MoveForward", this, &ASCharacter::MoveForward);
- PlayerInputComponent->BindAxis("MoveRight", this, &ASCharacter::MoveRight);
- PlayerInputComponent->BindAxis("LookUp", this, &ASCharacter::AddControllerPitchInput);
- PlayerInputComponent->BindAxis("Turn", this, &ASCharacter::AddControllerYawInput);
- PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ASCharacter::ExecCrouch);
- PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ASCharacter::Jump);
- PlayerInputComponent->BindAction("ADS", IE_Pressed, this, &ASCharacter::StartADS);
- PlayerInputComponent->BindAction("ADS", IE_Released, this, &ASCharacter::StopADS);
- PlayerInputComponent->BindAction("LeanLeft", IE_Pressed, this, &ASCharacter::LeanLeft);
- PlayerInputComponent->BindAction("LeanRight", IE_Pressed, this, &ASCharacter::LeanRight);
- PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &ASCharacter::Sprint);
- PlayerInputComponent->BindAction("Sprint", IE_Released, this, &ASCharacter::StopSprint);
- }
- void ASCharacter::ServerDetermineIsA_Implementation()
- {
- if (GetControlRotation().Pitch > 180)
- {
- PickA = true;
- }
- if (GetControlRotation().Pitch < 180)
- {
- PickA = false;
- }
- }
- bool ASCharacter::ServerDetermineIsA_Validate()
- {
- return true;
- }
- void ASCharacter::ServerExecLeanLeft_Implementation(float DeltaTime)
- {
- float TargetLeanAmountLeft = IsLeaningLeft ? 0.0f : LeanAmount;
- float NewLeanAmountLeft = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountLeft, DeltaTime, LeanSpeed);
- ViewRotation.Pitch = NewLeanAmountLeft;
- }
- bool ASCharacter::ServerExecLeanLeft_Validate(float DeltaTime)
- {
- return true;
- }
- void ASCharacter::ServerExecLeanRight_Implementation(float DeltaTime)
- {
- float TargetLeanAmountRight = IsLeaningRight ? 0.0f : LeanAmount;
- float NewLeanAmountRight = FMath::FInterpTo(ViewRotation.Pitch, TargetLeanAmountRight, DeltaTime, LeanSpeed);
- ViewRotation.Pitch = NewLeanAmountRight;
- }
- bool ASCharacter::ServerExecLeanRight_Validate(float DeltaTime)
- {
- return true;
- }
- void ASCharacter::ServerLeanLeft_Implementation()
- {
- if (!IsLeaningLeft)
- {
- LeanAmount = 22.5f;
- IsLeaningLeft = true;
- IsLeaningRight = false;
- GetCharacterMovement()->MaxWalkSpeed = 150.0f;
- }
- else
- {
- LeanAmount = 0.0f;
- IsLeaningLeft = false;
- IsLeaningRight = false;
- if (IsCrouching)
- {
- }
- else
- {
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- }
- }
- }
- bool ASCharacter::ServerLeanLeft_Validate()
- {
- return true;
- }
- void ASCharacter::ServerLeanRight_Implementation()
- {
- if (!IsLeaningRight)
- {
- LeanAmount = -22.5f;
- IsLeaningLeft = false;
- IsLeaningRight = true;
- GetCharacterMovement()->MaxWalkSpeed = 150.0f;
- }
- else
- {
- LeanAmount = 0.0f;
- IsLeaningLeft = false;
- IsLeaningRight = false;
- if (IsCrouching)
- {
- }
- else
- {
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- }
- }
- }
- bool ASCharacter::ServerLeanRight_Validate()
- {
- return true;
- }
- void ASCharacter::ServerReplicateMuzzleLocation_Implementation()
- {
- if (!IsLocallyControlled())
- {
- FRotator NewRot = MeshComp->RelativeRotation;
- NewRot.Pitch = RemoteViewPitch * 360.0f / 255.0f;
- MeshComp->SetRelativeRotation(NewRot);
- }
- }
- bool ASCharacter::ServerReplicateMuzzleLocation_Validate()
- {
- return true;
- }
- void ASCharacter::ServerSetViewRotation_Implementation()
- {
- ViewRotation.Roll = SelectFloatReturn / 3.0f;
- }
- bool ASCharacter::ServerSetViewRotation_Validate()
- {
- return true;
- }
- void ASCharacter::ServerSelectFloat_Implementation()
- {
- if (PickA == true)
- {
- CalculationValue = GetControlRotation().Pitch;
- SelectFloatReturn = 360.0f - CalculationValue;
- }
- if (PickA != true)
- {
- CalculationValue = GetControlRotation().Pitch;
- SelectFloatReturn = CalculationValue * -1.0f;
- }
- }
- bool ASCharacter::ServerSelectFloat_Validate()
- {
- return true;
- }
- void ASCharacter::ServerWantsToAimTrue_Implementation()
- {
- WantsToAim = true;
- }
- bool ASCharacter::ServerWantsToAimTrue_Validate()
- {
- return true;
- }
- void ASCharacter::ServerWantsToAimFalse_Implementation()
- {
- WantsToAim = false;
- }
- bool ASCharacter::ServerWantsToAimFalse_Validate()
- {
- return true;
- }
- void ASCharacter::ServerSetAimingTrue_Implementation()
- {
- IsAiming = true;
- }
- bool ASCharacter::ServerSetAimingTrue_Validate()
- {
- return true;
- }
- void ASCharacter::ServerSetAimingFalse_Implementation()
- {
- IsAiming = false;
- }
- bool ASCharacter::ServerSetAimingFalse_Validate()
- {
- return true;
- }
- void ASCharacter::ServerSprintStop_Implementation()
- {
- if (!IsCrouching)
- {
- if (IsCrouching == true)
- {
- CanAim = true;
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- }
- else
- {
- IsSprinting = false;
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- CanAim = true;
- }
- }
- else
- {
- }
- }
- bool ASCharacter::ServerSprintStop_Validate()
- {
- return true;
- }
- void ASCharacter::ServerSprintStart_Implementation()
- {
- if (IsCrouching == true)
- {
- IsCrouching = false;
- CrouchToggle = false;
- }
- IsSprinting = true;
- GetCharacterMovement()->MaxWalkSpeed = 600.0f;
- CanAim = false;
- }
- bool ASCharacter::ServerSprintStart_Validate()
- {
- return true;
- }
- void ASCharacter::ServerCrouch_Implementation()
- {
- if (CrouchToggle == false)
- {
- if (IsSprinting == true)
- {
- IsSprinting = false;
- }
- IsCrouching = true;
- CrouchToggle = true;
- GetCharacterMovement()->MaxWalkSpeed = 150.0f;
- CanAim = true;
- }
- else
- {
- IsCrouching = false;
- CrouchToggle = false;
- GetCharacterMovement()->MaxWalkSpeed = 400.0f;
- CanAim = true;
- }
- }
- bool ASCharacter::ServerCrouch_Validate()
- {
- return true;
- }
- void ASCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
- {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME(ASCharacter, IsCrouching);
- DOREPLIFETIME(ASCharacter, IsSprinting);
- DOREPLIFETIME(ASCharacter, WantsToAim);
- DOREPLIFETIME(ASCharacter, CanAim);
- DOREPLIFETIME(ASCharacter, IsAiming);
- DOREPLIFETIME(ASCharacter, ViewRotation);
- DOREPLIFETIME(ASCharacter, IsLeaningLeft);
- DOREPLIFETIME(ASCharacter, IsLeaningRight);
- DOREPLIFETIME(ASCharacter, LeanAmount);
- DOREPLIFETIME(ASCharacter, PickA);
- DOREPLIFETIME(ASCharacter, SelectFloatReturn);
- DOREPLIFETIME(ASCharacter, CalculationValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement