blaster/Source/Blaster/Character/BlasterCharacter.cpp

337 lines
8.3 KiB
C++
Raw Normal View History

2022-04-28 10:18:21 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterCharacter.h"
2022-04-30 11:09:28 +00:00
#include "Blaster/Components/CombatComponent.h"
2022-04-30 10:26:25 +00:00
#include "Blaster/Weapon/Weapon.h"
2022-04-28 10:41:43 +00:00
#include "Camera/CameraComponent.h"
2022-05-03 17:50:47 +00:00
#include "Components/CapsuleComponent.h"
2022-04-29 21:38:34 +00:00
#include "Components/WidgetComponent.h"
2022-04-28 13:30:18 +00:00
#include "GameFramework/CharacterMovementComponent.h"
2022-04-28 10:41:43 +00:00
#include "GameFramework/SpringArmComponent.h"
2022-05-03 21:14:04 +00:00
#include "Kismet/KismetMathLibrary.h"
2022-04-30 10:26:25 +00:00
#include "Net/UnrealNetwork.h"
2022-04-28 10:41:43 +00:00
2022-04-28 10:18:21 +00:00
ABlasterCharacter::ABlasterCharacter()
{
PrimaryActorTick.bCanEverTick = true;
2022-04-28 10:41:43 +00:00
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetMesh());
2022-05-04 22:39:17 +00:00
CameraBoom->TargetArmLength = 350.f;
2022-04-28 12:39:36 +00:00
CameraBoom->bUsePawnControlRotation = true;
2022-04-28 10:41:43 +00:00
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
2022-04-28 13:30:18 +00:00
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
2022-04-29 21:38:34 +00:00
OverheadWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("OverheadWidget"));
OverheadWidget->SetupAttachment(RootComponent);
2022-04-30 11:09:28 +00:00
Combat = CreateDefaultSubobject<UCombatComponent>(TEXT("CombatComponent"));
Combat->SetIsReplicated(true);
2022-04-30 15:10:07 +00:00
GetCharacterMovement()->NavAgentProps.bCanCrouch = true;
2022-05-03 17:50:47 +00:00
GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
2022-05-04 12:57:48 +00:00
GetCharacterMovement()->RotationRate = FRotator(0.f, 850.f, 0.f);
2022-05-04 09:28:00 +00:00
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
2022-05-04 12:27:45 +00:00
NetUpdateFrequency = 66.f;
MinNetUpdateFrequency = 33.f;
2022-04-28 10:18:21 +00:00
}
2022-04-30 10:26:25 +00:00
void ABlasterCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME_CONDITION(ABlasterCharacter, OverlappingWeapon, COND_OwnerOnly);
}
2022-04-30 11:09:28 +00:00
void ABlasterCharacter::PostInitializeComponents()
{
Super::PostInitializeComponents();
if (Combat)
{
Combat->Character = this;
}
}
2022-05-04 20:13:31 +00:00
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);
}
}
2022-04-28 10:18:21 +00:00
void ABlasterCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ABlasterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
2022-05-03 21:14:04 +00:00
AimOffset(DeltaTime);
2022-04-28 10:18:21 +00:00
}
void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
2022-05-04 12:57:48 +00:00
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ABlasterCharacter::Jump);
2022-04-30 11:09:28 +00:00
PlayerInputComponent->BindAction("Equip", IE_Pressed, this, &ABlasterCharacter::EquipButtonPressed);
2022-04-30 15:10:07 +00:00
PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ABlasterCharacter::CrouchButtonPressed);
2022-04-30 15:38:03 +00:00
PlayerInputComponent->BindAction("Aim", IE_Pressed, this, &ABlasterCharacter::AimButtonPressed);
PlayerInputComponent->BindAction("Aim", IE_Released, this, &ABlasterCharacter::AimButtonReleased);
2022-05-04 20:13:31 +00:00
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ABlasterCharacter::FireButtonPressed);
PlayerInputComponent->BindAction("Fire", IE_Released, this, &ABlasterCharacter::FireButtonReleased);
2022-04-30 14:08:00 +00:00
2022-04-28 12:39:36 +00:00
PlayerInputComponent->BindAxis("MoveForward", this, &ABlasterCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ABlasterCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &ABlasterCharacter::Turn);
PlayerInputComponent->BindAxis("LookUp", this, &ABlasterCharacter::LookUp);
2022-04-28 10:18:21 +00:00
}
2022-04-28 12:39:36 +00:00
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);
}
2022-04-30 10:26:25 +00:00
2022-04-30 11:09:28 +00:00
void ABlasterCharacter::EquipButtonPressed()
{
2022-04-30 14:08:00 +00:00
if (Combat)
2022-04-30 11:09:28 +00:00
{
2022-04-30 14:08:00 +00:00
if (HasAuthority())
{
Combat->EquipWeapon(OverlappingWeapon);
}
else
{
ServerEquipButtonPressed();
}
2022-04-30 11:09:28 +00:00
}
}
2022-04-30 15:10:07 +00:00
void ABlasterCharacter::CrouchButtonPressed()
{
if (bIsCrouched)
{
UnCrouch();
}
else
{
Crouch();
}
}
2022-04-30 15:38:03 +00:00
void ABlasterCharacter::AimButtonPressed()
{
if (Combat)
{
Combat->SetAiming(true);
}
}
void ABlasterCharacter::AimButtonReleased()
{
if (Combat)
{
Combat->SetAiming(false);
}
}
2022-05-03 21:14:04 +00:00
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;
2022-05-04 12:09:01 +00:00
if (TurningInPlace == ETurningInPlace::ETIP_NotTurning)
{
InterpAO_Yaw = AO_Yaw;
}
bUseControllerRotationYaw = true;
2022-05-04 09:28:00 +00:00
TurnInPlace(DeltaTime);
2022-05-03 21:14:04 +00:00
}
if (Speed > 0.f || bIsInAir) // Running or jumping
{
StartingAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f);
AO_Yaw = 0.f;
bUseControllerRotationYaw = true;
2022-05-04 09:28:00 +00:00
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
2022-05-03 21:14:04 +00:00
}
AO_Pitch = GetBaseAimRotation().Pitch;
2022-05-03 21:36:35 +00:00
// 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);
}
2022-05-03 21:14:04 +00:00
}
2022-05-04 12:57:48 +00:00
void ABlasterCharacter::Jump()
{
if (bIsCrouched)
{
UnCrouch();
}
else
{
Super::Jump();
}
}
2022-05-04 20:13:31 +00:00
void ABlasterCharacter::FireButtonPressed()
{
if (Combat)
{
Combat->FireButtonPressed(true);
}
}
void ABlasterCharacter::FireButtonReleased()
{
if (Combat)
{
Combat->FireButtonPressed(false);
}
}
2022-04-30 14:08:00 +00:00
void ABlasterCharacter::ServerEquipButtonPressed_Implementation()
{
EquipButtonPressed();
}
2022-05-04 09:28:00 +00:00
void ABlasterCharacter::TurnInPlace(float DeltaTime)
{
2022-05-04 12:09:01 +00:00
// UE_LOG(LogTemp, Warning, TEXT("AO_Yaw: %f"), AO_Yaw);
2022-05-04 09:28:00 +00:00
if (AO_Yaw > 90.f)
{
TurningInPlace = ETurningInPlace::ETIP_Right;
}
else if (AO_Yaw < -90.f)
{
TurningInPlace = ETurningInPlace::ETIP_Left;
}
2022-05-04 12:09:01 +00:00
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;
}
2022-05-04 09:28:00 +00:00
}
2022-04-30 10:26:25 +00:00
void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(false);
}
2022-04-30 11:09:28 +00:00
2022-04-30 10:26:25 +00:00
OverlappingWeapon = Weapon;
if (IsLocallyControlled())
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
}
}
2022-04-30 14:48:34 +00:00
bool ABlasterCharacter::IsWeaponEquipped()
{
return Combat && Combat->EquippedWeapon;
}
2022-04-30 15:38:03 +00:00
bool ABlasterCharacter::IsAiming()
{
return Combat && Combat->bAiming;
}
2022-05-03 22:43:10 +00:00
AWeapon* ABlasterCharacter::GetEquippedWeapon()
{
if (Combat == nullptr) return nullptr;
return Combat->EquippedWeapon;
}
2022-04-30 10:26:25 +00:00
void ABlasterCharacter::OnRep_OverlappingWeapon(AWeapon* LastWeapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
if (LastWeapon)
{
LastWeapon->ShowPickupWidget(false);
}
}