diff --git a/Source/BuildingEscape/OpenDoor.cpp b/Source/BuildingEscape/OpenDoor.cpp index 890b6ad..f39c94f 100644 --- a/Source/BuildingEscape/OpenDoor.cpp +++ b/Source/BuildingEscape/OpenDoor.cpp @@ -1,6 +1,4 @@ -// Fill out your copyright notice in the Description page of Project Settings. #include "OpenDoor.h" -#include "Components/AudioComponent.h" #include "Components/PrimitiveComponent.h" #include "Engine/World.h" #include "GameFramework/Actor.h" @@ -22,32 +20,6 @@ UOpenDoor::UOpenDoor() void UOpenDoor::BeginPlay() { Super::BeginPlay(); - - InitialYaw = GetOwner()->GetActorRotation().Yaw; - CurrentYaw = InitialYaw; - TargetAngleOfOpenDoor += InitialYaw; - - FindPressurePlateComponent(); - - FindAudioComponent(); - //ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn(); -} - -void UOpenDoor::FindAudioComponent() -{ - AudioComponent = GetOwner()->FindComponentByClass(); //<> for function templates - if (!AudioComponent) // same as if(PhysicsHandle == nullptr) - { - UE_LOG(LogTemp, Error, TEXT("No Audio component found on: %s !"), *GetOwner()->GetName()); - } -} - -void UOpenDoor::FindPressurePlateComponent() -{ - if (!PressurePlate) - { - UE_LOG(LogTemp, Error, TEXT("%s has OpenDoor on it, but no PressurePlate set!"), *GetOwner()->GetName()); - } } // Called every frame @@ -60,76 +32,36 @@ void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorCompon //if(PressurePlate && PressurePlate->IsOverlappingActor(ActorThatOpens)) //prüft erst, ob es ein actor gibt if (TotalMass() >= MassToOpenDoor) { - UE_LOG(LogTemp, Warning, TEXT("TEST OPEN DOOR")); - OpenDoor(DeltaTime); + OpenDoor(); //DoorLastOpened When the door was opened - DoorLastOpened = GetWorld()->GetTimeSeconds(); + doorLastOpened = GetWorld()->GetTimeSeconds(); } - else + else if(open) { //if door has been open longer than DoorCloseDelay //if(GetWorld()->GetTimeSeconds() > DoorLastOpened + DoorDelay) - if (GetWorld()->GetTimeSeconds() - DoorLastOpened > DoorDelay) - CloseDoor(DeltaTime); + if (GetWorld()->GetTimeSeconds() - doorLastOpened > RotationDelay) + CloseDoor(); } - - if(rotating) - { - if (GetOwner()->GetActorRotation().Equals(targetRotation, 1.0)) - { - rotating = false; - } - else - { - GetOwner()->AddActorLocalRotation(DeltaTime * OpenDoorRotationSpeed * FRotator(0.0f, 1.0f, 0.0f)); - } - } - - } -void UOpenDoor::OpenDoor(float DeltaTime) +void UOpenDoor::OpenDoor() { - FRotator DoorRotation = GetOwner()->GetActorRotation(); - //CurrentYaw = FMath::FInterpTo(CurrentYaw, TargetYaw, DeltaTime, 2.f); - CurrentYaw = FMath::Lerp(CurrentYaw, TargetAngleOfOpenDoor, DeltaTime * OpenDoorRotationSpeed); - //complex Interpolation - DoorRotation.Yaw = CurrentYaw; - rotateDoor(DoorRotation); - - CloseDoorSoundHasBeenPlayed = false; - if (!AudioComponent) { return; } - if (!OpenDoorSoundHasBeenPlayed) - { - AudioComponent->Play(); - OpenDoorSoundHasBeenPlayed = true; - } + FRotator rotation = GetOwner()->GetActorRotation(); + rotation.Yaw = TargetAngle; + rotate(rotation); + open = true; } -void UOpenDoor::CloseDoor(float DeltaTime) +void UOpenDoor::CloseDoor() { - FRotator DoorRotation = GetOwner()->GetActorRotation(); - CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * CloseDoorRotationSpeed); - DoorRotation.Yaw = CurrentYaw; - rotateDoor(DoorRotation); - - OpenDoorSoundHasBeenPlayed = false; - if (!AudioComponent) { return; } - if (!CloseDoorSoundHasBeenPlayed) - { - AudioComponent->Play(); - CloseDoorSoundHasBeenPlayed = true; - } + FRotator rotation = GetOwner()->GetActorRotation(); + rotation.Yaw = 0.0f; + rotate(rotation); + open = false; } -void UOpenDoor::rotateDoor(FRotator targetRotation) -{ - this->targetRotation = targetRotation; - rotating = true; -} - - float UOpenDoor::TotalMass() const { float TotalMass = 0.f; @@ -147,32 +79,4 @@ float UOpenDoor::TotalMass() const } return TotalMass; -} - -//Notes -//UE_LOG(LogTemp, Warning, TEXT("%f"), TargetYaw); -// UE_LOG(LogTemp, Warning, TEXT("%s"), *GetOwner()->GetActorRotation().ToString()); -// UE_LOG(LogTemp, Warning, TEXT("The Yaw is: %f"), GetOwner()->GetActorRotation().Yaw); - -// FRotator CurrentRotation = GetOwner()->GetActorRotation(); -// FMath::Lerp(CurrentRotation, FRotator(0.f,90.f,0.f), 0.2f); - -// GetOwner()->SetActorRotation(CurrentRotation); -// UE_LOG(LogTemp, Warning, TEXT("The Yaw is: %f"), CurrentRotation.Yaw); -//TargetYaw = 90.f; -// FRotator OpenDoor(0.f, 0.f, 0.f); -// //OpenDoor.Yaw = FMath::Lerp(CurrentYaw, TargetYaw, 0.02f); //complex Interpolation -// OpenDoor.Yaw = FMath::FInterpTo(CurrentYaw, TargetYaw, DeltaTime, 2.f);//complex Interpolation Framerate unabhängig wegen Deltatime -// //OpenDoor.Yaw = FMath::FInterpConstantTo(CurrentYaw, TargetYaw, DeltaTime, 0.02f); //lineare interpolation -// GetOwner()->SetActorRotation(OpenDoor); - - -//float f = 10.f; -//FRotator CurrentRotation = GetOwner()->GetActorRotation(); -//FRotator Rotation = FRotator(0.f, -90.f, 0.f); -//FRotator OpenDoor={float,float,float} -//FRotator OpenDoor{float,float,float} -//FRotator OpenDoor(float,float,float) -//CurrentRotation.Yaw = 90.f; - -//GetOwner()->SetActorRotation(CurrentRotation); +} \ No newline at end of file diff --git a/Source/BuildingEscape/OpenDoor.h b/Source/BuildingEscape/OpenDoor.h index 5f349f7..07142be 100644 --- a/Source/BuildingEscape/OpenDoor.h +++ b/Source/BuildingEscape/OpenDoor.h @@ -1,6 +1,8 @@ #pragma once #include "CoreMinimal.h" + +#include "RotatingActor.h" #include "Components/AudioComponent.h" #include "Components/ActorComponent.h" #include "Engine/TriggerVolume.h" @@ -10,7 +12,7 @@ UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) -class BUILDINGESCAPE_API UOpenDoor : public UActorComponent +class BUILDINGESCAPE_API UOpenDoor : public URotatingActor { GENERATED_BODY() @@ -24,46 +26,19 @@ protected: public: // Called every frame virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; - void OpenDoor(float DeltaTime); - void CloseDoor(float DeltaTime); + void OpenDoor(); + void CloseDoor(); float TotalMass() const; - void FindAudioComponent(); - void FindPressurePlateComponent(); - //Tracks if Sound has been Played - bool OpenDoorSoundHasBeenPlayed = false; - bool CloseDoorSoundHasBeenPlayed = true; - private: - - float InitialYaw; - float CurrentYaw; - float DoorLastOpened = 0.f; - bool HasSoundBeenPlayed = false; - bool rotating = false; - FRotator targetRotation; - - void rotateDoor(FRotator targetRotation); + bool open = false; + float doorLastOpened; UPROPERTY(EditAnywhere) float MassToOpenDoor = 50.f; - UPROPERTY(EditAnywhere) - float TargetAngleOfOpenDoor =90.f; - - UPROPERTY(EditAnywhere) - float OpenDoorRotationSpeed = 2.f; - - UPROPERTY(EditAnywhere) - float CloseDoorRotationSpeed = 0.5f; - - UPROPERTY(EditAnywhere) - float DoorDelay = 2.f; //2 second delay - UPROPERTY(EditAnywhere) ATriggerVolume* PressurePlate = nullptr; - UPROPERTY() - UAudioComponent* AudioComponent = nullptr; }; diff --git a/Source/BuildingEscape/RotatingActor.cpp b/Source/BuildingEscape/RotatingActor.cpp new file mode 100644 index 0000000..7296adb --- /dev/null +++ b/Source/BuildingEscape/RotatingActor.cpp @@ -0,0 +1,52 @@ +#include "RotatingActor.h" + + +URotatingActor::URotatingActor() +{ + PrimaryComponentTick.bCanEverTick = true; +} + +void URotatingActor::BeginPlay() +{ + Super::BeginPlay(); + AudioComponent = GetOwner()->FindComponentByClass(); + 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, 1.0f, 0.0f)); + } + } +} + +void URotatingActor::rotate(FRotator targetRotation) +{ + this->targetRotation = targetRotation; + rotating = true; + if(AudioComponent != nullptr && !soundHasBeenPlayed) + { + AudioComponent->Play(); + soundHasBeenPlayed = true; + } +} + +void URotatingActor::rotate() +{ + FRotator rotation = GetOwner()->GetActorRotation(); + rotation.Yaw = TargetAngle; + rotate(rotation); + +} diff --git a/Source/BuildingEscape/RotatingActor.h b/Source/BuildingEscape/RotatingActor.h new file mode 100644 index 0000000..42c14f9 --- /dev/null +++ b/Source/BuildingEscape/RotatingActor.h @@ -0,0 +1,44 @@ +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" +#include "Components/ActorComponent.h" +#include "Components/AudioComponent.h" + +#include "RotatingActor.generated.h" + + +UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) +class BUILDINGESCAPE_API URotatingActor : public UActorComponent +{ + GENERATED_BODY() + +public: + URotatingActor(); + +protected: + virtual void BeginPlay() override; + void rotate(FRotator targetRotation); + void rotate(); + +public: + virtual void TickComponent(float DeltaTime, ELevelTick TickType, + FActorComponentTickFunction* ThisTickFunction) override; + + UPROPERTY(EditAnywhere) + float TargetAngle = 90.0f; + + UPROPERTY(EditAnywhere) + UAudioComponent * AudioComponent; + + UPROPERTY(EditAnywhere) + float RotationSpeed = 15.f; + + UPROPERTY(EditAnywhere) + float RotationDelay = 2.f; + +private: + FRotator targetRotation; + bool rotating; + bool soundHasBeenPlayed; +}; diff --git a/Source/BuildingEscape/TriggerOpenDoor.cpp b/Source/BuildingEscape/TriggerOpenDoor.cpp index f4a8337..cbd1c0c 100644 --- a/Source/BuildingEscape/TriggerOpenDoor.cpp +++ b/Source/BuildingEscape/TriggerOpenDoor.cpp @@ -8,11 +8,6 @@ UTriggerOpenDoor::UTriggerOpenDoor() void UTriggerOpenDoor::BeginPlay() { Super::BeginPlay(); - AudioComponent = GetOwner()->FindComponentByClass(); - if (!AudioComponent) - { - UE_LOG(LogTemp, Error, TEXT("No audio component found on: %s !"), *GetOwner()->GetName()); - } if(!TriggerVolume) { UE_LOG(LogTemp, Error, TEXT("No trigger volume found on: %s !"), *GetOwner()->GetName()); @@ -24,8 +19,7 @@ void UTriggerOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); - if (!doorOpen) - { + if(!TriggerVolume) { return; @@ -35,25 +29,6 @@ void UTriggerOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, TriggerVolume->GetOverlappingActors(overlappingActors); if (overlappingActors.Num() >= 1) { - - if (AudioComponent) - { - AudioComponent->Play(); - } - doorOpen = true; - opening = true; - currentRotation = GetOwner()->GetActorRotation(); + rotate(); } - } - if (opening) - { - if (GetOwner()->GetActorRotation().Equals(TargetRotation, 1.0)) - { - opening = false; - } - else - { - GetOwner()->AddActorLocalRotation(DeltaTime * TargetSpeed); - } - } } diff --git a/Source/BuildingEscape/TriggerOpenDoor.h b/Source/BuildingEscape/TriggerOpenDoor.h index 926d4b1..72c0054 100644 --- a/Source/BuildingEscape/TriggerOpenDoor.h +++ b/Source/BuildingEscape/TriggerOpenDoor.h @@ -3,6 +3,8 @@ #pragma once #include "CoreMinimal.h" + +#include "RotatingActor.h" #include "Components/ActorComponent.h" #include "Components/AudioComponent.h" #include "Engine/TriggerVolume.h" @@ -11,7 +13,7 @@ UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) -class BUILDINGESCAPE_API UTriggerOpenDoor : public UActorComponent +class BUILDINGESCAPE_API UTriggerOpenDoor : public URotatingActor { GENERATED_BODY() @@ -19,24 +21,11 @@ public: UTriggerOpenDoor(); virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; - - UPROPERTY(EditAnywhere) - FRotator TargetRotation = FRotator(00.0f, 90.0f, 0.0f); - - UPROPERTY(EditAnywhere) - FRotator TargetSpeed = FRotator(0.0f, 1.0f, 0.0f); UPROPERTY(EditAnywhere) ATriggerVolume * TriggerVolume = nullptr; - UPROPERTY(EditAnywhere) - UAudioComponent * AudioComponent = nullptr; - protected: virtual void BeginPlay() override; - -private: - bool doorOpen = false; - bool opening = false; - FRotator currentRotation; + };