blaster/Source/Blaster/Components/CombatComponent.cpp

184 lines
4.7 KiB
C++
Raw Normal View History

2022-04-30 11:09:28 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "CombatComponent.h"
#include "Blaster/Character/BlasterCharacter.h"
2022-05-05 15:57:57 +00:00
#include "Blaster/HUD/BlasterHUD.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
2022-04-30 11:09:28 +00:00
#include "Blaster/Weapon/Weapon.h"
#include "Engine/SkeletalMeshSocket.h"
2022-05-02 13:40:33 +00:00
#include "GameFramework/CharacterMovementComponent.h"
2022-05-04 22:39:17 +00:00
#include "Kismet/GameplayStatics.h"
2022-04-30 14:48:34 +00:00
#include "Net/UnrealNetwork.h"
2022-04-30 11:09:28 +00:00
UCombatComponent::UCombatComponent()
{
2022-05-04 22:39:17 +00:00
PrimaryComponentTick.bCanEverTick = true;
2022-05-03 17:50:47 +00:00
BaseWalkSpeed = 600.f;
2022-05-04 12:57:48 +00:00
AimWalkSpeed = 350.f;
2022-04-30 11:09:28 +00:00
}
2022-05-04 20:57:45 +00:00
void UCombatComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UCombatComponent, EquippedWeapon);
DOREPLIFETIME(UCombatComponent, bAiming);
}
2022-04-30 11:09:28 +00:00
void UCombatComponent::BeginPlay()
{
Super::BeginPlay();
2022-05-03 17:50:47 +00:00
if (Character)
{
Character->GetCharacterMovement()->MaxWalkSpeed = BaseWalkSpeed;
}
2022-04-30 11:09:28 +00:00
}
2022-05-05 15:57:57 +00:00
void UCombatComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
SetHUDCrosshairs(DeltaTime);
}
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)
{
FHUDPackage HUDPackage;
if (EquippedWeapon)
{
HUDPackage.CrosshairsCenter = EquippedWeapon->CrosshairsCenter;
HUDPackage.CrosshairsLeft = EquippedWeapon->CrosshairsLeft;
HUDPackage.CrosshairsRight = EquippedWeapon->CrosshairsRight;
HUDPackage.CrosshairsTop = EquippedWeapon->CrosshairsTop;
HUDPackage.CrosshairsBottom = EquippedWeapon->CrosshairsBottom;
}
else
{
HUDPackage.CrosshairsCenter = nullptr;
HUDPackage.CrosshairsLeft = nullptr;
HUDPackage.CrosshairsRight = nullptr;
HUDPackage.CrosshairsTop = nullptr;
HUDPackage.CrosshairsBottom = nullptr;
}
HUD->SetHUDPackage(HUDPackage);
}
}
}
2022-04-30 15:38:03 +00:00
void UCombatComponent::SetAiming(bool bIsAiming)
{
bAiming = bIsAiming;
ServerSetAiming(bIsAiming);
2022-05-03 17:50:47 +00:00
if (Character)
{
Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed;
}
}
void UCombatComponent::ServerSetAiming_Implementation(bool bIsAiming)
{
bAiming = bIsAiming;
if (Character)
{
Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed;
}
2022-04-30 15:38:03 +00:00
}
2022-05-02 13:40:33 +00:00
void UCombatComponent::OnRep_EquippedWeapon()
{
if (EquippedWeapon && Character)
{
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
}
}
2022-05-04 20:13:31 +00:00
void UCombatComponent::FireButtonPressed(bool bPressed)
{
bFireButtonPressed = bPressed;
2022-05-04 20:57:45 +00:00
if (bFireButtonPressed)
{
2022-05-05 12:29:21 +00:00
FHitResult HitResult;
TraceUnderCrosshairs(HitResult);
ServerFire(HitResult.ImpactPoint);
2022-05-04 20:57:45 +00:00
}
}
2022-05-04 22:39:17 +00:00
void UCombatComponent::TraceUnderCrosshairs(FHitResult& TraceHitResult)
{
FVector2d ViewPortSize;
if (GEngine && GEngine->GameViewport)
{
GEngine->GameViewport->GetViewportSize(ViewPortSize);
}
FVector2d CrosshairLocation(ViewPortSize.X / 2.f, ViewPortSize.Y / 2.0f);
FVector CrosshairWorldPosition;
FVector CrosshairWorldDirection;
bool bScreenToWorld = UGameplayStatics::DeprojectScreenToWorld(
UGameplayStatics::GetPlayerController(this, 0),
CrosshairLocation,
CrosshairWorldPosition,
CrosshairWorldDirection
);
if (bScreenToWorld)
{
FVector Start = CrosshairWorldPosition;
FVector End = Start + CrosshairWorldDirection * TRACE_LENGTH;
GetWorld()->LineTraceSingleByChannel(
TraceHitResult,
Start,
End,
ECollisionChannel::ECC_Visibility
);
}
}
2022-05-05 12:29:21 +00:00
void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize& TraceHitTarget)
2022-05-04 20:57:45 +00:00
{
2022-05-05 12:29:21 +00:00
MulticastFire(TraceHitTarget);
2022-05-04 20:57:45 +00:00
}
2022-05-05 12:29:21 +00:00
void UCombatComponent::MulticastFire_Implementation(const FVector_NetQuantize& TraceHitTarget)
2022-05-04 20:57:45 +00:00
{
2022-05-04 20:27:26 +00:00
if (EquippedWeapon == nullptr) return;
2022-05-04 20:57:45 +00:00
if (Character)
2022-05-04 20:13:31 +00:00
{
Character->PlayFireMontage(bAiming);
2022-05-05 12:29:21 +00:00
EquippedWeapon->Fire(TraceHitTarget);
2022-05-04 20:13:31 +00:00
}
}
2022-04-30 11:09:28 +00:00
void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
{
if (Character == nullptr || WeaponToEquip == nullptr) return;
EquippedWeapon = WeaponToEquip;
EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(FName("RightHandSocket"));
if (HandSocket)
{
HandSocket->AttachActor(EquippedWeapon, Character->GetMesh());
}
EquippedWeapon->SetOwner(Character);
2022-05-02 13:40:33 +00:00
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
2022-04-30 11:09:28 +00:00
}