blaster/Source/Blaster/Weapon/Projectile.cpp

81 lines
2.1 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Projectile.h"
#include "Blaster/Blaster.h"
#include "Blaster/Character/BlasterCharacter.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(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);
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)
{
if (ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(OtherActor))
{
BlasterCharacter->MulticastHit();
}
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);
}