Produktion_Digitaler_Medien/Source/BuildingEscape/MovingActor.cpp

105 lines
2.4 KiB
C++

#include "MovingActor.h"
#include "Components/AudioComponent.h"
#include "Kismet/GameplayStatics.h"
UMovingActor::UMovingActor()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UMovingActor::BeginPlay()
{
Super::BeginPlay();
}
void UMovingActor::Move()
{
if(moving) return;
UE_LOG(LogTemp, Warning, TEXT("start"))
targetLocation = GetOwner()->GetActorLocation();
if(!Negative && !Up)
{
this->targetLocation.X += MovementTarget;
this->tempLocation = FVector(MovementSpeed, 0.0f, 0.0f);
}
if (Negative)
{
this->targetLocation.Y += MovementTarget;
this->tempLocation = FVector(0.0f, MovementSpeed, 0.0f);
}
if(Up)
{
this->targetLocation.Z += MovementTarget;
this->tempLocation = FVector(0.0f, 0.0f, MovementSpeed);
}
this->moving = true;
UE_LOG(LogTemp, Warning, TEXT("%f %f %f"), targetLocation.X, targetLocation.Y, targetLocation.Z)
PlaySound();
}
void UMovingActor::MoveBack()
{
if(moving) return;
this->targetLocation = GetOwner()->GetActorLocation();
if(!Negative && !Up)
{
this->targetLocation.X -= MovementTarget;
this->tempLocation = FVector(-MovementSpeed, 0.0f, 0.0f);
}
if (Negative)
{
this->targetLocation.Y -= MovementTarget;
this->tempLocation = FVector(0.0f, -MovementSpeed, 0.0f);
}
if(Up)
{
this->targetLocation.Z -= MovementTarget;
this->tempLocation = FVector(0.0f, 0.0f, -MovementSpeed);
}
this->moving = true;
PlaySound();
}
void UMovingActor::PlaySound()
{
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 UMovingActor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (moving)
{
FVector Location = GetOwner()->GetActorLocation();
if (Location.Equals(targetLocation, 5.0f))
{
moving = false;
}
else
GetOwner()->SetActorLocation(Location + tempLocation);
}
}