Advertisement
Guest User

Untitled

a guest
May 25th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "HeavyDoorActor.h"
  4. #include "Runtime/Engine/Classes/Components/StaticMeshComponent.h"
  5.  
  6. // Sets default values
  7. AHeavyDoorActor::AHeavyDoorActor()
  8. {
  9.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  10.     PrimaryActorTick.bCanEverTick = true;
  11.     Door = CreateDefaultSubobject<UStaticMeshComponent>(FName("Door"));
  12.     Door->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
  13.  
  14.     RotationPoint = CreateDefaultSubobject<USceneComponent>(FName("Rotation Point"));
  15.     RotationPoint->AttachToComponent(Door, FAttachmentTransformRules::KeepRelativeTransform);
  16.  
  17.     DoorCenter = CreateDefaultSubobject<UStaticMeshComponent>(FName("Door Center"));
  18.     DoorCenter->AttachToComponent(RotationPoint, FAttachmentTransformRules::KeepRelativeTransform);
  19. }
  20.  
  21. // Called when the game starts or when spawned
  22. void AHeavyDoorActor::BeginPlay()
  23. {
  24.     Super::BeginPlay();
  25.  
  26.     DoorClosedPosition = Door->GetComponentLocation();
  27.     if(DoorOpensUp)
  28.     {
  29.         DoorOpenPosition = DoorClosedPosition + (DoorMovementAmount * GetActorUpVector());
  30.     }else
  31.     {
  32.         DoorOpenPosition = DoorClosedPosition + (DoorMovementAmount * GetActorUpVector() * -1);
  33.     }
  34.  
  35.     DoorClosedRotation = RotationPoint->GetComponentRotation();
  36.     DoorOpenRotation = DoorClosedRotation;
  37.     DoorOpenRotation.Pitch += 180;
  38. }
  39.  
  40. // Called every frame
  41. void AHeavyDoorActor::Tick(float DeltaTime)
  42. {
  43.     Super::Tick(DeltaTime);
  44.  
  45.     if (InProgress) {
  46.         if (IsRotating)
  47.         {
  48.             CurrentTime += DeltaTime;
  49.             float Alpha = CurrentTime / DoorRotationTime;
  50.             if(Alpha >= 1)
  51.             {
  52.                 Alpha = 1;
  53.                 if(!DoorOpen)
  54.                 {
  55.                     InProgress = false;
  56.                 }
  57.                 IsRotating = false;
  58.                 CurrentTime = 0.f;
  59.             }
  60.             FRotator DoorCenterRotation = FMath::Lerp(CurrentRotation, DesiredRotation, Alpha);
  61.             RotationPoint->SetWorldRotation(DoorCenterRotation);
  62.         }
  63.         else
  64.         {
  65.             CurrentTime += DeltaTime;
  66.             float Alpha = CurrentTime / DoorMovementTime;
  67.             if(Alpha >= 1)
  68.             {
  69.                 Alpha = 1;
  70.                 if(DoorOpen)
  71.                 {
  72.                     InProgress = false;
  73.                 }else
  74.                 {
  75.                     IsRotating = true;
  76.                 }
  77.                 CurrentTime = 0.f;
  78.             }
  79.             FVector DoorPosition = FMath::Lerp(CurrentPosition, DesiredPosition, Alpha);
  80.             Door->SetWorldLocation(DoorPosition);
  81.         }
  82.     }
  83. }
  84.  
  85. void AHeavyDoorActor::InteractWithDoor_Implementation()
  86. {
  87.     if(!InProgress)
  88.     {
  89.         if(DoorOpen)
  90.         {
  91.             CloseDoor();
  92.             DoorOpen = false;
  93.         }
  94.         else
  95.         {
  96.             OpenDoor();
  97.             DoorOpen = true;
  98.         }
  99.     }
  100. }
  101.  
  102. void AHeavyDoorActor::OpenDoor()
  103. {
  104.     UE_LOG(LogTemp, Warning, TEXT("DOOR OPENING"))
  105.     InProgress = true;
  106.     CurrentPosition = DoorClosedPosition;
  107.     DesiredPosition = DoorOpenPosition;
  108.     CurrentRotation = DoorClosedRotation;
  109.     DesiredRotation = DoorOpenRotation;
  110.     CurrentTime = 0;
  111.     IsRotating = true;
  112. }
  113.  
  114. void AHeavyDoorActor::CloseDoor()
  115. {
  116.     UE_LOG(LogTemp, Warning, TEXT("DOOR CLOSING"))
  117.     InProgress = true;
  118.     CurrentPosition = DoorOpenPosition;
  119.     DesiredPosition = DoorClosedPosition;
  120.     CurrentRotation = DoorOpenRotation;
  121.     DesiredRotation = DoorClosedRotation;
  122.     CurrentTime = 0;
  123.     IsRotating = false;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement