blaster/Source/Blaster/Weapon/HitScanWeapon.cpp

78 lines
1.8 KiB
C++
Raw Normal View History

2022-05-19 17:13:21 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "HitScanWeapon.h"
#include "Blaster/Character/BlasterCharacter.h"
#include "Engine/SkeletalMeshSocket.h"
#include "Kismet/GameplayStatics.h"
2022-05-19 17:35:20 +00:00
#include "Particles/ParticleSystemComponent.h"
2022-05-19 17:13:21 +00:00
void AHitScanWeapon::Fire(const FVector& HitTarget)
{
Super::Fire(HitTarget);
APawn* OwnerPawn = Cast<APawn>(GetOwner());
if (OwnerPawn == nullptr) return;
AController* InstigatorController = OwnerPawn->GetController();
const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash");
2022-05-19 17:35:20 +00:00
if (MuzzleFlashSocket)
2022-05-19 17:13:21 +00:00
{
FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
FVector Start = SocketTransform.GetLocation();
FVector End = Start + (HitTarget - Start) * 1.25f;
FHitResult FireHit;
UWorld* World = GetWorld();
if (World)
{
World->LineTraceSingleByChannel(
FireHit,
Start,
End,
ECC_Visibility
);
2022-05-19 17:35:20 +00:00
FVector BeamEnd = End;
2022-05-19 17:13:21 +00:00
if (FireHit.bBlockingHit)
{
2022-05-19 17:35:20 +00:00
BeamEnd = FireHit.ImpactPoint;
2022-05-19 17:13:21 +00:00
ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(FireHit.GetActor());
2022-05-19 17:35:20 +00:00
if (BlasterCharacter && HasAuthority() && InstigatorController)
2022-05-19 17:13:21 +00:00
{
2022-05-19 17:35:20 +00:00
UGameplayStatics::ApplyDamage(
BlasterCharacter,
Damage,
InstigatorController,
this,
UDamageType::StaticClass()
);
2022-05-19 17:13:21 +00:00
}
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(
World,
ImpactParticles,
FireHit.ImpactPoint,
FireHit.ImpactNormal.Rotation()
);
}
}
2022-05-19 17:35:20 +00:00
if (BeamParticles)
{
UParticleSystemComponent* Beam = UGameplayStatics::SpawnEmitterAtLocation(
World,
BeamParticles,
SocketTransform
);
if (Beam)
{
Beam->SetVectorParameter(FName("Target"), BeamEnd);
}
}
2022-05-19 17:13:21 +00:00
}
}
}