561 lines
14 KiB
C++
561 lines
14 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "BlasterCharacter.h"
|
|
|
|
#include "Blaster/Blaster.h"
|
|
#include "Blaster/Components/CombatComponent.h"
|
|
#include "Blaster/GameMode/BlasterGameMode.h"
|
|
#include "Blaster/PlayerController/BlasterPlayerController.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;
|
|
|
|
SpawnCollisionHandlingMethod = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
|
|
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->SetIsReplicated(true);
|
|
|
|
GetCharacterMovement()->NavAgentProps.bCanCrouch = true;
|
|
GetCapsuleComponent()->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
|
|
GetMesh()->SetCollisionObjectType(ECC_SkeletalMesh);
|
|
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;
|
|
|
|
DissolveTimeline = CreateDefaultSubobject<UTimelineComponent>(TEXT("DissolveTimelineComponent"));
|
|
}
|
|
|
|
void ABlasterCharacter::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
UpdateHUDHealth();
|
|
if (HasAuthority())
|
|
{
|
|
OnTakeAnyDamage.AddDynamic(this, &ABlasterCharacter::ReceiveDamage);
|
|
}
|
|
}
|
|
|
|
void ABlasterCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
|
|
{
|
|
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
|
|
|
DOREPLIFETIME_CONDITION(ABlasterCharacter, OverlappingWeapon, COND_OwnerOnly);
|
|
DOREPLIFETIME(ABlasterCharacter, Health);
|
|
}
|
|
|
|
void ABlasterCharacter::OnRep_ReplicatedMovement()
|
|
{
|
|
Super::OnRep_ReplicatedMovement();
|
|
|
|
SimProxiesTurn();
|
|
TimeSinceLastMovementReplication = 0.f;
|
|
}
|
|
|
|
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);
|
|
const FName SectionName = bAiming ? FName("RifleADS") : FName("RifleHip");
|
|
AnimInstance->Montage_JumpToSection(SectionName);
|
|
}
|
|
}
|
|
|
|
void ABlasterCharacter::PlayEliminatedMontage()
|
|
{
|
|
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
|
|
if (AnimInstance && EliminatedMontage)
|
|
{
|
|
AnimInstance->Montage_Play(EliminatedMontage);
|
|
}
|
|
}
|
|
|
|
void ABlasterCharacter::Eliminated()
|
|
{
|
|
MulticastEliminated();
|
|
GetWorldTimerManager().SetTimer(EliminationTimer, this, &ABlasterCharacter::EliminationTimerFinished, EliminationDelay);
|
|
}
|
|
|
|
void ABlasterCharacter::MulticastEliminated_Implementation()
|
|
{
|
|
bEliminated = true;
|
|
PlayEliminatedMontage();
|
|
|
|
if (DissolveMaterialInstance)
|
|
{
|
|
DynamicDissolveMaterialInstance = UMaterialInstanceDynamic::Create(DissolveMaterialInstance, this);
|
|
GetMesh()->SetMaterial(0, DynamicDissolveMaterialInstance);
|
|
DynamicDissolveMaterialInstance->SetScalarParameterValue(TEXT("Dissolve"), 0.55f);
|
|
DynamicDissolveMaterialInstance->SetScalarParameterValue(TEXT("Glow"), 200.f);
|
|
}
|
|
StartDissolve();
|
|
}
|
|
|
|
void ABlasterCharacter::EliminationTimerFinished()
|
|
{
|
|
ABlasterGameMode* GameMode = GetWorld()->GetAuthGameMode<ABlasterGameMode>();
|
|
if (GameMode)
|
|
{
|
|
GameMode->RequestRespawn(this, Controller);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
void ABlasterCharacter::ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* InstigatorController,
|
|
AActor* DamageCauser)
|
|
{
|
|
Health = FMath::Clamp(Health - Damage, 0.f, MaxHealth);
|
|
|
|
UpdateHUDHealth();
|
|
if (Health > 0.f)
|
|
{
|
|
PlayHitReactMontage();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ABlasterCharacter::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
if (GetLocalRole() > ROLE_SimulatedProxy && IsLocallyControlled())
|
|
{
|
|
AimOffset(DeltaTime);
|
|
}
|
|
else
|
|
{
|
|
TimeSinceLastMovementReplication += DeltaTime;
|
|
if (TimeSinceLastMovementReplication > 0.25f)
|
|
{
|
|
OnRep_ReplicatedMovement();
|
|
}
|
|
CalculateAO_Pitch();
|
|
}
|
|
|
|
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;
|
|
|
|
const float Speed = CalculateSpeed();
|
|
const bool bIsInAir = GetCharacterMovement()->IsFalling();
|
|
|
|
if (Speed == 0.f && !bIsInAir) // Standing still, not jumping
|
|
{
|
|
bRotateRootBone = true;
|
|
const FRotator CurrentAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f);
|
|
const 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
|
|
{
|
|
bRotateRootBone = false;
|
|
StartingAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f);
|
|
AO_Yaw = 0.f;
|
|
bUseControllerRotationYaw = true;
|
|
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
|
|
}
|
|
|
|
CalculateAO_Pitch();
|
|
}
|
|
|
|
void ABlasterCharacter::CalculateAO_Pitch()
|
|
{
|
|
AO_Pitch = GetBaseAimRotation().Pitch;
|
|
|
|
// Fix pitch/yaw compression
|
|
if (AO_Pitch > 90.f && !IsLocallyControlled())
|
|
{
|
|
// map pitch from [270, 360) to [-90, 0)
|
|
const FVector2d InRange(270.f, 360.f);
|
|
const FVector2d OutRange(-90.f, 0.f);
|
|
AO_Pitch = FMath::GetMappedRangeValueClamped(InRange, OutRange, AO_Pitch);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
float ABlasterCharacter::CalculateSpeed()
|
|
{
|
|
FVector Velocity = GetVelocity();
|
|
Velocity.Z = 0.f;
|
|
return Velocity.Size();
|
|
}
|
|
|
|
void ABlasterCharacter::OnRep_Health()
|
|
{
|
|
UpdateHUDHealth();
|
|
if (Health > 0.f)
|
|
{
|
|
PlayHitReactMontage();
|
|
}
|
|
}
|
|
|
|
void ABlasterCharacter::UpdateHUDHealth()
|
|
{
|
|
BlasterPlayerController = BlasterPlayerController == nullptr ? Cast<ABlasterPlayerController>(Controller) : BlasterPlayerController;
|
|
if (BlasterPlayerController)
|
|
{
|
|
BlasterPlayerController->SetHUDHealth(Health, MaxHealth);
|
|
}
|
|
}
|
|
|
|
void ABlasterCharacter::UpdateDissolveMaterial(float DissolveValue)
|
|
{
|
|
if (DynamicDissolveMaterialInstance)
|
|
{
|
|
DynamicDissolveMaterialInstance->SetScalarParameterValue(TEXT("Dissolve"), DissolveValue);
|
|
}
|
|
}
|
|
|
|
void ABlasterCharacter::StartDissolve()
|
|
{
|
|
DissolveTrack.BindDynamic(this, &ABlasterCharacter::UpdateDissolveMaterial);
|
|
if (DissolveCurve && DissolveTimeline)
|
|
{
|
|
DissolveTimeline->AddInterpFloat(DissolveCurve, DissolveTrack);
|
|
DissolveTimeline->Play();
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|