blaster/Source/Blaster/Components/CombatComponent.cpp

869 lines
23 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "CombatComponent.h"
#include "Blaster/Character/BlasterCharacter.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Blaster/Weapon/Shotgun.h"
#include "Blaster/Weapon/Weapon.h"
#include "Camera/CameraComponent.h"
#include "Engine/SkeletalMeshSocket.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Net/UnrealNetwork.h"
UCombatComponent::UCombatComponent()
{
PrimaryComponentTick.bCanEverTick = true;
BaseWalkSpeed = 600.f;
AimWalkSpeed = 350.f;
}
void UCombatComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UCombatComponent, EquippedWeapon);
DOREPLIFETIME(UCombatComponent, SecondaryWeapon);
DOREPLIFETIME(UCombatComponent, bAiming);
DOREPLIFETIME_CONDITION(UCombatComponent, CarriedAmmo, COND_OwnerOnly);
DOREPLIFETIME(UCombatComponent, CombatState);
DOREPLIFETIME(UCombatComponent, Grenades);
}
void UCombatComponent::ShotgunShellReload()
{
if (Character && Character->HasAuthority())
{
UpdateShotgunAmmoValues();
}
}
void UCombatComponent::PickupAmmo(EWeaponType WeaponType, int32 AmmoAmount)
{
if (CarriedAmmoMap.Contains(WeaponType))
{
CarriedAmmoMap[WeaponType] = FMath::Clamp(CarriedAmmoMap[WeaponType] + AmmoAmount, 0, MaxCarriedAmmo);
UpdateCarriedAmmo();
}
if (EquippedWeapon && EquippedWeapon->IsEmpty() && EquippedWeapon->GetWeaponType() == WeaponType)
{
Reload();
}
}
void UCombatComponent::BeginPlay()
{
Super::BeginPlay();
if (Character)
{
Character->GetCharacterMovement()->MaxWalkSpeed = BaseWalkSpeed;
if (Character->GetFollowCamera())
{
DefaultFOV = Character->GetFollowCamera()->FieldOfView;
CurrentFOV = DefaultFOV;
}
if (Character->HasAuthority())
{
InitializeCarriedAmmo();
}
}
}
void UCombatComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (Character && Character->IsLocallyControlled())
{
FHitResult HitResult;
TraceUnderCrosshairs(HitResult);
HitTarget = HitResult.ImpactPoint;
SetHUDCrosshairs(DeltaTime);
InterpFOV(DeltaTime);
}
}
void UCombatComponent::FireButtonPressed(bool bPressed)
{
bFireButtonPressed = bPressed;
if (bFireButtonPressed)
{
Fire();
}
}
void UCombatComponent::Fire()
{
if (CanFire())
{
bCanFire = false;
if (EquippedWeapon)
{
CrosshairShootingFactor = .75f;
switch (EquippedWeapon->FireType)
{
case EFireType::EFT_HitScan:
FireHitScanWeapon();
break;
case EFireType::EFT_Projectile:
FireProjectileWeapon();
break;
case EFireType::EFT_Shotgun:
FireShotgun();
break;
}
}
StartFireTimer();
}
}
void UCombatComponent::FireProjectileWeapon()
{
if (EquippedWeapon && Character)
{
HitTarget = EquippedWeapon->bUseScatter ? EquippedWeapon->TraceEndWithScatter(HitTarget) : HitTarget;
if (!Character->HasAuthority()) LocalFire(HitTarget);
ServerFire(HitTarget, EquippedWeapon->FireDelay);
}
}
void UCombatComponent::FireHitScanWeapon()
{
if (EquippedWeapon && Character)
{
HitTarget = EquippedWeapon->bUseScatter ? EquippedWeapon->TraceEndWithScatter(HitTarget) : HitTarget;
if (!Character->HasAuthority()) LocalFire(HitTarget);
ServerFire(HitTarget, EquippedWeapon->FireDelay);
}
}
void UCombatComponent::FireShotgun()
{
AShotgun* Shotgun = Cast<AShotgun>(EquippedWeapon);
if (Shotgun && Character)
{
TArray<FVector_NetQuantize> HitTargets;
Shotgun->ShotgunTraceEndWithScatter(HitTarget, HitTargets);
if (!Character->HasAuthority()) ShotgunLocalFire(HitTargets);
ServerShotgunFire(HitTargets, EquippedWeapon->FireDelay);
}
}
void UCombatComponent::StartFireTimer()
{
if (EquippedWeapon == nullptr || Character == nullptr) return;
Character->GetWorldTimerManager().SetTimer(
FireTimer,
this,
&UCombatComponent::FireTimerFinished,
EquippedWeapon->FireDelay
);
}
void UCombatComponent::FireTimerFinished()
{
if (EquippedWeapon == nullptr) return;
bCanFire = true;
if (bFireButtonPressed && EquippedWeapon->bAutomatic)
{
Fire();
}
ReloadEmptyWeapon();
}
void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize& TraceHitTarget, float FireDelay)
{
MulticastFire(TraceHitTarget);
}
bool UCombatComponent::ServerFire_Validate(const FVector_NetQuantize& TraceHitTarget, float FireDelay)
{
if (EquippedWeapon == nullptr) return true;
return FMath::IsNearlyEqual(EquippedWeapon->FireDelay, FireDelay, 0.001f);
}
void UCombatComponent::MulticastFire_Implementation(const FVector_NetQuantize& TraceHitTarget)
{
if (Character && Character->IsLocallyControlled() && !Character->HasAuthority()) return;
LocalFire(TraceHitTarget);
}
void UCombatComponent::ServerShotgunFire_Implementation(const TArray<FVector_NetQuantize>& TraceHitTargets, float FireDelay)
{
MulticastShotgunFire(TraceHitTargets);
}
bool UCombatComponent::ServerShotgunFire_Validate(const TArray<FVector_NetQuantize>& TraceHitTargets, float FireDelay)
{
if (EquippedWeapon == nullptr) return true;
return FMath::IsNearlyEqual(EquippedWeapon->FireDelay, FireDelay, 0.001f);
}
void UCombatComponent::MulticastShotgunFire_Implementation(const TArray<FVector_NetQuantize>& TraceHitTargets)
{
if (Character && Character->IsLocallyControlled() && !Character->HasAuthority()) return;
ShotgunLocalFire(TraceHitTargets);
}
void UCombatComponent::LocalFire(const FVector_NetQuantize& TraceHitTarget)
{
if (EquippedWeapon == nullptr) return;
if (Character && CombatState == ECombatState::ECS_Unoccupied)
{
Character->PlayFireMontage(bAiming);
EquippedWeapon->Fire(TraceHitTarget);
}
}
void UCombatComponent::ShotgunLocalFire(const TArray<FVector_NetQuantize>& TraceHitTargets)
{
AShotgun* Shotgun = Cast<AShotgun>(EquippedWeapon);
if (Shotgun == nullptr || Character == nullptr) return;
if (CombatState == ECombatState::ECS_Reloading || CombatState == ECombatState::ECS_Unoccupied)
{
bLocallyReloading = false;
Character->PlayFireMontage(bAiming);
Shotgun->FireShotgun(TraceHitTargets);
CombatState = ECombatState::ECS_Unoccupied;
}
}
void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
{
if (Character == nullptr || WeaponToEquip == nullptr) return;
if (CombatState != ECombatState::ECS_Unoccupied) return;
if (EquippedWeapon != nullptr && SecondaryWeapon == nullptr)
{
EquipSecondaryWeapon(WeaponToEquip);
}
else
{
EquipPrimaryWeapon(WeaponToEquip);
}
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
}
void UCombatComponent::SwapWeapons()
{
if (CombatState != ECombatState::ECS_Unoccupied || Character == nullptr || !Character->HasAuthority()) return;
Character->PlaySwapMontage();
CombatState = ECombatState::ECS_SwappingWeapons;
Character->bFinishedSwapping = false;
if (SecondaryWeapon) SecondaryWeapon->EnableCustomDepth(false);
}
void UCombatComponent::EquipPrimaryWeapon(AWeapon* WeaponToEquip)
{
if (WeaponToEquip == nullptr) return;
DropEquippedWeapon();
EquippedWeapon = WeaponToEquip;
EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(EquippedWeapon);
EquippedWeapon->SetOwner(Character);
EquippedWeapon->SetHUDAmmo();
UpdateCarriedAmmo();
PlayEquipWeaponSound(WeaponToEquip);
ReloadEmptyWeapon();
}
void UCombatComponent::EquipSecondaryWeapon(AWeapon* WeaponToEquip)
{
if (WeaponToEquip == nullptr) return;
SecondaryWeapon = WeaponToEquip;
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_EquippedSecondary);
AttachActorToBackpack(WeaponToEquip);
PlayEquipWeaponSound(WeaponToEquip);
SecondaryWeapon->SetOwner(Character);
}
void UCombatComponent::OnRep_Aiming()
{
if (Character && Character->IsLocallyControlled())
{
bAiming = bAimButtonPressed;
}
}
void UCombatComponent::DropEquippedWeapon()
{
if (EquippedWeapon)
{
EquippedWeapon->Dropped();
}
}
void UCombatComponent::AttachActorToRightHand(AActor* ActorToAttach)
{
if (Character == nullptr || Character->GetMesh() == nullptr || ActorToAttach == nullptr) return;
const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(FName("RightHandSocket"));
if (HandSocket)
{
HandSocket->AttachActor(ActorToAttach, Character->GetMesh());
}
}
void UCombatComponent::AttachActorToLeftHand(AActor* ActorToAttach)
{
if (Character == nullptr || Character->GetMesh() == nullptr || ActorToAttach == nullptr || EquippedWeapon == nullptr) return;
bool bUsePistolSocket = EquippedWeapon->IsPistol() || EquippedWeapon->IsSMG();
FName SocketName = bUsePistolSocket ? FName("LeftHandPistolSocket") : FName("LeftHandSocket");
const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(SocketName);
if (HandSocket)
{
HandSocket->AttachActor(ActorToAttach, Character->GetMesh());
}
}
void UCombatComponent::AttachActorToBackpack(AActor* ActorToAttach)
{
if (Character == nullptr || Character->GetMesh() == nullptr || ActorToAttach == nullptr) return;
const USkeletalMeshSocket* BackpackSocket = Character->GetMesh()->GetSocketByName(FName("BackpackSocket"));
if (BackpackSocket)
{
BackpackSocket->AttachActor(ActorToAttach, Character->GetMesh());
}
}
void UCombatComponent::UpdateCarriedAmmo()
{
if (EquippedWeapon == nullptr) return;
if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType()))
{
CarriedAmmo = CarriedAmmoMap[EquippedWeapon->GetWeaponType()];
}
Controller = Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
if (Controller)
{
Controller->SetHUDCarriedAmmo(CarriedAmmo);
}
}
void UCombatComponent::PlayEquipWeaponSound(const AWeapon* WeaponToEquip)
{
if (Character && WeaponToEquip && WeaponToEquip->EquipSound)
{
UGameplayStatics::PlaySoundAtLocation(
this,
WeaponToEquip->EquipSound,
Character->GetActorLocation()
);
}
}
void UCombatComponent::ReloadEmptyWeapon()
{
if (EquippedWeapon && EquippedWeapon->IsEmpty())
{
Reload();
}
}
void UCombatComponent::Reload()
{
if (CarriedAmmo > 0 && CombatState == ECombatState::ECS_Unoccupied && EquippedWeapon && !EquippedWeapon->IsFull() && !bLocallyReloading)
{
ServerReload();
HandleReload();
bLocallyReloading = true;
}
}
void UCombatComponent::ServerReload_Implementation()
{
if (Character == nullptr || EquippedWeapon == nullptr) return;
// return if weapon mag is at max capacity
if (EquippedWeapon->GetAmmo() == EquippedWeapon->GetMagCapacity()) return;
CombatState = ECombatState::ECS_Reloading;
if (!Character->IsLocallyControlled()) HandleReload();
}
void UCombatComponent::FinishedReloading()
{
if (Character == nullptr) return;
bLocallyReloading = false;
if (Character->HasAuthority())
{
CombatState = ECombatState::ECS_Unoccupied;
UpdateAmmoValues();
}
if (bFireButtonPressed)
{
Fire();
}
}
void UCombatComponent::FinishedSwap()
{
if (Character && Character->HasAuthority())
{
CombatState = ECombatState::ECS_Unoccupied;
}
if (Character) Character->bFinishedSwapping = true;
if (SecondaryWeapon) SecondaryWeapon->EnableCustomDepth(true);
}
void UCombatComponent::FinishedSwapAttachWeapons()
{
PlayEquipWeaponSound(SecondaryWeapon);
if (Character == nullptr || !Character->HasAuthority()) return;
AWeapon* TempWeapon = EquippedWeapon;
EquippedWeapon = SecondaryWeapon;
SecondaryWeapon = TempWeapon;
EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(EquippedWeapon);
EquippedWeapon->SetHUDAmmo();
UpdateCarriedAmmo();
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_EquippedSecondary);
AttachActorToBackpack(SecondaryWeapon);
}
void UCombatComponent::DropWeapons()
{
if (EquippedWeapon) EquippedWeapon->Dropped();
if (SecondaryWeapon) SecondaryWeapon->Dropped();
}
void UCombatComponent::UpdateAmmoValues()
{
if (Character == nullptr || EquippedWeapon == nullptr) return;
int32 ReloadAmount = AmountToReload();
if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType()))
{
CarriedAmmoMap[EquippedWeapon->GetWeaponType()] -= ReloadAmount;
CarriedAmmo = CarriedAmmoMap[EquippedWeapon->GetWeaponType()];
}
Controller = Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
if (Controller)
{
Controller->SetHUDCarriedAmmo(CarriedAmmo);
}
EquippedWeapon->AddAmmo(ReloadAmount);
}
void UCombatComponent::UpdateShotgunAmmoValues()
{
if (Character == nullptr || EquippedWeapon == nullptr) return;
if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType()))
{
CarriedAmmoMap[EquippedWeapon->GetWeaponType()] -= 1;
CarriedAmmo = CarriedAmmoMap[EquippedWeapon->GetWeaponType()];
}
Controller = Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
if (Controller)
{
Controller->SetHUDCarriedAmmo(CarriedAmmo);
}
EquippedWeapon->AddAmmo(1);
bCanFire = true;
if (EquippedWeapon->IsFull() || CarriedAmmo == 0)
{
JumpToShotgunEnd();
}
}
void UCombatComponent::OnRep_Grenades()
{
UpdateHUDGrenades();
}
void UCombatComponent::JumpToShotgunEnd()
{
// Jump to ShotgunEnd section
UAnimInstance* AnimInstance = Character->GetMesh()->GetAnimInstance();
if (AnimInstance && Character->GetReloadMontage())
{
AnimInstance->Montage_JumpToSection(FName("ShotgunEnd"));
}
}
void UCombatComponent::ThrowGrenadeFinished()
{
CombatState = ECombatState::ECS_Unoccupied;
AttachActorToRightHand(EquippedWeapon);
}
void UCombatComponent::LaunchGrenade()
{
ShowAttachedGrenade(false);
if (Character && Character->IsLocallyControlled())
{
ServerLaunchGrenade(HitTarget);
}
}
void UCombatComponent::ServerLaunchGrenade_Implementation(const FVector_NetQuantize& Target)
{
if (Character && GrenadeClass && Character->GetAttachedGrenade())
{
const FVector StartingLocation = Character->GetAttachedGrenade()->GetComponentLocation();
FVector ToTarget = Target - StartingLocation;
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = Character;
SpawnParams.Instigator = Character;
UWorld* World = GetWorld();
if (World)
{
World->SpawnActor<AProjectile>(
GrenadeClass,
StartingLocation + 10.f,
ToTarget.Rotation(),
SpawnParams
);
}
}
}
void UCombatComponent::OnRep_CombatState()
{
switch (CombatState)
{
case ECombatState::ECS_Reloading:
if (Character && !Character->IsLocallyControlled()) HandleReload();
break;
case ECombatState::ECS_Unoccupied:
if (bFireButtonPressed)
{
Fire();
}
break;
case ECombatState::ECS_ThrowingGrenade:
if (Character && !Character->IsLocallyControlled())
{
Character->PlayThrowGrenadeMontage();
AttachActorToLeftHand(EquippedWeapon);
ShowAttachedGrenade(true);
}
break;
case ECombatState::ECS_SwappingWeapons:
if (Character && !Character->IsLocallyControlled())
{
Character->PlaySwapMontage();
}
break;
}
}
void UCombatComponent::HandleReload()
{
if (Character)
{
Character->PlayReloadMontage();
}
}
int32 UCombatComponent::AmountToReload()
{
if (EquippedWeapon == nullptr) return 0;
int32 RoomInMag = EquippedWeapon->GetMagCapacity() - EquippedWeapon->GetAmmo();
if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType()))
{
int32 AmountCarried = CarriedAmmoMap[EquippedWeapon->GetWeaponType()];
int32 Least = FMath::Min(RoomInMag, AmountCarried);
return FMath::Clamp(RoomInMag, 0, Least);
}
return 0;
}
void UCombatComponent::ThrowGrenade()
{
if (Grenades == 0) return;
if (CombatState != ECombatState::ECS_Unoccupied || EquippedWeapon == nullptr) return;
CombatState = ECombatState::ECS_ThrowingGrenade;
if (Character)
{
Character->PlayThrowGrenadeMontage();
AttachActorToLeftHand(EquippedWeapon);
ShowAttachedGrenade(true);
}
if (Character && !Character->HasAuthority())
{
ServerThrowGrenade();
}
if (Character && Character->HasAuthority())
{
Grenades = FMath::Clamp(Grenades -1, 0, MaxGrenades);
UpdateHUDGrenades();
}
}
void UCombatComponent::ServerThrowGrenade_Implementation()
{
if (Grenades == 0) return;
CombatState = ECombatState::ECS_ThrowingGrenade;
if (Character)
{
Character->PlayThrowGrenadeMontage();
AttachActorToLeftHand(EquippedWeapon);
ShowAttachedGrenade(true);
}
Grenades = FMath::Clamp(Grenades -1, 0, MaxGrenades);
UpdateHUDGrenades();
}
void UCombatComponent::UpdateHUDGrenades()
{
Controller = Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
if (Controller)
{
Controller->SetHUDGrenades(Grenades);
}
}
bool UCombatComponent::ShouldSwapWeapons()
{
return EquippedWeapon != nullptr && SecondaryWeapon != nullptr;
}
void UCombatComponent::ShowAttachedGrenade(bool bShowGrenade)
{
if (Character && Character->GetAttachedGrenade())
{
Character->GetAttachedGrenade()->SetVisibility(bShowGrenade);
}
}
void UCombatComponent::OnRep_EquippedWeapon()
{
if (EquippedWeapon && Character)
{
EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(EquippedWeapon);
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
PlayEquipWeaponSound(EquippedWeapon);
EquippedWeapon->EnableCustomDepth(false);
EquippedWeapon->SetHUDAmmo();
}
}
void UCombatComponent::OnRep_SecondaryWeapon()
{
if (SecondaryWeapon && Character)
{
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_EquippedSecondary);
AttachActorToBackpack(SecondaryWeapon);
PlayEquipWeaponSound(EquippedWeapon);
}
}
void UCombatComponent::TraceUnderCrosshairs(FHitResult& TraceHitResult)
{
FVector2D ViewportSize;
if (GEngine && GEngine->GameViewport)
{
GEngine->GameViewport->GetViewportSize(ViewportSize);
}
FVector2D CrosshairLocation(ViewportSize.X / 2.f, ViewportSize.Y / 2.f);
FVector CrosshairWorldPosition;
FVector CrosshairWorldDirection;
bool bScreenToWorld = UGameplayStatics::DeprojectScreenToWorld(
UGameplayStatics::GetPlayerController(this, 0),
CrosshairLocation,
CrosshairWorldPosition,
CrosshairWorldDirection
);
if (bScreenToWorld)
{
FVector Start = CrosshairWorldPosition;
if (Character)
{
float DistanceToCharacter = (Character->GetActorLocation() - Start).Size();
Start += CrosshairWorldDirection * (DistanceToCharacter + 100.f);
}
FVector End = Start + CrosshairWorldDirection * TRACE_LENGTH;
GetWorld()->LineTraceSingleByChannel(
TraceHitResult,
Start,
End,
ECollisionChannel::ECC_Visibility
);
if (TraceHitResult.GetActor() && TraceHitResult.GetActor()->Implements<UInteractWithCrosshairInterface>())
{
HUDPackage.CrosshairsColor = FLinearColor::Red;
}
else
{
HUDPackage.CrosshairsColor = FLinearColor::White;
}
if (!TraceHitResult.bBlockingHit) TraceHitResult.ImpactPoint = End;
}
}
void UCombatComponent::SetHUDCrosshairs(float DeltaTime)
{
if (Character == nullptr || Character->Controller == nullptr) return;
Controller = Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
if (Controller)
{
HUD = HUD == nullptr ? Cast<ABlasterHUD>(Controller->GetHUD()) : HUD;
if (HUD)
{
if (EquippedWeapon)
{
HUDPackage.CrosshairsCenter = EquippedWeapon->CrosshairsCenter;
HUDPackage.CrosshairsLeft = EquippedWeapon->CrosshairsLeft;
HUDPackage.CrosshairsRight = EquippedWeapon->CrosshairsRight;
HUDPackage.CrosshairsBottom = EquippedWeapon->CrosshairsBottom;
HUDPackage.CrosshairsTop = EquippedWeapon->CrosshairsTop;
}
else
{
HUDPackage.CrosshairsCenter = nullptr;
HUDPackage.CrosshairsLeft = nullptr;
HUDPackage.CrosshairsRight = nullptr;
HUDPackage.CrosshairsBottom = nullptr;
HUDPackage.CrosshairsTop = nullptr;
}
// Calculate crosshair spread
// [0, 600] -> [0, 1]
FVector2D WalkSpeedRange(0.f, Character->GetCharacterMovement()->MaxWalkSpeed);
FVector2D VelocityMultiplierRange(0.f, 1.f);
FVector Velocity = Character->GetVelocity();
Velocity.Z = 0.f;
CrosshairVelocityFactor = FMath::GetMappedRangeValueClamped(WalkSpeedRange, VelocityMultiplierRange, Velocity.Size());
if (Character->GetCharacterMovement()->IsFalling())
{
CrosshairInAirFactor = FMath::FInterpTo(CrosshairInAirFactor, 2.25f, DeltaTime, 2.25f);
}
else
{
CrosshairInAirFactor = FMath::FInterpTo(CrosshairInAirFactor, 0.f, DeltaTime, 30.f);
}
if (bAiming)
{
CrosshairAimFactor = FMath::FInterpTo(CrosshairAimFactor, 0.58f, DeltaTime, 30.f);
}
else
{
CrosshairAimFactor = FMath::FInterpTo(CrosshairAimFactor, 0.f, DeltaTime, 30.f);
}
CrosshairShootingFactor = FMath::FInterpTo(CrosshairShootingFactor, 0.f, DeltaTime, 40.f);
HUDPackage.CrosshairSpread =
0.5f +
CrosshairVelocityFactor +
CrosshairInAirFactor -
CrosshairAimFactor +
CrosshairShootingFactor;
HUD->SetHUDPackage(HUDPackage);
}
}
}
void UCombatComponent::InterpFOV(float DeltaTime)
{
if (EquippedWeapon == nullptr) return;
if (bAiming)
{
CurrentFOV = FMath::FInterpTo(CurrentFOV, EquippedWeapon->GetZoomedFOV(), DeltaTime, EquippedWeapon->GetZoomInterpSpeed());
}
else
{
CurrentFOV = FMath::FInterpTo(CurrentFOV, DefaultFOV, DeltaTime, ZoomInterpSpeed);
}
if (Character && Character->GetFollowCamera())
{
Character->GetFollowCamera()->SetFieldOfView(CurrentFOV);
}
}
void UCombatComponent::SetAiming(bool bIsAiming)
{
if (Character == nullptr || EquippedWeapon == nullptr) return;
bAiming = bIsAiming;
ServerSetAiming(bIsAiming);
if (Character)
{
Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed;
}
if (Character->IsLocallyControlled() && EquippedWeapon->IsSniper())
{
Character->ShowSniperScopeWidget(bIsAiming);
}
if (Character->IsLocallyControlled()) bAimButtonPressed = bIsAiming;
}
void UCombatComponent::ServerSetAiming_Implementation(bool bIsAiming)
{
bAiming = bIsAiming;
if (Character)
{
Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed;
}
}
bool UCombatComponent::CanFire()
{
if (EquippedWeapon == nullptr) return false;
if (EquippedWeapon->IsEmpty()) return false;
if (!bCanFire) return false;
if (CombatState == ECombatState::ECS_Reloading && EquippedWeapon->GetWeaponType() == EWeaponType::EWT_Shotgun) return true;
if (bLocallyReloading) return false;
return CombatState == ECombatState::ECS_Unoccupied;
}
void UCombatComponent::OnRep_CarriedAmmo()
{
if (Character == nullptr) return;
Controller = Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
if (Controller)
{
Controller->SetHUDCarriedAmmo(CarriedAmmo);
}
bool bJumpToShotgunEnd = CombatState == ECombatState::ECS_Reloading &&
EquippedWeapon != nullptr &&
EquippedWeapon->GetWeaponType() == EWeaponType::EWT_Shotgun &&
CarriedAmmo == 0;
if (bJumpToShotgunEnd)
{
JumpToShotgunEnd();
}
}
void UCombatComponent::InitializeCarriedAmmo()
{
CarriedAmmoMap.Emplace(EWeaponType::EWT_AssaultRifle, StartingARAmmo);
CarriedAmmoMap.Emplace(EWeaponType::EWT_RocketLauncher, StartingRocketAmmo);
CarriedAmmoMap.Emplace(EWeaponType::EWT_Pistol, StartingPistolAmmo);
CarriedAmmoMap.Emplace(EWeaponType::EWT_SubmachineGun, StartingSMGAmmo);
CarriedAmmoMap.Emplace(EWeaponType::EWT_Shotgun, StartingShotgunAmmo);
CarriedAmmoMap.Emplace(EWeaponType::EWT_SniperRifle, StartingSniperAmmo);
CarriedAmmoMap.Emplace(EWeaponType::EWT_GrenadeLauncher, StartingGrenadeLauncherAmmo);
}