Produktion_Digitaler_Medien/Source/BuildingEscape/ExtraSpecialRotatingDoor.cpp

95 lines
2.3 KiB
C++

#include "ExtraSpecialRotatingDoor.h"
#include "Components/AudioComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/World.h"
#include "Engine/Public/TimerManager.h"
UExtraSpecialRotatingDoor::UExtraSpecialRotatingDoor()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UExtraSpecialRotatingDoor::BeginPlay()
{
Super::BeginPlay();
}
void UExtraSpecialRotatingDoor::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(DeltaTime * RotationSpeed * tempPitchRotation, 0.0f, DeltaTime * RotationSpeed * tempRollRotation);
GetOwner()->SetActorRotation(Rotation);
}
}
}
void UExtraSpecialRotatingDoor::Rotate(const FRotator TargetRotation)
{
FTimerHandle handle;
if(RotationDelay == 0)
{
Move(TargetRotation);
}else
{
GetWorld()->GetTimerManager().SetTimer(handle, [this, TargetRotation]() {
Move(TargetRotation);
}, RotationDelay, 1);
}
}
void UExtraSpecialRotatingDoor::Rotate()
{
FRotator rotation = GetOwner()->GetActorRotation();
rotation.Pitch = TargetAnglePitch;
rotation.Roll = TargetAngleRoll;
Rotate(rotation);
}
void UExtraSpecialRotatingDoor::Move(const FRotator TargetRotation)
{
this->targetRotation = TargetRotation;
rotating = true;
tempPitchRotation = (TargetRotation.Pitch < 0) ? -1.f : 1.f;
if (Negative)
{
tempPitchRotation = (TargetRotation.Pitch < 0) ? 1.f : -1.f;
}
tempRollRotation = (TargetRotation.Roll < 0) ? -1.f : 1.f;
if (Negative)
{
tempRollRotation = (TargetRotation.Roll < 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;
}