blaster/Source/Blaster/Character/BlasterCharacter.cpp

517 lines
13 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-05-05 22:41:35 +00:00
#include "Blaster/Blaster.h"
2022-04-30 11:09:28 +00:00
#include "Blaster/Components/CombatComponent.h"
2022-05-07 15:11:53 +00:00
#include "Blaster/GameMode/BlasterGameMode.h"
2022-05-07 10:03:33 +00:00
#include "Blaster/PlayerController/BlasterPlayerController.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"));
2022-05-06 21:55:01 +00:00
Combat->SetIsReplicated(true);
2022-04-30 15:10:07 +00:00
GetCharacterMovement()->NavAgentProps.bCanCrouch = true;
2022-05-05 20:46:35 +00:00
GetCapsuleComponent()->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
2022-05-05 22:41:35 +00:00
GetMesh()->SetCollisionObjectType(ECC_SkeletalMesh);
2022-05-05 20:46:35 +00:00
GetMesh()->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
GetMesh()->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
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-05-07 10:03:33 +00:00
void ABlasterCharacter::BeginPlay()
{
Super::BeginPlay();
2022-05-07 11:03:19 +00:00
UpdateHUDHealth();
if (HasAuthority())
2022-05-07 10:03:33 +00:00
{
2022-05-07 11:03:19 +00:00
OnTakeAnyDamage.AddDynamic(this, &ABlasterCharacter::ReceiveDamage);
2022-05-07 10:03:33 +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-05-07 09:04:11 +00:00
DOREPLIFETIME(ABlasterCharacter, Health);
2022-04-30 10:26:25 +00:00
}
2022-05-06 10:43:18 +00:00
void ABlasterCharacter::OnRep_ReplicatedMovement()
{
Super::OnRep_ReplicatedMovement();
SimProxiesTurn();
TimeSinceLastMovementReplication = 0.f;
}
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);
2022-05-06 10:43:18 +00:00
const FName SectionName = bAiming ? FName("RifleADS") : FName("RifleHip");
2022-05-04 20:13:31 +00:00
AnimInstance->Montage_JumpToSection(SectionName);
}
}
2022-05-07 17:10:32 +00:00
void ABlasterCharacter::PlayEliminatedMontage()
2022-05-07 15:11:53 +00:00
{
2022-05-07 17:10:32 +00:00
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && EliminatedMontage)
{
UE_LOG(LogTemp, Warning, TEXT("Playing Eliminated Montage, %s"), (bEliminated ? "true" : "false"));
AnimInstance->Montage_Play(EliminatedMontage);
}
}
void ABlasterCharacter::Eliminated_Implementation()
{
bEliminated = true;
PlayEliminatedMontage();
2022-05-07 15:11:53 +00:00
}
2022-05-05 22:41:35 +00:00
void ABlasterCharacter::PlayHitReactMontage()
{
if (Combat == nullptr || Combat->EquippedWeapon == nullptr) return;
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && HitReactMontage)
{
AnimInstance->Montage_Play(HitReactMontage);
const FName SectionName = FName("FromFront");
AnimInstance->Montage_JumpToSection(SectionName);
}
}
2022-05-07 11:03:19 +00:00
void ABlasterCharacter::ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* InstigatorController,
AActor* DamageCauser)
{
Health = FMath::Clamp(Health - Damage, 0.f, MaxHealth);
UpdateHUDHealth();
2022-05-07 17:10:32 +00:00
if (Health > 0.f)
{
PlayHitReactMontage();
}
2022-05-07 15:11:53 +00:00
if (Health == 0.f)
{
ABlasterGameMode* GameMode = GetWorld()->GetAuthGameMode<ABlasterGameMode>();
if (GameMode)
{
BlasterPlayerController = BlasterPlayerController == nullptr ? Cast<ABlasterPlayerController>(Controller) : BlasterPlayerController;
ABlasterPlayerController* AttackerController = Cast<ABlasterPlayerController>(InstigatorController);
GameMode->PlayerEliminated(this, BlasterPlayerController, AttackerController);
}
}
2022-05-07 11:03:19 +00:00
}
2022-04-28 10:18:21 +00:00
void ABlasterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
2022-05-03 21:14:04 +00:00
2022-05-06 10:43:18 +00:00
if (GetLocalRole() > ROLE_SimulatedProxy && IsLocallyControlled())
{
AimOffset(DeltaTime);
}
else
{
TimeSinceLastMovementReplication += DeltaTime;
if (TimeSinceLastMovementReplication > 0.25f)
{
OnRep_ReplicatedMovement();
}
CalculateAO_Pitch();
}
2022-05-05 21:03:09 +00:00
HideCameraIfCharacterClose();
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;
2022-05-06 10:43:18 +00:00
const float Speed = CalculateSpeed();
const bool bIsInAir = GetCharacterMovement()->IsFalling();
2022-05-03 21:14:04 +00:00
if (Speed == 0.f && !bIsInAir) // Standing still, not jumping
{
2022-05-06 10:43:18 +00:00
bRotateRootBone = true;
const FRotator CurrentAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f);
const FRotator DeltaAimRotation = UKismetMathLibrary::NormalizedDeltaRotator(CurrentAimRotation, StartingAimRotation);
2022-05-03 21:14:04 +00:00
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
{
2022-05-06 10:43:18 +00:00
bRotateRootBone = false;
2022-05-03 21:14:04 +00:00
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
}
2022-05-06 10:43:18 +00:00
CalculateAO_Pitch();
}
void ABlasterCharacter::CalculateAO_Pitch()
{
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)
2022-05-06 10:43:18 +00:00
const FVector2d InRange(270.f, 360.f);
const FVector2d OutRange(-90.f, 0.f);
2022-05-03 21:36:35 +00:00
AO_Pitch = FMath::GetMappedRangeValueClamped(InRange, OutRange, AO_Pitch);
}
2022-05-03 21:14:04 +00:00
}
2022-05-06 10:43:18 +00:00
void ABlasterCharacter::SimProxiesTurn()
{
if (Combat == nullptr || Combat->EquippedWeapon == nullptr) return;
bRotateRootBone = false;
const float Speed = CalculateSpeed();
if (Speed > 0.f)
{
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
return;
}
ProxyRotationLastFrame = ProxyRotation;
ProxyRotation = GetActorRotation();
ProxyYaw = UKismetMathLibrary::NormalizedDeltaRotator(ProxyRotation, ProxyRotationLastFrame).Yaw;
if (FMath::Abs(ProxyYaw) > TurnThreshold)
{
if (ProxyYaw > TurnThreshold)
{
TurningInPlace = ETurningInPlace::ETIP_Right;
}
else if (ProxyYaw < -TurnThreshold)
{
TurningInPlace = ETurningInPlace::ETIP_Left;
}
else
{
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
}
return;
}
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
}
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)
{
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);
}
}
2022-05-07 17:10:32 +00:00
// 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-05-05 21:03:09 +00:00
void ABlasterCharacter::HideCameraIfCharacterClose()
{
if (!IsLocallyControlled()) return;
2022-05-06 10:43:18 +00:00
if ((FollowCamera->GetComponentLocation() - GetActorLocation()).Size() < CameraThreshold)
2022-05-05 21:03:09 +00:00
{
GetMesh()->SetVisibility(false);
if (Combat && Combat->EquippedWeapon && Combat->EquippedWeapon->GetWeaponMesh())
{
Combat->EquippedWeapon->GetWeaponMesh()->bOwnerNoSee = true;
}
2022-05-06 10:43:18 +00:00
}
else
2022-05-05 21:03:09 +00:00
{
GetMesh()->SetVisibility(true);
if (Combat && Combat->EquippedWeapon && Combat->EquippedWeapon->GetWeaponMesh())
{
Combat->EquippedWeapon->GetWeaponMesh()->bOwnerNoSee = false;
}
}
}
2022-05-06 10:43:18 +00:00
float ABlasterCharacter::CalculateSpeed()
{
FVector Velocity = GetVelocity();
Velocity.Z = 0.f;
return Velocity.Size();
}
2022-05-07 09:04:11 +00:00
void ABlasterCharacter::OnRep_Health()
{
2022-05-07 11:03:19 +00:00
UpdateHUDHealth();
2022-05-07 17:10:32 +00:00
if (Health > 0.f)
{
PlayHitReactMontage();
}
2022-05-07 11:03:19 +00:00
}
void ABlasterCharacter::UpdateHUDHealth()
{
2022-05-07 15:11:53 +00:00
BlasterPlayerController = BlasterPlayerController == nullptr ? Cast<ABlasterPlayerController>(Controller) : BlasterPlayerController;
2022-05-07 11:03:19 +00:00
if (BlasterPlayerController)
{
BlasterPlayerController->SetHUDHealth(Health, MaxHealth);
}
2022-05-07 09:04:11 +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-05-05 18:14:17 +00:00
FVector ABlasterCharacter::GetHitTarget() const
{
if (Combat == nullptr) return FVector();
return Combat->HitTarget;
}
2022-04-30 10:26:25 +00:00
void ABlasterCharacter::OnRep_OverlappingWeapon(AWeapon* LastWeapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
if (LastWeapon)
{
LastWeapon->ShowPickupWidget(false);
}
}