Produktion_Digitaler_Medien/Source/BuildingEscape/OpenDoor.cpp

89 lines
2.5 KiB
C++
Raw Normal View History

2020-10-08 15:33:58 +02:00
#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()
{
2020-12-01 18:30:41 +01:00
// 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;
2020-10-08 15:33:58 +02:00
2020-12-01 18:30:41 +01:00
// ...
2020-10-08 15:33:58 +02:00
}
// Called when the game starts
void UOpenDoor::BeginPlay()
{
2020-12-01 18:30:41 +01:00
Super::BeginPlay();
2020-10-08 15:33:58 +02:00
}
// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
2020-12-01 18:30:41 +01:00
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
2020-12-16 19:25:26 +01:00
//UE_LOG(LogTemp, Display, TEXT("%f Total Weight"), TotalMass());
2020-12-01 18:30:41 +01:00
if (TotalMass() >= MassToOpenDoor)
{
2020-12-16 19:25:26 +01:00
UE_LOG(LogTemp, Display, TEXT("%f Total Weight"), TotalMass());
2020-12-03 14:12:54 +01:00
OpenDoor();
2020-12-01 18:30:41 +01:00
//DoorLastOpened When the door was opened
2020-12-03 14:12:54 +01:00
doorLastOpened = GetWorld()->GetTimeSeconds();
2020-12-01 18:30:41 +01:00
}
2020-12-03 14:12:54 +01:00
else if(open)
2020-12-01 18:30:41 +01:00
{
//if door has been open longer than DoorCloseDelay
//if(GetWorld()->GetTimeSeconds() > DoorLastOpened + DoorDelay)
2020-12-03 14:12:54 +01:00
if (GetWorld()->GetTimeSeconds() - doorLastOpened > RotationDelay)
CloseDoor();
2020-12-01 18:30:41 +01:00
}
2020-10-08 15:33:58 +02:00
}
2020-12-03 14:12:54 +01:00
void UOpenDoor::OpenDoor()
2020-10-08 15:33:58 +02:00
{
2020-12-03 14:12:54 +01:00
FRotator rotation = GetOwner()->GetActorRotation();
rotation.Yaw = TargetAngle;
2020-12-08 12:21:49 +01:00
Rotate(rotation);
2020-12-03 14:12:54 +01:00
open = true;
2020-10-08 15:33:58 +02:00
}
2020-12-03 14:12:54 +01:00
void UOpenDoor::CloseDoor()
2020-12-01 18:30:41 +01:00
{
2020-12-03 14:12:54 +01:00
FRotator rotation = GetOwner()->GetActorRotation();
rotation.Yaw = 0.0f;
2020-12-08 12:21:49 +01:00
Rotate(rotation);
2020-12-03 14:12:54 +01:00
open = false;
2020-12-01 18:30:41 +01:00
}
2020-10-08 15:33:58 +02:00
float UOpenDoor::TotalMass() const
{
2020-12-01 18:30:41 +01:00
float TotalMass = 0.f;
2020-10-08 15:33:58 +02:00
2020-12-01 18:30:41 +01:00
//find All Overlapping Actors
TArray<AActor*> OverlapingActors;
2020-10-08 15:33:58 +02:00
2020-12-01 18:30:41 +01:00
if (!PressurePlate) { return TotalMass; }
PressurePlate->GetOverlappingActors(OUT OverlapingActors);
2020-10-08 15:33:58 +02:00
2020-12-01 18:30:41 +01:00
//Add Up Their Masses
for (AActor* Actor : OverlapingActors)
{
2020-12-16 19:25:26 +01:00
UE_LOG(LogTemp, Display, TEXT("%s Actor"), *Actor->GetName());
if (Actor->FindComponentByClass<UPrimitiveComponent>()->IsAnySimulatingPhysics())//probably why doors wont open
{
TotalMass =+ Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
}
2020-12-01 18:30:41 +01:00
}
2020-10-08 15:33:58 +02:00
2020-12-01 18:30:41 +01:00
return TotalMass;
2020-12-03 14:12:54 +01:00
}