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-17 12:40:19 +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-17 12:40:19 +01:00
|
|
|
// ...
|
2020-10-08 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the game starts
|
|
|
|
void UOpenDoor::BeginPlay()
|
|
|
|
{
|
2020-12-17 12:40:19 +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-17 12:40:19 +01:00
|
|
|
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
2020-12-01 18:30:41 +01:00
|
|
|
|
2020-12-17 12:40:19 +01:00
|
|
|
// ...
|
|
|
|
//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();
|
|
|
|
}
|
2020-12-01 18:30:41 +01:00
|
|
|
|
2020-12-17 12:40:19 +01:00
|
|
|
else if (open)
|
|
|
|
{
|
|
|
|
//if door has been open longer than DoorCloseDelay
|
|
|
|
//if(GetWorld()->GetTimeSeconds() > DoorLastOpened + DoorDelay)
|
|
|
|
if (GetWorld()->GetTimeSeconds() - doorLastOpened > RotationDelay)
|
|
|
|
CloseDoor();
|
|
|
|
}
|
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-17 12:40:19 +01:00
|
|
|
FRotator rotation = GetOwner()->GetActorRotation();
|
|
|
|
rotation.Yaw = TargetAngle;
|
|
|
|
Rotate(rotation);
|
|
|
|
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-17 12:40:19 +01:00
|
|
|
FRotator rotation = GetOwner()->GetActorRotation();
|
|
|
|
rotation.Yaw = 0.0f;
|
|
|
|
Rotate(rotation);
|
|
|
|
open = false;
|
2020-12-01 18:30:41 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 15:33:58 +02:00
|
|
|
float UOpenDoor::TotalMass() const
|
|
|
|
{
|
2020-12-17 12:40:19 +01:00
|
|
|
float TotalMass = 0.f;
|
2020-10-08 15:33:58 +02:00
|
|
|
|
2020-12-17 12:40:19 +01:00
|
|
|
//find All Overlapping Actors
|
|
|
|
TArray<AActor*> OverlapingActors;
|
2020-10-08 15:33:58 +02:00
|
|
|
|
2020-12-17 12:40:19 +01:00
|
|
|
if (!PressurePlate) { return TotalMass; }
|
|
|
|
PressurePlate->GetOverlappingActors(OUT OverlapingActors);
|
2020-10-08 15:33:58 +02:00
|
|
|
|
2020-12-17 12:40:19 +01:00
|
|
|
//Add Up Their Masses
|
|
|
|
for (AActor* Actor : OverlapingActors)
|
|
|
|
{
|
2020-12-21 09:04:36 +01:00
|
|
|
if(Actor->GetRootComponent()->Mobility == EComponentMobility::Movable)
|
|
|
|
{
|
|
|
|
//UE_LOG(LogTemp, Display, TEXT("Actor: %s"), *Actor->GetName());
|
|
|
|
TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
|
|
|
|
}
|
|
|
|
|
2020-12-17 12:40:19 +01:00
|
|
|
}
|
2020-10-08 15:33:58 +02:00
|
|
|
|
2020-12-17 12:40:19 +01:00
|
|
|
return TotalMass;
|
|
|
|
}
|