Produktion_Digitaler_Medien/Source/BuildingEscape/RotatingActor.cpp

88 lines
2.0 KiB
C++
Raw Normal View History

2020-12-03 14:12:54 +01:00
#include "RotatingActor.h"
2021-01-07 16:31:32 +01:00
#include "Components/AudioComponent.h"
2021-01-19 15:25:56 +01:00
#include "Kismet/GameplayStatics.h"
#include "Engine/World.h"
#include "Engine/Public/TimerManager.h"
2020-12-03 14:12:54 +01:00
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)
{
2020-12-16 19:25:26 +01:00
FRotator Rotation = GetOwner()->GetActorRotation();
2021-03-13 19:57:51 +01:00
if (Rotation.Equals(targetRotation, Tolerance))
2020-12-03 14:12:54 +01:00
{
rotating = false;
2021-01-07 16:31:32 +01:00
}
else
2020-12-03 14:12:54 +01:00
{
2020-12-16 19:25:26 +01:00
Rotation.Add(0.0f, DeltaTime * RotationSpeed * tempRotation, 0.0f);
GetOwner()->SetActorRotation(Rotation);
2020-12-03 14:12:54 +01:00
}
}
}
2020-12-08 12:21:49 +01:00
void URotatingActor::Rotate(const FRotator TargetRotation)
{
FTimerHandle handle;
if(RotationDelay == 0)
{
2021-03-13 14:13:44 +01:00
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)
2020-12-03 14:12:54 +01:00
{
2020-12-08 12:21:49 +01:00
this->targetRotation = TargetRotation;
2020-12-03 14:12:54 +01:00
rotating = true;
2020-12-08 12:21:49 +01:00
tempRotation = (TargetRotation.Yaw < 0) ? -1.f : 1.f;
2021-01-07 16:31:32 +01:00
if (Negative)
2020-12-08 12:21:49 +01:00
{
tempRotation = (TargetRotation.Yaw < 0) ? 1.f : -1.f;
}
2021-01-07 16:31:32 +01:00
TArray<UAudioComponent*> Audios;
GetOwner()->GetComponents<UAudioComponent>(Audios);
2021-01-07 16:31:32 +01:00
if (Audios.Num() == 0) return;
if (soundHasBeenPlayed) return;
2021-01-07 16:31:32 +01:00
for (auto Audio : Audios)
2020-12-03 14:12:54 +01:00
{
2021-01-07 16:31:32 +01:00
if (Audio->ComponentHasTag("location"))
{
UGameplayStatics::PlaySoundAtLocation(GetOwner(), Audio->Sound, GetOwner()->GetActorLocation(),
GetOwner()->GetActorRotation(), 1, 1, 0,
Audio->AttenuationSettings, nullptr, GetOwner());
2021-01-07 16:31:32 +01:00
}
else
{
Audio->Play();
}
2020-12-03 14:12:54 +01:00
}
2021-01-07 16:31:32 +01:00
soundHasBeenPlayed = true;
}