blaster/Source/Blaster/Weapon/Projectile.cpp

72 lines
1.7 KiB
C++
Raw Normal View History

2022-05-04 17:17:25 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "Projectile.h"
2022-05-05 22:41:35 +00:00
#include "Blaster/Blaster.h"
2022-05-04 17:17:25 +00:00
#include "Components/BoxComponent.h"
2022-05-05 12:15:25 +00:00
#include "Kismet/GameplayStatics.h"
2022-05-05 13:02:47 +00:00
#include "Sound/SoundCue.h"
2022-05-04 17:17:25 +00:00
AProjectile::AProjectile()
{
PrimaryActorTick.bCanEverTick = true;
2022-05-05 12:15:25 +00:00
bReplicates = true;
2022-05-04 17:17:25 +00:00
2022-05-05 12:15:25 +00:00
CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
2022-05-04 17:17:25 +00:00
SetRootComponent(CollisionBox);
2022-05-05 22:41:35 +00:00
CollisionBox->SetCollisionObjectType(ECC_WorldDynamic);
2022-05-04 17:17:25 +00:00
CollisionBox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
2022-05-05 22:41:35 +00:00
CollisionBox->SetCollisionResponseToChannels(ECR_Ignore);
CollisionBox->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
CollisionBox->SetCollisionResponseToChannel(ECC_WorldStatic, ECR_Block);
CollisionBox->SetCollisionResponseToChannel(ECC_SkeletalMesh, ECR_Block);
2022-05-04 17:17:25 +00:00
}
void AProjectile::BeginPlay()
{
Super::BeginPlay();
2022-05-05 12:15:25 +00:00
if (Tracer)
{
TracerComponent = UGameplayStatics::SpawnEmitterAttached(
Tracer,
CollisionBox,
FName(),
GetActorLocation(),
GetActorRotation(),
EAttachLocation::KeepWorldPosition
);
}
2022-05-05 13:02:47 +00:00
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());
}
2022-05-04 17:17:25 +00:00
}
void AProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}