Produktion_Digitaler_Medien/Source/BuildingEscape/RotatingActor.cpp

58 lines
1.3 KiB
C++

#include "RotatingActor.h"
URotatingActor::URotatingActor()
{
PrimaryComponentTick.bCanEverTick = true;
}
void URotatingActor::BeginPlay()
{
Super::BeginPlay();
AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
if (!AudioComponent)
{
UE_LOG(LogTemp, Error, TEXT("No audio component found on: %s !"), *GetOwner()->GetName());
}
}
void URotatingActor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (rotating)
{
if (GetOwner()->GetActorRotation().Equals(targetRotation, 1.0f))
{
rotating = false;
}else
{
GetOwner()->AddActorLocalRotation(DeltaTime * RotationSpeed * FRotator(0.0f, tempRotation, 0.0f));
}
}
}
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;
}
if(AudioComponent != nullptr && !soundHasBeenPlayed)
{
AudioComponent->Play();
soundHasBeenPlayed = true;
}
}
void URotatingActor::Rotate()
{
FRotator rotation = GetOwner()->GetActorRotation();
rotation.Yaw = TargetAngle;
Rotate(rotation);
}