blaster/Source/Blaster/Character/BlasterCharacter.cpp

950 lines
26 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-05-22 21:17:42 +00:00
#include "Blaster/Components/BuffComponent.h"
2022-04-30 11:09:28 +00:00
#include "Blaster/Components/CombatComponent.h"
2022-05-27 09:06:39 +00:00
#include "Blaster/Components/LagCompensationComponent.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-05-09 14:33:48 +00:00
#include "Blaster/PlayerState/BlasterPlayerState.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-26 16:27:47 +00:00
#include "Components/BoxComponent.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-08 18:59:54 +00:00
#include "Kismet/GameplayStatics.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-05-08 18:59:54 +00:00
#include "Particles/ParticleSystemComponent.h"
2022-04-28 10:41:43 +00:00
2022-04-28 10:18:21 +00:00
ABlasterCharacter::ABlasterCharacter()
{
PrimaryActorTick.bCanEverTick = true;
2022-05-07 18:34:40 +00:00
SpawnCollisionHandlingMethod = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
2022-04-28 10:41:43 +00:00
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetMesh());
2022-05-12 13:37:49 +00:00
CameraBoom->TargetArmLength = 600.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
2022-05-27 09:06:39 +00:00
LagCompensation = CreateDefaultSubobject<ULagCompensationComponent>(TEXT("LagCompensation"));
2022-05-22 21:17:42 +00:00
Buff = CreateDefaultSubobject<UBuffComponent>(TEXT("BuffComponent"));
Buff->SetIsReplicated(true);
2022-04-30 15:10:07 +00:00
GetCharacterMovement()->NavAgentProps.bCanCrouch = true;
2022-05-12 13:37:49 +00:00
GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
2022-05-05 22:41:35 +00:00
GetMesh()->SetCollisionObjectType(ECC_SkeletalMesh);
2022-05-12 13:37:49 +00:00
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block);
2022-05-04 12:57:48 +00:00
GetCharacterMovement()->RotationRate = FRotator(0.f, 850.f, 0.f);
2022-05-12 13:37:49 +00:00
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-05-08 16:35:37 +00:00
DissolveTimeline = CreateDefaultSubobject<UTimelineComponent>(TEXT("DissolveTimelineComponent"));
2022-05-21 14:45:02 +00:00
AttachedGrenade = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Attached Grenade"));
AttachedGrenade->SetupAttachment(GetMesh(), FName("RightHandThrowableSocket"));
AttachedGrenade->SetCollisionEnabled(ECollisionEnabled::NoCollision);
2022-05-26 16:27:47 +00:00
// Hit boxes for server-side rewind
head = CreateDefaultSubobject<UBoxComponent>(TEXT("head"));
head->SetupAttachment(GetMesh(), FName("head"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("head"), head);
2022-05-26 16:27:47 +00:00
pelvis = CreateDefaultSubobject<UBoxComponent>(TEXT("pelvis"));
pelvis->SetupAttachment(GetMesh(), FName("pelvis"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("pelvis"), pelvis);
2022-05-26 16:27:47 +00:00
spine_02 = CreateDefaultSubobject<UBoxComponent>(TEXT("spine_02"));
spine_02->SetupAttachment(GetMesh(), FName("spine_02"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("spine_02"), spine_02);
2022-05-26 16:27:47 +00:00
spine_03 = CreateDefaultSubobject<UBoxComponent>(TEXT("spine_03"));
spine_03->SetupAttachment(GetMesh(), FName("spine_03"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("spine_03"), spine_03);
2022-05-26 16:27:47 +00:00
upperarm_l = CreateDefaultSubobject<UBoxComponent>(TEXT("upperarm_l"));
upperarm_l->SetupAttachment(GetMesh(), FName("upperarm_l"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("upperarm_l"), upperarm_l);
2022-05-26 16:27:47 +00:00
upperarm_r = CreateDefaultSubobject<UBoxComponent>(TEXT("upperarm_r"));
upperarm_r->SetupAttachment(GetMesh(), FName("upperarm_r"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("upperarm_r"), upperarm_r);
2022-05-26 16:27:47 +00:00
lowerarm_l = CreateDefaultSubobject<UBoxComponent>(TEXT("lowerarm_l"));
lowerarm_l->SetupAttachment(GetMesh(), FName("lowerarm_l"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("lowerarm_l"), lowerarm_l);
2022-05-26 16:27:47 +00:00
lowerarm_r = CreateDefaultSubobject<UBoxComponent>(TEXT("lowerarm_r"));
lowerarm_r->SetupAttachment(GetMesh(), FName("lowerarm_r"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("lowerarm_r"), lowerarm_r);
2022-05-26 16:27:47 +00:00
hand_l = CreateDefaultSubobject<UBoxComponent>(TEXT("hand_l"));
hand_l->SetupAttachment(GetMesh(), FName("hand_l"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("hand_l"), hand_l);
2022-05-26 16:27:47 +00:00
hand_r = CreateDefaultSubobject<UBoxComponent>(TEXT("hand_r"));
hand_r->SetupAttachment(GetMesh(), FName("hand_r"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("hand_r"), hand_r);
2022-05-26 16:27:47 +00:00
backpack = CreateDefaultSubobject<UBoxComponent>(TEXT("backpack"));
backpack->SetupAttachment(GetMesh(), FName("backpack"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("backpack"), backpack);
2022-05-26 16:27:47 +00:00
blanket = CreateDefaultSubobject<UBoxComponent>(TEXT("blanket"));
blanket->SetupAttachment(GetMesh(), FName("backpack"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("blanket"), blanket);
2022-05-26 16:27:47 +00:00
thigh_l = CreateDefaultSubobject<UBoxComponent>(TEXT("thigh_l"));
thigh_l->SetupAttachment(GetMesh(), FName("thigh_l"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("thigh_l"), thigh_l);
2022-05-26 16:27:47 +00:00
thigh_r = CreateDefaultSubobject<UBoxComponent>(TEXT("thigh_r"));
thigh_r->SetupAttachment(GetMesh(), FName("thigh_r"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("thigh_r"), thigh_r);
2022-05-26 16:27:47 +00:00
calf_l = CreateDefaultSubobject<UBoxComponent>(TEXT("calf_l"));
calf_l->SetupAttachment(GetMesh(), FName("calf_l"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("calf_l"), calf_l);
2022-05-26 16:27:47 +00:00
calf_r = CreateDefaultSubobject<UBoxComponent>(TEXT("calf_r"));
calf_r->SetupAttachment(GetMesh(), FName("calf_r"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("calf_r"), calf_r);
2022-05-26 16:27:47 +00:00
foot_l = CreateDefaultSubobject<UBoxComponent>(TEXT("foot_l"));
foot_l->SetupAttachment(GetMesh(), FName("foot_l"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("foot_l"), foot_l);
2022-05-26 16:27:47 +00:00
foot_r = CreateDefaultSubobject<UBoxComponent>(TEXT("foot_r"));
foot_r->SetupAttachment(GetMesh(), FName("foot_r"));
2022-05-27 09:29:32 +00:00
HitCollisionBoxes.Add(FName("foot_r"), foot_r);
2022-05-27 22:03:06 +00:00
for (auto Box : HitCollisionBoxes)
{
if (Box.Value)
{
Box.Value->SetCollisionObjectType(ECC_HitBox);
Box.Value->SetCollisionResponseToAllChannels(ECR_Ignore);
Box.Value->SetCollisionResponseToChannel(ECC_HitBox, ECR_Block);
Box.Value->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
}
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-05-07 09:04:11 +00:00
DOREPLIFETIME(ABlasterCharacter, Health);
2022-05-23 09:27:12 +00:00
DOREPLIFETIME(ABlasterCharacter, Shield);
2022-05-10 14:03:35 +00:00
DOREPLIFETIME(ABlasterCharacter, bDisableGameplay);
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-05-28 14:26:06 +00:00
void ABlasterCharacter::Eliminated(bool bPlayerLeftGame)
2022-05-07 18:34:40 +00:00
{
2022-05-25 13:49:51 +00:00
if (Combat) Combat->DropWeapons();
2022-05-28 14:26:06 +00:00
MulticastEliminated(bPlayerLeftGame);
2022-05-07 18:34:40 +00:00
}
2022-05-28 14:26:06 +00:00
void ABlasterCharacter::MulticastEliminated_Implementation(bool bPlayerLeftGame)
2022-05-07 17:10:32 +00:00
{
2022-05-28 14:26:06 +00:00
bLeftGame = bPlayerLeftGame;
2022-05-09 16:39:41 +00:00
if (BlasterPlayerController)
{
BlasterPlayerController->SetHUDWeaponAmmo(0);
}
2022-05-07 17:10:32 +00:00
bEliminated = true;
PlayEliminatedMontage();
2022-05-08 17:31:58 +00:00
// Start dissolve effect
2022-05-08 16:35:37 +00:00
if (DissolveMaterialInstance)
{
DynamicDissolveMaterialInstance = UMaterialInstanceDynamic::Create(DissolveMaterialInstance, this);
GetMesh()->SetMaterial(0, DynamicDissolveMaterialInstance);
DynamicDissolveMaterialInstance->SetScalarParameterValue(TEXT("Dissolve"), 0.55f);
DynamicDissolveMaterialInstance->SetScalarParameterValue(TEXT("Glow"), 200.f);
}
StartDissolve();
2022-05-08 17:31:58 +00:00
// Disable character movement
GetCharacterMovement()->DisableMovement();
GetCharacterMovement()->StopMovementImmediately();
2022-05-10 14:03:35 +00:00
bDisableGameplay = true;
2022-05-17 22:41:31 +00:00
if (Combat)
{
Combat->FireButtonPressed(false);
}
2022-05-08 17:31:58 +00:00
// Disable collision
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
GetMesh()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
2022-05-08 18:59:54 +00:00
// Elimination bot
if (EliminationBotEffect)
{
const FVector EliminationBotSpawnPoint(GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z + 200.f);
2022-05-12 13:37:49 +00:00
EliminationBotComponent = UGameplayStatics::SpawnEmitterAtLocation(
GetWorld(),
EliminationBotEffect,
EliminationBotSpawnPoint,
GetActorRotation()
);
2022-05-08 18:59:54 +00:00
}
if (EliminationBotSound)
{
2022-05-12 13:37:49 +00:00
UGameplayStatics::SpawnSoundAtLocation(
this,
EliminationBotSound,
GetActorLocation()
);
2022-05-08 18:59:54 +00:00
}
2022-05-24 10:46:30 +00:00
if (IsLocallyControlled() && GetPrimaryWeapon() && GetPrimaryWeapon()->IsSniper() && IsAiming())
2022-05-20 14:12:12 +00:00
{
ShowSniperScopeWidget(false);
}
2022-05-28 14:26:06 +00:00
GetWorldTimerManager().SetTimer(
EliminationTimer,
this,
&ABlasterCharacter::EliminationTimerFinished,
EliminationDelay
);
2022-05-07 15:11:53 +00:00
}
2022-05-07 18:34:40 +00:00
void ABlasterCharacter::EliminationTimerFinished()
{
2022-05-12 13:37:49 +00:00
ABlasterGameMode* BlasterGameMode = GetWorld()->GetAuthGameMode<ABlasterGameMode>();
2022-05-28 14:26:06 +00:00
if (BlasterGameMode && !bLeftGame)
2022-05-07 18:34:40 +00:00
{
2022-05-12 13:37:49 +00:00
BlasterGameMode->RequestRespawn(this, Controller);
2022-05-07 18:34:40 +00:00
}
2022-05-28 14:26:06 +00:00
if (bLeftGame && IsLocallyControlled())
{
OnLeftGame.Broadcast();
}
}
void ABlasterCharacter::ServerLeaveGame_Implementation()
{
ABlasterGameMode* BlasterGameMode = GetWorld()->GetAuthGameMode<ABlasterGameMode>();
BlasterPlayerState = BlasterPlayerState == nullptr ? GetPlayerState<ABlasterPlayerState>() : BlasterPlayerState;
if (BlasterGameMode && BlasterPlayerState)
{
BlasterGameMode->PlayerLeftGame(BlasterPlayerState);
}
2022-05-07 18:34:40 +00:00
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::Destroyed()
2022-05-05 22:41:35 +00:00
{
2022-05-12 13:37:49 +00:00
Super::Destroyed();
2022-05-05 22:41:35 +00:00
2022-05-12 13:37:49 +00:00
if (EliminationBotComponent)
2022-05-05 22:41:35 +00:00
{
2022-05-12 13:37:49 +00:00
EliminationBotComponent->DestroyComponent();
}
2022-05-17 22:41:31 +00:00
ABlasterGameMode* BlasterGameMode = Cast<ABlasterGameMode>(UGameplayStatics::GetGameMode(this));
bool bMatchNotInProgress = BlasterGameMode && BlasterGameMode->GetMatchState() != MatchState::InProgress;
2022-05-24 10:46:30 +00:00
if (Combat && Combat->PrimaryWeapon && bMatchNotInProgress)
2022-05-12 13:37:49 +00:00
{
2022-05-24 10:46:30 +00:00
Combat->PrimaryWeapon->Destroy();
2022-05-05 22:41:35 +00:00
}
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::BeginPlay()
2022-05-07 11:03:19 +00:00
{
2022-05-12 13:37:49 +00:00
Super::BeginPlay();
2022-05-07 11:03:19 +00:00
2022-05-23 23:16:06 +00:00
SpawnDefaultWeapon();
UpdateHUDAmmo();
2022-05-07 11:03:19 +00:00
UpdateHUDHealth();
2022-05-23 09:47:49 +00:00
UpdateHUDShield();
2022-05-12 13:37:49 +00:00
if (HasAuthority())
2022-05-07 17:10:32 +00:00
{
2022-05-12 13:37:49 +00:00
OnTakeAnyDamage.AddDynamic(this, &ABlasterCharacter::ReceiveDamage);
2022-05-07 17:10:32 +00:00
}
2022-05-21 15:03:30 +00:00
if (AttachedGrenade)
{
AttachedGrenade->SetVisibility(false);
}
2022-05-12 13:37:49 +00:00
}
2022-05-07 15:11:53 +00:00
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
RotateInPlace(DeltaTime);
HideCameraIfCharacterClose();
PollInit();
2022-05-07 11:03:19 +00:00
}
2022-05-10 14:03:35 +00:00
void ABlasterCharacter::RotateInPlace(float DeltaTime)
2022-04-28 10:18:21 +00:00
{
2022-05-10 14:03:35 +00:00
if (bDisableGameplay)
{
bUseControllerRotationYaw = false;
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
return;
}
2022-05-12 13:37:49 +00:00
if (GetLocalRole() > ENetRole::ROLE_SimulatedProxy && IsLocallyControlled())
2022-05-06 10:43:18 +00:00
{
AimOffset(DeltaTime);
}
else
{
TimeSinceLastMovementReplication += DeltaTime;
if (TimeSinceLastMovementReplication > 0.25f)
{
OnRep_ReplicatedMovement();
}
CalculateAO_Pitch();
}
2022-05-10 14:03:35 +00:00
}
2022-05-06 10:43:18 +00:00
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
2022-05-10 14:03:35 +00:00
{
2022-05-12 13:37:49 +00:00
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ABlasterCharacter::Jump);
PlayerInputComponent->BindAxis("MoveForward", this, &ABlasterCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ABlasterCharacter::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &ABlasterCharacter::Turn);
PlayerInputComponent->BindAxis("LookUp", this, &ABlasterCharacter::LookUp);
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->BindAction("Reload", IE_Pressed, this, &ABlasterCharacter::ReloadButtonPressed);
2022-05-21 10:13:03 +00:00
PlayerInputComponent->BindAction("ThrowGrenade", IE_Pressed, this, &ABlasterCharacter::GrenadeButtonPressed);
2022-05-09 14:33:48 +00:00
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::PostInitializeComponents()
2022-05-09 14:33:48 +00:00
{
2022-05-12 13:37:49 +00:00
Super::PostInitializeComponents();
if (Combat)
2022-05-09 14:33:48 +00:00
{
2022-05-12 13:37:49 +00:00
Combat->Character = this;
}
2022-05-22 21:17:42 +00:00
if (Buff)
{
Buff->Character = this;
2022-05-22 23:06:42 +00:00
Buff->SetInitialSpeeds(GetCharacterMovement()->MaxWalkSpeed, GetCharacterMovement()->MaxWalkSpeedCrouched);
2022-05-22 23:53:02 +00:00
Buff->SetInitialJumpVelocity(GetCharacterMovement()->JumpZVelocity);
2022-05-22 21:17:42 +00:00
}
2022-05-27 09:06:39 +00:00
if (LagCompensation)
{
LagCompensation->Character = this;
if (Controller)
{
LagCompensation->Controller = Cast<ABlasterPlayerController>(Controller);
}
}
2022-05-12 13:37:49 +00:00
}
void ABlasterCharacter::PlayFireMontage(bool bAiming)
{
2022-05-24 10:46:30 +00:00
if (Combat == nullptr || Combat->PrimaryWeapon == nullptr) return;
2022-05-12 13:37:49 +00:00
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && FireWeaponMontage)
{
AnimInstance->Montage_Play(FireWeaponMontage);
FName SectionName;
SectionName = bAiming ? FName("RifleADS") : FName("RifleHip");
AnimInstance->Montage_JumpToSection(SectionName);
}
}
void ABlasterCharacter::PlayReloadMontage()
{
2022-05-24 10:46:30 +00:00
if (Combat == nullptr || Combat->PrimaryWeapon == nullptr) return;
2022-05-12 13:37:49 +00:00
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && ReloadMontage)
{
AnimInstance->Montage_Play(ReloadMontage);
FName SectionName;
2022-05-24 10:46:30 +00:00
switch (Combat->PrimaryWeapon->GetWeaponType())
2022-05-09 14:33:48 +00:00
{
2022-05-12 13:37:49 +00:00
case EWeaponType::EWT_AssaultRifle:
SectionName = FName("Rifle");
break;
2022-05-19 09:10:41 +00:00
case EWeaponType::EWT_RocketLauncher:
2022-05-20 21:24:41 +00:00
SectionName = FName("RocketLauncher");
2022-05-19 09:10:41 +00:00
break;
2022-05-19 17:13:21 +00:00
case EWeaponType::EWT_Pistol:
2022-05-20 21:24:41 +00:00
SectionName = FName("Pistol");
2022-05-19 17:13:21 +00:00
break;
2022-05-19 18:51:50 +00:00
case EWeaponType::EWT_SubmachineGun:
2022-05-20 21:24:41 +00:00
SectionName = FName("Pistol");
2022-05-19 18:51:50 +00:00
break;
2022-05-20 10:10:21 +00:00
case EWeaponType::EWT_Shotgun:
2022-05-20 21:24:41 +00:00
SectionName = FName("Shotgun");
2022-05-20 10:10:21 +00:00
break;
2022-05-20 12:59:40 +00:00
case EWeaponType::EWT_SniperRifle:
2022-05-20 21:24:41 +00:00
SectionName = FName("SniperRifle");
2022-05-20 12:59:40 +00:00
break;
2022-05-20 14:29:14 +00:00
case EWeaponType::EWT_GrenadeLauncher:
2022-05-20 21:24:41 +00:00
SectionName = FName("GrenadeLauncher");
2022-05-20 14:29:14 +00:00
break;
2022-05-19 17:13:21 +00:00
default:
2022-05-20 12:59:40 +00:00
SectionName = FName("Rifle");
2022-05-19 17:13:21 +00:00
break;
2022-05-09 14:33:48 +00:00
}
2022-05-12 13:37:49 +00:00
AnimInstance->Montage_JumpToSection(SectionName);
2022-05-09 14:33:48 +00:00
}
2022-04-28 10:18:21 +00:00
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::PlayEliminatedMontage()
2022-04-28 10:18:21 +00:00
{
2022-05-12 13:37:49 +00:00
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && EliminatedMontage)
{
AnimInstance->Montage_Play(EliminatedMontage);
}
}
2022-04-28 10:18:21 +00:00
2022-05-21 10:13:03 +00:00
void ABlasterCharacter::PlayThrowGrenadeMontage()
{
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && ThrowGrenadeMontage)
{
AnimInstance->Montage_Play(ThrowGrenadeMontage);
}
}
2022-05-28 10:16:32 +00:00
void ABlasterCharacter::PlaySwapMontage()
{
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && SwapMontage)
{
AnimInstance->Montage_Play(SwapMontage);
}
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::PlayHitReactMontage()
{
2022-05-24 10:46:30 +00:00
if (Combat == nullptr || Combat->PrimaryWeapon == nullptr) return;
2022-04-30 14:08:00 +00:00
2022-05-12 13:37:49 +00:00
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && HitReactMontage)
{
AnimInstance->Montage_Play(HitReactMontage);
FName SectionName("FromFront");
AnimInstance->Montage_JumpToSection(SectionName);
}
}
void ABlasterCharacter::ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* InstigatorController, AActor* DamageCauser)
{
2022-05-22 13:21:17 +00:00
if (bEliminated) return;
2022-05-23 09:47:49 +00:00
float DamageToHealth = Damage;
if (Shield > 0)
{
if (Shield >= Damage)
{
Shield = FMath::Clamp(Shield - Damage, 0.f, MaxShield);
DamageToHealth = 0.f;
}
else
{
DamageToHealth = FMath::Clamp(DamageToHealth - (Damage - Shield), 0.f, Damage);
Shield = 0.f;
}
}
Health = FMath::Clamp(Health - DamageToHealth, 0.f, MaxHealth);
2022-05-12 13:37:49 +00:00
UpdateHUDHealth();
2022-05-23 09:47:49 +00:00
UpdateHUDShield();
2022-05-12 13:37:49 +00:00
if (Health > 0.f)
{
PlayHitReactMontage();
}
if (Health == 0.f)
{
ABlasterGameMode* BlasterGameMode = GetWorld()->GetAuthGameMode<ABlasterGameMode>();
if (BlasterGameMode)
{
BlasterPlayerController = BlasterPlayerController == nullptr ? Cast<ABlasterPlayerController>(Controller) : BlasterPlayerController;
ABlasterPlayerController* AttackerController = Cast<ABlasterPlayerController>(InstigatorController);
BlasterGameMode->PlayerEliminated(this, BlasterPlayerController, AttackerController);
}
}
2022-04-28 10:18:21 +00:00
}
2022-04-28 12:39:36 +00:00
void ABlasterCharacter::MoveForward(float Value)
{
2022-05-10 14:03:35 +00:00
if (bDisableGameplay) return;
2022-04-28 12:39:36 +00:00
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)
{
2022-05-10 14:03:35 +00:00
if (bDisableGameplay) return;
2022-04-28 12:39:36 +00:00
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-05-10 14:03:35 +00:00
if (bDisableGameplay) return;
2022-04-30 14:08:00 +00:00
if (Combat)
2022-04-30 11:09:28 +00:00
{
2022-05-28 10:16:32 +00:00
if (Combat->CombatState == ECombatState::ECS_Unoccupied) ServerEquipButtonPressed();
bool bSwap = Combat->ShouldSwapWeapons() &&
!HasAuthority() &&
Combat->CombatState == ECombatState::ECS_Unoccupied &&
OverlappingWeapon == nullptr;
if (bSwap)
{
PlaySwapMontage();
Combat->CombatState = ECombatState::ECS_SwappingWeapons;
bFinishedSwapping = false;
}
2022-04-30 11:09:28 +00:00
}
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::ServerEquipButtonPressed_Implementation()
2022-05-09 20:19:20 +00:00
{
2022-05-24 10:46:30 +00:00
if (Combat)
{
2022-05-25 13:24:29 +00:00
if (OverlappingWeapon)
{
Combat->EquipWeapon(OverlappingWeapon);
}
else if (Combat->ShouldSwapWeapons())
{
Combat->SwapWeapons();
}
2022-05-24 10:46:30 +00:00
}
2022-05-09 20:19:20 +00:00
}
2022-04-30 15:10:07 +00:00
void ABlasterCharacter::CrouchButtonPressed()
{
2022-05-10 14:03:35 +00:00
if (bDisableGameplay) return;
2022-04-30 15:10:07 +00:00
if (bIsCrouched)
{
UnCrouch();
}
else
{
Crouch();
}
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::ReloadButtonPressed()
2022-04-30 15:38:03 +00:00
{
2022-05-10 14:03:35 +00:00
if (bDisableGameplay) return;
2022-04-30 15:38:03 +00:00
if (Combat)
{
2022-05-12 13:37:49 +00:00
Combat->Reload();
2022-04-30 15:38:03 +00:00
}
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::AimButtonPressed()
2022-04-30 15:38:03 +00:00
{
2022-05-10 14:03:35 +00:00
if (bDisableGameplay) return;
2022-04-30 15:38:03 +00:00
if (Combat)
{
2022-05-12 13:37:49 +00:00
Combat->SetAiming(true);
2022-04-30 15:38:03 +00:00
}
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::AimButtonReleased()
2022-05-10 14:03:35 +00:00
{
if (bDisableGameplay) return;
if (Combat)
{
2022-05-12 13:37:49 +00:00
Combat->SetAiming(false);
2022-05-10 14:03:35 +00:00
}
}
2022-05-21 10:13:03 +00:00
void ABlasterCharacter::GrenadeButtonPressed()
{
if (Combat)
{
Combat->ThrowGrenade();
}
}
2022-05-12 13:37:49 +00:00
float ABlasterCharacter::CalculateSpeed()
2022-05-10 14:03:35 +00:00
{
2022-05-12 13:37:49 +00:00
FVector Velocity = GetVelocity();
Velocity.Z = 0.f;
return Velocity.Size();
2022-05-10 14:03:35 +00:00
}
2022-05-03 21:14:04 +00:00
void ABlasterCharacter::AimOffset(float DeltaTime)
{
2022-05-24 10:46:30 +00:00
if (Combat && Combat->PrimaryWeapon == nullptr) return;
2022-05-12 13:37:49 +00:00
float Speed = CalculateSpeed();
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;
2022-05-12 13:37:49 +00:00
FRotator CurrentAimRotation = FRotator(0.f, GetBaseAimRotation().Yaw, 0.f);
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
if (AO_Pitch > 90.f && !IsLocallyControlled())
{
// map pitch from [270, 360) to [-90, 0)
2022-05-12 13:37:49 +00:00
FVector2D InRange(270.f, 360.f);
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()
{
2022-05-24 10:46:30 +00:00
if (Combat == nullptr || Combat->PrimaryWeapon == nullptr) return;
2022-05-06 10:43:18 +00:00
bRotateRootBone = false;
2022-05-12 13:37:49 +00:00
float Speed = CalculateSpeed();
2022-05-06 10:43:18 +00:00
if (Speed > 0.f)
{
TurningInPlace = ETurningInPlace::ETIP_NotTurning;
return;
}
2022-05-12 13:37:49 +00:00
2022-05-06 10:43:18 +00:00
ProxyRotationLastFrame = ProxyRotation;
ProxyRotation = GetActorRotation();
ProxyYaw = UKismetMathLibrary::NormalizedDeltaRotator(ProxyRotation, ProxyRotationLastFrame).Yaw;
2022-05-12 13:37:49 +00:00
2022-05-06 10:43:18 +00:00
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-12 13:37:49 +00:00
2022-05-06 10:43:18 +00:00
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::Jump()
2022-04-30 14:08:00 +00:00
{
2022-05-12 13:37:49 +00:00
if (bDisableGameplay) return;
if (bIsCrouched)
{
UnCrouch();
}
else
{
Super::Jump();
}
}
void ABlasterCharacter::FireButtonPressed()
{
if (bDisableGameplay) return;
if (Combat)
{
Combat->FireButtonPressed(true);
}
}
void ABlasterCharacter::FireButtonReleased()
{
if (bDisableGameplay) return;
if (Combat)
{
Combat->FireButtonPressed(false);
}
2022-04-30 14:08:00 +00:00
}
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-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);
2022-05-24 10:46:30 +00:00
if (Combat && Combat->PrimaryWeapon && Combat->PrimaryWeapon->GetWeaponMesh())
2022-05-05 21:03:09 +00:00
{
2022-05-24 10:46:30 +00:00
Combat->PrimaryWeapon->GetWeaponMesh()->bOwnerNoSee = true;
2022-05-05 21:03:09 +00:00
}
2022-05-25 13:24:29 +00:00
if (Combat && Combat->SecondaryWeapon && Combat->SecondaryWeapon->GetWeaponMesh())
{
Combat->SecondaryWeapon->GetWeaponMesh()->bOwnerNoSee = true;
}
2022-05-06 10:43:18 +00:00
}
else
2022-05-05 21:03:09 +00:00
{
GetMesh()->SetVisibility(true);
2022-05-24 10:46:30 +00:00
if (Combat && Combat->PrimaryWeapon && Combat->PrimaryWeapon->GetWeaponMesh())
2022-05-05 21:03:09 +00:00
{
2022-05-24 10:46:30 +00:00
Combat->PrimaryWeapon->GetWeaponMesh()->bOwnerNoSee = false;
2022-05-05 21:03:09 +00:00
}
2022-05-25 13:24:29 +00:00
if (Combat && Combat->SecondaryWeapon && Combat->SecondaryWeapon->GetWeaponMesh())
{
Combat->SecondaryWeapon->GetWeaponMesh()->bOwnerNoSee = false;
}
2022-05-05 21:03:09 +00:00
}
}
2022-05-22 22:16:27 +00:00
void ABlasterCharacter::OnRep_Health(float LastHealth)
2022-05-07 09:04:11 +00:00
{
2022-05-07 11:03:19 +00:00
UpdateHUDHealth();
2022-05-22 22:16:27 +00:00
if (Health < LastHealth)
2022-05-07 17:10:32 +00:00
{
PlayHitReactMontage();
}
2022-05-07 11:03:19 +00:00
}
2022-05-23 09:27:12 +00:00
void ABlasterCharacter::OnRep_Shield(float LastShield)
{
UpdateHUDShield();
if (Shield < LastShield)
{
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-05-23 09:27:12 +00:00
void ABlasterCharacter::UpdateHUDShield()
{
BlasterPlayerController = BlasterPlayerController == nullptr ? Cast<ABlasterPlayerController>(Controller) : BlasterPlayerController;
if (BlasterPlayerController)
{
BlasterPlayerController->SetHUDShield(Shield, MaxShield);
}
}
2022-05-23 23:16:06 +00:00
void ABlasterCharacter::UpdateHUDAmmo()
{
BlasterPlayerController = BlasterPlayerController == nullptr ? Cast<ABlasterPlayerController>(Controller) : BlasterPlayerController;
2022-05-24 10:46:30 +00:00
if (BlasterPlayerController && Combat && Combat->PrimaryWeapon)
2022-05-23 23:16:06 +00:00
{
BlasterPlayerController->SetHUDCarriedAmmo(Combat->CarriedAmmo);
2022-05-24 10:46:30 +00:00
BlasterPlayerController->SetHUDWeaponAmmo(Combat->PrimaryWeapon->GetAmmo());
2022-05-23 23:16:06 +00:00
}
}
void ABlasterCharacter::SpawnDefaultWeapon()
{
const ABlasterGameMode* BlasterGameMode = Cast<ABlasterGameMode>(UGameplayStatics::GetGameMode(this));
UWorld* World = GetWorld();
if (BlasterGameMode && World && !bEliminated && DefaultWeaponClass)
{
AWeapon* StartingWeapon = World->SpawnActor<AWeapon>(DefaultWeaponClass);
StartingWeapon->bDestroyWeapon = true;
if (Combat)
{
Combat->EquipWeapon(StartingWeapon);
}
}
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::PollInit()
{
if (BlasterPlayerState == nullptr)
{
BlasterPlayerState = GetPlayerState<ABlasterPlayerState>();
if (BlasterPlayerState)
{
// Initialize Score now we have the PlayerState
BlasterPlayerState->IncreaseScore(0.f);
BlasterPlayerState->IncreaseDefeats(0);
}
}
}
2022-05-08 16:35:37 +00:00
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();
}
}
2022-04-30 10:26:25 +00:00
void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(false);
}
OverlappingWeapon = Weapon;
if (IsLocallyControlled())
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
}
}
2022-05-12 13:37:49 +00:00
void ABlasterCharacter::OnRep_OverlappingWeapon(AWeapon* LastWeapon)
{
if (OverlappingWeapon)
{
OverlappingWeapon->ShowPickupWidget(true);
}
if (LastWeapon)
{
LastWeapon->ShowPickupWidget(false);
}
}
2022-04-30 14:48:34 +00:00
bool ABlasterCharacter::IsWeaponEquipped()
{
2022-05-24 10:46:30 +00:00
return Combat && Combat->PrimaryWeapon;
2022-04-30 14:48:34 +00:00
}
2022-04-30 15:38:03 +00:00
bool ABlasterCharacter::IsAiming()
{
return Combat && Combat->bAiming;
}
2022-05-24 10:46:30 +00:00
AWeapon* ABlasterCharacter::GetPrimaryWeapon()
2022-05-03 22:43:10 +00:00
{
if (Combat == nullptr) return nullptr;
2022-05-24 10:46:30 +00:00
return Combat->PrimaryWeapon;
2022-05-03 22:43:10 +00:00
}
2022-05-05 18:14:17 +00:00
FVector ABlasterCharacter::GetHitTarget() const
{
if (Combat == nullptr) return FVector();
return Combat->HitTarget;
}
2022-05-09 21:16:55 +00:00
ECombatState ABlasterCharacter::GetCombatState() const
{
if (Combat == nullptr) return ECombatState::ECS_MAX;
return Combat->CombatState;
2022-05-26 15:09:25 +00:00
}
bool ABlasterCharacter::IsLocallyReloading()
{
if (Combat == nullptr) return false;
return Combat->bLocallyReloading;
}