blaster/Source/Blaster/Weapon/Projectile.cpp

72 lines
1.7 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Projectile.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::Destroyed()
{
Super::Destroyed();
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, GetActorTransform());
}
if (ImpactSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation());
}
}
void AProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}