// Fill out your copyright notice in the Description page of Project Settings. #include "ProjectileRocket.h" #include "Kismet/GameplayStatics.h" #include "NiagaraFunctionLibrary.h" #include "NiagaraComponent.h" #include "Components/AudioComponent.h" #include "Components/BoxComponent.h" #include "Sound/SoundCue.h" AProjectileRocket::AProjectileRocket() { RocketMesh = CreateDefaultSubobject(TEXT("Rocket Mesh")); RocketMesh->SetupAttachment(RootComponent); RocketMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision); } 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(nullptr), false ); } } void AProjectileRocket::DestroyTimerFinished() { Destroy(); } void AProjectileRocket::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) { APawn* FiringPawn = GetInstigator(); if (FiringPawn && HasAuthority()) { AController* FiringController = FiringPawn->GetController(); if (FiringController) { UGameplayStatics::ApplyRadialDamageWithFalloff( this, Damage, 10.f, GetActorLocation(), 200.f, 500.f, 1.f, UDamageType::StaticClass(), TArray(), this, FiringController ); } } 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(); } }