#include "OpenDoor.h" #include "Components/PrimitiveComponent.h" #include "Engine/World.h" #include "GameFramework/Actor.h" #include "GameFramework/PlayerController.h" #define OUT // Sets default values for this component's properties UOpenDoor::UOpenDoor() { // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features // off to improve performance if you don't need them. PrimaryComponentTick.bCanEverTick = true; // ... } // Called when the game starts void UOpenDoor::BeginPlay() { Super::BeginPlay(); } // Called every frame void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); // ... //if(PressurePlate->IsOverlappingActor(ActorThatOpens)) // kann nullpointer + UE4 crash geben //if(PressurePlate && PressurePlate->IsOverlappingActor(ActorThatOpens)) //prüft erst, ob es ein actor gibt //UE_LOG(LogTemp, Display, TEXT("%f Total Weight"), TotalMass()); if (TotalMass() >= MassToOpenDoor) { //UE_LOG(LogTemp, Display, TEXT("%f Total Weight"), TotalMass()); OpenDoor(); //DoorLastOpened When the door was opened doorLastOpened = GetWorld()->GetTimeSeconds(); } else if (open) { //if door has been open longer than DoorCloseDelay //if(GetWorld()->GetTimeSeconds() > DoorLastOpened + DoorDelay) if (GetWorld()->GetTimeSeconds() - doorLastOpened > RotationDelay) CloseDoor(); } } void UOpenDoor::OpenDoor() { FRotator rotation = GetOwner()->GetActorRotation(); rotation.Yaw = TargetAngle; Rotate(rotation); open = true; } void UOpenDoor::CloseDoor() { FRotator rotation = GetOwner()->GetActorRotation(); rotation.Yaw = 0.0f; Rotate(rotation); open = false; } float UOpenDoor::TotalMass() const { float TotalMass = 0.f; //find All Overlapping Actors TArray OverlapingActors; if (!PressurePlate) { return TotalMass; } PressurePlate->GetOverlappingActors(OUT OverlapingActors); //Add Up Their Masses for (AActor* Actor : OverlapingActors) { if(Actor->GetRootComponent()->Mobility == EComponentMobility::Movable) { //UE_LOG(LogTemp, Display, TEXT("Actor: %s"), *Actor->GetName()); TotalMass += Actor->FindComponentByClass()->GetMass(); } } return TotalMass; }