blaster/Source/Blaster/Weapon/Projectile.cpp

128 lines
2.7 KiB
C++
Raw Normal View History

2022-05-04 17:17:25 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "Projectile.h"
2022-05-20 16:12:50 +00:00
#include "NiagaraFunctionLibrary.h"
2022-05-05 22:41:35 +00:00
#include "Blaster/Blaster.h"
2022-05-04 17:17:25 +00:00
#include "Components/BoxComponent.h"
2022-05-05 12:15:25 +00:00
#include "Kismet/GameplayStatics.h"
2022-05-05 13:02:47 +00:00
#include "Sound/SoundCue.h"
2022-05-04 17:17:25 +00:00
AProjectile::AProjectile()
{
PrimaryActorTick.bCanEverTick = true;
2022-05-05 12:15:25 +00:00
bReplicates = true;
2022-05-04 17:17:25 +00:00
2022-05-05 12:15:25 +00:00
CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
2022-05-04 17:17:25 +00:00
SetRootComponent(CollisionBox);
2022-05-05 22:41:35 +00:00
CollisionBox->SetCollisionObjectType(ECC_WorldDynamic);
2022-05-04 17:17:25 +00:00
CollisionBox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
2022-05-05 22:41:35 +00:00
CollisionBox->SetCollisionResponseToChannels(ECR_Ignore);
CollisionBox->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
CollisionBox->SetCollisionResponseToChannel(ECC_WorldStatic, ECR_Block);
CollisionBox->SetCollisionResponseToChannel(ECC_SkeletalMesh, ECR_Block);
2022-05-04 17:17:25 +00:00
}
void AProjectile::BeginPlay()
{
Super::BeginPlay();
2022-05-05 12:15:25 +00:00
if (Tracer)
{
TracerComponent = UGameplayStatics::SpawnEmitterAttached(
Tracer,
CollisionBox,
FName(),
GetActorLocation(),
GetActorRotation(),
EAttachLocation::KeepWorldPosition
);
}
2022-05-05 13:02:47 +00:00
if (HasAuthority())
{
CollisionBox->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);
}
}
void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
Destroy();
}
2022-05-20 16:12:50 +00:00
void AProjectile::SpawnTrailSystem()
2022-05-04 17:17:25 +00:00
{
2022-05-20 16:12:50 +00:00
if (TrailSystem)
{
TrailSystemComponent = UNiagaraFunctionLibrary::SpawnSystemAttached(
TrailSystem,
GetRootComponent(),
FName(),
GetActorLocation(),
GetActorRotation(),
EAttachLocation::KeepWorldPosition,
false
);
}
}
void AProjectile::ExplodeDamage()
{
APawn* FiringPawn = GetInstigator();
if (FiringPawn && HasAuthority())
{
AController* FiringController = FiringPawn->GetController();
if (FiringController)
{
UGameplayStatics::ApplyRadialDamageWithFalloff(
this,
Damage,
10.f,
GetActorLocation(),
DamageInnerRadius,
DamageOuterRadius,
1.f,
UDamageType::StaticClass(),
TArray<AActor*>(),
this,
FiringController
);
}
}
2022-05-04 17:17:25 +00:00
}
2022-05-29 09:59:15 +00:00
void AProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AProjectile::StartDestroyTimer()
{
GetWorldTimerManager().SetTimer(
DestroyTimer,
this,
&AProjectile::DestroyTimerFinished,
DestroyTime
);
}
void AProjectile::DestroyTimerFinished()
{
Destroy();
}
void AProjectile::Destroyed()
{
Super::Destroyed();
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, GetActorTransform());
}
if (ImpactSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation());
}
}