blaster/Source/Blaster/Weapon/ProjectileRocket.cpp

134 lines
3.0 KiB
C++
Raw Normal View History

2022-05-18 12:44:39 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "ProjectileRocket.h"
#include "Kismet/GameplayStatics.h"
2022-05-18 17:20:18 +00:00
#include "NiagaraFunctionLibrary.h"
#include "NiagaraComponent.h"
2022-05-19 09:10:41 +00:00
#include "RocketMovementComponent.h"
2022-05-18 17:20:18 +00:00
#include "Components/AudioComponent.h"
#include "Components/BoxComponent.h"
#include "Sound/SoundCue.h"
2022-05-18 12:44:39 +00:00
AProjectileRocket::AProjectileRocket()
{
RocketMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Rocket Mesh"));
RocketMesh->SetupAttachment(RootComponent);
RocketMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
2022-05-19 09:10:41 +00:00
RocketMovementComponent = CreateDefaultSubobject<URocketMovementComponent>("RocketMovementComponent");
RocketMovementComponent->bRotationFollowsVelocity = true;
RocketMovementComponent->SetIsReplicated(true);
2022-05-18 12:44:39 +00:00
}
2022-05-18 17:20:18 +00:00
void AProjectileRocket::Destroyed()
{
}
void AProjectileRocket::BeginPlay()
{
Super::BeginPlay();
if (!HasAuthority())
{
CollisionBox->OnComponentHit.AddDynamic(this, &AProjectileRocket::OnHit);
}
if (TrailSystem)
{
TrailSystemComponent = UNiagaraFunctionLibrary::SpawnSystemAttached(
TrailSystem,
GetRootComponent(),
FName(),
GetActorLocation(),
GetActorRotation(),
EAttachLocation::KeepWorldPosition,
false
);
}
if (ProjectileLoop && LoopingSoundAttenuation)
{
ProjectileLoopComponent = UGameplayStatics::SpawnSoundAttached(
ProjectileLoop,
GetRootComponent(),
FName(),
GetActorLocation(),
EAttachLocation::KeepWorldPosition,
false,
1.f,
1.f,
0.f,
LoopingSoundAttenuation,
static_cast<USoundConcurrency*>(nullptr),
false
);
}
}
void AProjectileRocket::DestroyTimerFinished()
{
Destroy();
}
2022-05-18 12:44:39 +00:00
void AProjectileRocket::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
2022-05-19 09:10:41 +00:00
if (OtherActor == GetOwner())
{
return;
}
2022-05-18 12:44:39 +00:00
APawn* FiringPawn = GetInstigator();
2022-05-18 17:20:18 +00:00
if (FiringPawn && HasAuthority())
2022-05-18 12:44:39 +00:00
{
AController* FiringController = FiringPawn->GetController();
if (FiringController)
{
UGameplayStatics::ApplyRadialDamageWithFalloff(
this,
Damage,
10.f,
GetActorLocation(),
200.f,
500.f,
1.f,
UDamageType::StaticClass(),
TArray<AActor*>(),
this,
FiringController
);
}
}
2022-05-18 17:20:18 +00:00
GetWorldTimerManager().SetTimer(
DestroyTimer,
this,
&AProjectileRocket::DestroyTimerFinished,
DestroyTime
);
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, GetActorTransform());
}
if (ImpactSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation());
}
if (RocketMesh)
{
RocketMesh->SetVisibility(false);
}
if (CollisionBox)
{
CollisionBox->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
if (TrailSystemComponent && TrailSystemComponent->GetSystemInstanceController().IsValid())
{
TrailSystemComponent->GetSystemInstanceController()->Deactivate();
}
if (ProjectileLoopComponent && ProjectileLoopComponent->IsPlaying())
{
ProjectileLoopComponent->Stop();
}
2022-05-18 12:44:39 +00:00
}