128 lines
2.7 KiB
C++
128 lines
2.7 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Projectile.h"
|
|
|
|
#include "NiagaraFunctionLibrary.h"
|
|
#include "Blaster/Blaster.h"
|
|
#include "Components/BoxComponent.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "Sound/SoundCue.h"
|
|
|
|
AProjectile::AProjectile()
|
|
{
|
|
PrimaryActorTick.bCanEverTick = true;
|
|
bReplicates = true;
|
|
|
|
CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
|
|
SetRootComponent(CollisionBox);
|
|
CollisionBox->SetCollisionObjectType(ECC_WorldDynamic);
|
|
CollisionBox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
|
|
CollisionBox->SetCollisionResponseToChannels(ECR_Ignore);
|
|
CollisionBox->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
|
|
CollisionBox->SetCollisionResponseToChannel(ECC_WorldStatic, ECR_Block);
|
|
CollisionBox->SetCollisionResponseToChannel(ECC_SkeletalMesh, ECR_Block);
|
|
}
|
|
|
|
void AProjectile::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
if (Tracer)
|
|
{
|
|
TracerComponent = UGameplayStatics::SpawnEmitterAttached(
|
|
Tracer,
|
|
CollisionBox,
|
|
FName(),
|
|
GetActorLocation(),
|
|
GetActorRotation(),
|
|
EAttachLocation::KeepWorldPosition
|
|
);
|
|
}
|
|
|
|
if (HasAuthority())
|
|
{
|
|
CollisionBox->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);
|
|
}
|
|
}
|
|
|
|
void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
|
|
{
|
|
Destroy();
|
|
}
|
|
|
|
void AProjectile::SpawnTrailSystem()
|
|
{
|
|
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
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|