Advertisement
Guest User

OpenDoor

a guest
Jan 28th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include "BuildingEscape.h"
  2. #include "OpenDoor.h"
  3.  
  4. #define OUT
  5.  
  6. // Sets default values for this component's properties
  7. UOpenDoor::UOpenDoor()
  8. {
  9.     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
  10.     // off to improve performance if you don't need them.
  11.     PrimaryComponentTick.bCanEverTick = true;
  12.  
  13.     if (!PressurePlate)
  14.     {
  15.         UE_LOG(LogTemp, Error, TEXT("%s missing pressure plate"), *Owner->GetName())
  16.     }
  17. }
  18.  
  19.  
  20. // Called when the game starts
  21. void UOpenDoor::BeginPlay()
  22. {
  23.     Super::BeginPlay();
  24.  
  25.     Owner = GetOwner();
  26. }
  27.  
  28. void UOpenDoor::OpenDoor()
  29. {
  30.     // Set the door rotation
  31.     Owner->SetActorRotation(FRotator(0.f, OpenAngle, 0.f));
  32. }
  33.  
  34. void UOpenDoor::CloseDoor()
  35. {
  36.     // Set the door rotation
  37.     Owner->SetActorRotation(FRotator(0.f, 0.f, 0.f));
  38. }
  39.  
  40.  
  41. // Called every frame
  42. void UOpenDoor::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
  43. {
  44.     Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
  45.  
  46.     // Poll the Trigger Volume
  47.     if (GetTotalMassOfActorsOnPlate() > 30.f) // TODO make into a parameter
  48.     {
  49.         OpenDoor();
  50.         LastDoorOpenTime = GetWorld()->GetTimeSeconds();
  51.     }
  52.  
  53.     // Check if it's time to close the door
  54.     if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime >= DoorCloseDelay)
  55.     {
  56.         CloseDoor();
  57.     }
  58. }
  59.  
  60. float UOpenDoor::GetTotalMassOfActorsOnPlate()
  61. {
  62.     float TotalMass = 0.f;
  63.  
  64.     // Find all the overlapping actors
  65.     TArray<AActor*> OverlappingActors;
  66.     if (!PressurePlate) { return TotalMass;  }
  67.     PressurePlate->GetOverlappingActors(OUT OverlappingActors);
  68.  
  69.     // Iterate through them, adding their masses
  70.     for (const auto* Actor : OverlappingActors)
  71.     {
  72.         UE_LOG(LogTemp, Warning, TEXT("%s on pressure plate"), *Actor->GetName())
  73.         TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
  74.     }
  75.  
  76.     return TotalMass;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement