73 lines
1.6 KiB
C++
73 lines
1.6 KiB
C++
#include "RotatingActor.h"
|
|
|
|
#include "Components/AudioComponent.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
|
|
|
|
URotatingActor::URotatingActor()
|
|
{
|
|
PrimaryComponentTick.bCanEverTick = true;
|
|
}
|
|
|
|
void URotatingActor::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
}
|
|
|
|
void URotatingActor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
|
{
|
|
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
|
if (rotating)
|
|
{
|
|
FRotator Rotation = GetOwner()->GetActorRotation();
|
|
if (Rotation.Equals(targetRotation, 1.0f))
|
|
{
|
|
rotating = false;
|
|
}
|
|
else
|
|
{
|
|
Rotation.Add(0.0f, DeltaTime * RotationSpeed * tempRotation, 0.0f);
|
|
GetOwner()->SetActorRotation(Rotation);
|
|
}
|
|
}
|
|
}
|
|
|
|
void URotatingActor::Rotate(const FRotator TargetRotation)
|
|
{
|
|
this->targetRotation = TargetRotation;
|
|
rotating = true;
|
|
tempRotation = (TargetRotation.Yaw < 0) ? -1.f : 1.f;
|
|
if (Negative)
|
|
{
|
|
tempRotation = (TargetRotation.Yaw < 0) ? 1.f : -1.f;
|
|
}
|
|
|
|
TArray<UAudioComponent*> Audios;
|
|
GetOwner()->GetComponents<UAudioComponent>(Audios);
|
|
|
|
if (Audios.Num() == 0) return;
|
|
if (soundHasBeenPlayed) return;
|
|
|
|
for (auto Audio : Audios)
|
|
{
|
|
if (Audio->ComponentHasTag("location"))
|
|
{
|
|
UGameplayStatics::PlaySoundAtLocation(GetOwner(), Audio->Sound, GetOwner()->GetActorLocation(),
|
|
GetOwner()->GetActorRotation(), 1, 1, 0,
|
|
Audio->AttenuationSettings, nullptr, GetOwner());
|
|
}
|
|
else
|
|
{
|
|
Audio->Play();
|
|
}
|
|
}
|
|
|
|
soundHasBeenPlayed = true;
|
|
}
|
|
|
|
void URotatingActor::Rotate()
|
|
{
|
|
FRotator rotation = GetOwner()->GetActorRotation();
|
|
rotation.Yaw = TargetAngle;
|
|
Rotate(rotation);
|
|
}
|