blaster/Source/Blaster/Weapon/Casing.cpp

46 lines
1.3 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Casing.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
ACasing::ACasing()
{
PrimaryActorTick.bCanEverTick = false;
ShellEjectionImpulseMin = 8.f;
ShellEjectionImpulseMax = 12.f;
CasingMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CasingMesh"));
CasingMesh->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
CasingMesh->SetCollisionResponseToChannel(ECC_Pawn, ECR_Ignore);
CasingMesh->SetSimulatePhysics(true);
CasingMesh->SetEnableGravity(true);
CasingMesh->SetNotifyRigidBodyCollision(true);
SetRootComponent(CasingMesh);
}
void ACasing::BeginPlay()
{
Super::BeginPlay();
CasingMesh->OnComponentHit.AddDynamic(this, &ACasing::OnHit);
CasingMesh->AddImpulse(GetActorForwardVector() * FMath::RandRange(ShellEjectionImpulseMin, ShellEjectionImpulseMax));
SetLifeSpan(5.f);
}
void ACasing::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if (ShellSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ShellSound, GetActorLocation());
}
// Disable hit event so that sound won't play multiple times
CasingMesh->SetNotifyRigidBodyCollision(false);
}