74 lines
1.9 KiB
C++
74 lines
1.9 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Projectile.h"
|
|
|
|
#include "Components/BoxComponent.h"
|
|
#include "GameFramework/ProjectileMovementComponent.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(ECollisionChannel::ECC_WorldDynamic);
|
|
CollisionBox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
|
|
CollisionBox->SetCollisionResponseToChannels(ECollisionResponse::ECR_Ignore);
|
|
CollisionBox->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block);
|
|
CollisionBox->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
|
|
|
|
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
|
|
ProjectileMovementComponent->bRotationFollowsVelocity = true;
|
|
}
|
|
|
|
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);
|
|
}
|