blaster/Source/Blaster/Character/BlasterCharacter.cpp

366 lines
9.1 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterCharacter.h"
#include "Blaster/Components/CombatComponent.h"
#include "Blaster/Weapon/Weapon.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/WidgetComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "Net/UnrealNetwork.h"
ABlasterCharacter::ABlasterCharacter()
{
PrimaryActorTick.bCanEverTick = true;
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetMesh());
CameraBoom->TargetArmLength = 350.f;
CameraBoom->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
OverheadWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("OverheadWidget"));
OverheadWidget->SetupAttachment(RootComponent);
Combat = CreateDefaultSubobject<UCombatComponent>(TEXT("CombatComponent"));
Combat->SetIsReplicatedByDefault(true);
GetCharacterMovement()->NavAgentProps.bCanCrouch = true;
GetCapsuleComponent()->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
GetMesh()->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
GetMesh()->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
GetCharacterMovement()->RotationRate = FRotator(0.f, 850.f, 0.f);
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
NetUpdateFrequency = 66.f;
MinNetUpdateFrequency = 33.f;
}
void ABlasterCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME_CONDITION(ABlasterCharacter, OverlappingWeapon, COND_OwnerOnly);
}
void ABlasterCharacter::PostInitializeComponents()
{
Super::PostInitializeComponents();
if (Combat)
{
Combat->Character = this;
}
}
void ABlasterCharacter::PlayFireMontage(bool bAiming)
{
if (Combat == nullptr || Combat->EquippedWeapon == nullptr) return;
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && FireWeaponMontage)
{
AnimInstance->Montage_Play(FireWeaponMontage);
FName SectionName = bAiming ? FName("RifleADS") : FName("RifleHip");
AnimInstance->Montage_JumpToSection(SectionName);
}
}
void ABlasterCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ABlasterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
AimOffset(DeltaTime);
HideCameraIfCharacterClose();
}
void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ABlasterCharacter::Jump);
PlayerInputComponent->BindAction("Equip", IE_Pressed, this, &ABlasterCharacter::EquipButtonPressed);
PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ABlasterCharacter::CrouchButtonPressed);
PlayerInputComponent->BindAction("Aim", IE_Pressed, this, &ABlasterCharacter::AimButtonPressed);
PlayerInputComponent->BindAction("Aim", IE_Released, this, &ABlasterCharacter::AimButtonReleased);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ABlasterCharacter::FireButtonPressed);
PlayerInputComponent->BindAction("Fire", IE_Released, this, &ABlasterCharacter::FireButtonReleased);
PlayerInputComponent->BindAxis("MoveForward", this, &ABlasterCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ABlasterCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &ABlasterCharacter::Turn);
PlayerInputComponent->BindAxis("LookUp", this, &ABlasterCharacter::LookUp);
}
void ABlasterCharacter::MoveForward(float Value)
{
if (Controller != nullptr && Value != 0.f)
{
const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);
const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X));
AddMovementInput(Direction, Value);
}
}
void ABlasterCharacter::MoveRight(float Value)
{
if (Controller != nullptr && Value != 0.f)
{
const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);
const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y));
AddMovementInput(Direction, Value);
}
}
void ABlasterCharacter::Turn(float Value)
{
AddControllerYawInput(Value);
}
void ABlasterCharacter::LookUp(float Value)
{
AddControllerPitchInput(Value);
}
void ABlasterCharacter::EquipButtonPressed()
{
if (Combat)
{
if (HasAuthority())
{
Combat->EquipWeapon(OverlappingWeapon);
}
else
{
ServerEquipButtonPressed();
}
}
}
void ABlasterCharacter::CrouchButtonPressed()
{
if (bIsCrouched)
{
UnCrouch();
}
else
{
Crouch();
}
}
void ABlasterCharacter::AimButtonPressed()
{
if (Combat)
{
Combat->SetAiming(true);
}
}
void ABlasterCharacter::AimButtonReleased()
{
if (Combat)
{
Combat->SetAiming(false);
}
}
void ABlasterCharacter::AimOffset(float DeltaTime)
{
if (Combat && Combat->EquippedWeapon == nullptr) return;
FVector Velocity = GetVelocity();
Velocity.Z = 0.f;
float Speed = Velocity.Size();
bool bIsInAir = GetCharacterMovement()->IsFalling();
if (Speed == 0.f && !bIsInAir) // Standing still, not jumping
{
FRotator CurrentAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f);
FRotator DeltaAimRotation = UKismetMathLibrary::NormalizedDeltaRotator(CurrentAimRotation, StartingAimRotation);
AO_Yaw = DeltaAimRotation.Yaw;
if (TurningInPlace == ETurningInPlace::ETIP_NotTurning)
{
InterpAO_Yaw = AO_Yaw;
}
bUseControllerRotationYaw = true;
TurnInPlace(DeltaTime);
}
if (Speed > 0.f || bIsInAir) // Running or jumping
{
StartingAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f);
AO_Yaw = 0.f;
bUseControllerRotationYaw = true;
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
}
AO_Pitch = GetBaseAimRotation().Pitch;
// Fix pitch/yaw compression
if (AO_Pitch > 90.f && !IsLocallyControlled())
{
// map pitch from [270, 360) to [-90, 0)
FVector2d InRange(270.f, 360.f);
FVector2d OutRange(-90.f, 0.f);
AO_Pitch = FMath::GetMappedRangeValueClamped(InRange, OutRange, AO_Pitch);
}
}
void ABlasterCharacter::Jump()
{
if (bIsCrouched)
{
UnCrouch();
}
else
{
Super::Jump();
}
}
void ABlasterCharacter::FireButtonPressed()
{
if (Combat)
{
Combat->FireButtonPressed(true);
}
}
void ABlasterCharacter::FireButtonReleased()
{
if (Combat)
{
Combat->FireButtonPressed(false);
}
}
void ABlasterCharacter::ServerEquipButtonPressed_Implementation()
{
EquipButtonPressed();
}
void ABlasterCharacter::TurnInPlace(float DeltaTime)
{
// UE_LOG(LogTemp, Warning, TEXT("AO_Yaw: %f"), AO_Yaw);
if (AO_Yaw > 90.f)
{
TurningInPlace = ETurningInPlace::ETIP_Right;
}
else if (AO_Yaw < -90.f)
{
TurningInPlace = ETurningInPlace::ETIP_Left;
}
if (TurningInPlace != ETurningInPlace::ETIP_NotTurning)
{
InterpAO_Yaw = FMath::FInterpTo(InterpAO_Yaw, 0.f, DeltaTime, 4.f);
AO_Yaw = InterpAO_Yaw;
if (FMath::Abs(AO_Yaw) < 15.f)
{
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
StartingAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f);
}
}
switch (TurningInPlace)
{
case ETurningInPlace::ETIP_Left:
UE_LOG(LogTemp, Warning, TEXT("TurningInPlace: Left"));
break;
case ETurningInPlace::ETIP_Right:
UE_LOG(LogTemp, Warning, TEXT("TurningInPlace: Right"));
break;
case ETurningInPlace::ETIP_NotTurning:
UE_LOG(LogTemp, Warning, TEXT("TurningInPlace: NotTurning"));
break;
}
}
void ABlasterCharacter::HideCameraIfCharacterClose()
{
if (!IsLocallyControlled()) return;
if((FollowCamera->GetComponentLocation() - GetActorLocation()).Size() < CameraThreshold)
{
GetMesh()->SetVisibility(false);
if (Combat && Combat->EquippedWeapon && Combat->EquippedWeapon->GetWeaponMesh())
{
Combat->EquippedWeapon->GetWeaponMesh()->bOwnerNoSee = true;
}
} else
{
GetMesh()->SetVisibility(true);
if (Combat && Combat->EquippedWeapon && Combat->EquippedWeapon->GetWeaponMesh())
{
Combat->EquippedWeapon->GetWeaponMesh()->bOwnerNoSee = false;
}
}
}
void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(false);
}
OverlappingWeapon = Weapon;
if (IsLocallyControlled())
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
}
}
bool ABlasterCharacter::IsWeaponEquipped()
{
return Combat && Combat->EquippedWeapon;
}
bool ABlasterCharacter::IsAiming()
{
return Combat && Combat->bAiming;
}
AWeapon* ABlasterCharacter::GetEquippedWeapon()
{
if (Combat == nullptr) return nullptr;
return Combat->EquippedWeapon;
}
FVector ABlasterCharacter::GetHitTarget() const
{
if (Combat == nullptr) return FVector();
return Combat->HitTarget;
}
void ABlasterCharacter::OnRep_OverlappingWeapon(AWeapon* LastWeapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
if (LastWeapon)
{
LastWeapon->ShowPickupWidget(false);
}
}