blaster/Source/Blaster/Weapon/Casing.cpp

46 lines
1.3 KiB
C++
Raw Normal View History

2022-05-05 13:49:24 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "Casing.h"
2022-05-05 14:40:14 +00:00
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
2022-05-05 13:49:24 +00:00
ACasing::ACasing()
{
PrimaryActorTick.bCanEverTick = false;
2022-05-05 14:40:14 +00:00
ShellEjectionImpulseMin = 8.f;
ShellEjectionImpulseMax = 12.f;
2022-05-05 13:49:24 +00:00
CasingMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CasingMesh"));
2022-05-05 14:40:14 +00:00
CasingMesh->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
2022-05-05 16:16:59 +00:00
CasingMesh->SetCollisionResponseToChannel(ECC_Pawn, ECR_Ignore);
2022-05-05 14:40:14 +00:00
CasingMesh->SetSimulatePhysics(true);
CasingMesh->SetEnableGravity(true);
CasingMesh->SetNotifyRigidBodyCollision(true);
2022-05-05 13:49:24 +00:00
SetRootComponent(CasingMesh);
}
void ACasing::BeginPlay()
{
Super::BeginPlay();
2022-05-05 14:40:14 +00:00
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);
2022-05-05 13:49:24 +00:00
}