blaster/Source/Blaster/Weapon/ProjectileRocket.cpp

110 lines
2.7 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "ProjectileRocket.h"
#include "Kismet/GameplayStatics.h"
#include "NiagaraComponent.h"
#include "RocketMovementComponent.h"
#include "Components/AudioComponent.h"
#include "Components/BoxComponent.h"
#include "Sound/SoundCue.h"
AProjectileRocket::AProjectileRocket()
{
ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Rocket Mesh"));
ProjectileMesh->SetupAttachment(RootComponent);
ProjectileMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
RocketMovementComponent = CreateDefaultSubobject<URocketMovementComponent>("RocketMovementComponent");
RocketMovementComponent->bRotationFollowsVelocity = true;
RocketMovementComponent->SetIsReplicated(true);
}
void AProjectileRocket::Destroyed()
{
}
#if WITH_EDITOR
void AProjectileRocket::PostEditChangeProperty(FPropertyChangedEvent& Event)
{
Super::PostEditChangeProperty(Event);
const FName PropertyName = Event.Property != nullptr ? Event.Property->GetFName() : NAME_None;
if (PropertyName == GET_MEMBER_NAME_CHECKED(AProjectileRocket, InitialSpeed))
{
if (RocketMovementComponent)
{
RocketMovementComponent->InitialSpeed = InitialSpeed;
RocketMovementComponent->MaxSpeed = InitialSpeed;
}
}
}
#endif
void AProjectileRocket::BeginPlay()
{
Super::BeginPlay();
if (!HasAuthority())
{
CollisionBox->OnComponentHit.AddDynamic(this, &AProjectileRocket::OnHit);
}
SpawnTrailSystem();
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::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if (OtherActor == GetOwner())
{
return;
}
ExplodeDamage();
StartDestroyTimer();
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, GetActorTransform());
}
if (ImpactSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation());
}
if (ProjectileMesh)
{
ProjectileMesh->SetVisibility(false);
}
if (CollisionBox)
{
CollisionBox->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
if (TrailSystemComponent && TrailSystemComponent->GetSystemInstanceController().IsValid())
{
TrailSystemComponent->GetSystemInstanceController()->Deactivate();
}
if (ProjectileLoopComponent && ProjectileLoopComponent->IsPlaying())
{
ProjectileLoopComponent->Stop();
}
}