Produktion_Digitaler_Medien/Source/BuildingEscape/RotatingActor.cpp

88 lines
2.0 KiB
C++

#include "RotatingActor.h"
#include "Components/AudioComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/World.h"
#include "Engine/Public/TimerManager.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, Tolerance))
{
rotating = false;
}
else
{
Rotation.Add(0.0f, DeltaTime * RotationSpeed * tempRotation, 0.0f);
GetOwner()->SetActorRotation(Rotation);
}
}
}
void URotatingActor::Rotate(const FRotator TargetRotation)
{
FTimerHandle handle;
if(RotationDelay == 0)
{
Move(TargetRotation);
}else
{
GetWorld()->GetTimerManager().SetTimer(handle, [this, TargetRotation]() {
Move(TargetRotation);
}, RotationDelay, 1);
}
}
void URotatingActor::Rotate()
{
FRotator rotation = GetOwner()->GetActorRotation();
rotation.Yaw = TargetAngle;
Rotate(rotation);
}
void URotatingActor::Move(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;
}