112 lines
2.6 KiB
C++
112 lines
2.6 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "CombatComponent.h"
|
|
|
|
#include "Blaster/Character/BlasterCharacter.h"
|
|
#include "Blaster/Weapon/Weapon.h"
|
|
#include "Engine/SkeletalMeshSocket.h"
|
|
#include "GameFramework/CharacterMovementComponent.h"
|
|
#include "Net/UnrealNetwork.h"
|
|
|
|
UCombatComponent::UCombatComponent()
|
|
{
|
|
PrimaryComponentTick.bCanEverTick = false;
|
|
|
|
BaseWalkSpeed = 600.f;
|
|
AimWalkSpeed = 350.f;
|
|
}
|
|
|
|
void UCombatComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
|
|
{
|
|
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
|
|
|
DOREPLIFETIME(UCombatComponent, EquippedWeapon);
|
|
DOREPLIFETIME(UCombatComponent, bAiming);
|
|
}
|
|
|
|
void UCombatComponent::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
if (Character)
|
|
{
|
|
Character->GetCharacterMovement()->MaxWalkSpeed = BaseWalkSpeed;
|
|
}
|
|
}
|
|
|
|
void UCombatComponent::SetAiming(bool bIsAiming)
|
|
{
|
|
bAiming = bIsAiming;
|
|
ServerSetAiming(bIsAiming);
|
|
if (Character)
|
|
{
|
|
Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed;
|
|
}
|
|
}
|
|
|
|
void UCombatComponent::ServerSetAiming_Implementation(bool bIsAiming)
|
|
{
|
|
bAiming = bIsAiming;
|
|
if (Character)
|
|
{
|
|
Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed;
|
|
}
|
|
}
|
|
|
|
void UCombatComponent::OnRep_EquippedWeapon()
|
|
{
|
|
if (EquippedWeapon && Character)
|
|
{
|
|
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
|
|
Character->bUseControllerRotationYaw = true;
|
|
}
|
|
}
|
|
|
|
void UCombatComponent::FireButtonPressed(bool bPressed)
|
|
{
|
|
bFireButtonPressed = bPressed;
|
|
|
|
if (bFireButtonPressed)
|
|
{
|
|
ServerFire();
|
|
}
|
|
}
|
|
|
|
void UCombatComponent::ServerFire_Implementation()
|
|
{
|
|
MulticastFire();
|
|
}
|
|
|
|
void UCombatComponent::MulticastFire_Implementation()
|
|
{
|
|
if (EquippedWeapon == nullptr) return;
|
|
if (Character)
|
|
{
|
|
Character->PlayFireMontage(bAiming);
|
|
EquippedWeapon->Fire();
|
|
}
|
|
}
|
|
|
|
void UCombatComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
|
{
|
|
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
|
|
|
}
|
|
|
|
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);
|
|
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
|
|
Character->bUseControllerRotationYaw = true;
|
|
}
|