Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "HeavyDoorActor.h"
- #include "Runtime/Engine/Classes/Components/StaticMeshComponent.h"
- // Sets default values
- AHeavyDoorActor::AHeavyDoorActor()
- {
- // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- Door = CreateDefaultSubobject<UStaticMeshComponent>(FName("Door"));
- Door->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
- RotationPoint = CreateDefaultSubobject<USceneComponent>(FName("Rotation Point"));
- RotationPoint->AttachToComponent(Door, FAttachmentTransformRules::KeepRelativeTransform);
- DoorCenter = CreateDefaultSubobject<UStaticMeshComponent>(FName("Door Center"));
- DoorCenter->AttachToComponent(RotationPoint, FAttachmentTransformRules::KeepRelativeTransform);
- }
- // Called when the game starts or when spawned
- void AHeavyDoorActor::BeginPlay()
- {
- Super::BeginPlay();
- DoorClosedPosition = Door->GetComponentLocation();
- if(DoorOpensUp)
- {
- DoorOpenPosition = DoorClosedPosition + (DoorMovementAmount * GetActorUpVector());
- }else
- {
- DoorOpenPosition = DoorClosedPosition + (DoorMovementAmount * GetActorUpVector() * -1);
- }
- DoorClosedRotation = RotationPoint->GetComponentRotation();
- DoorOpenRotation = DoorClosedRotation;
- DoorOpenRotation.Pitch += 180;
- }
- // Called every frame
- void AHeavyDoorActor::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- if (InProgress) {
- if (IsRotating)
- {
- CurrentTime += DeltaTime;
- float Alpha = CurrentTime / DoorRotationTime;
- if(Alpha >= 1)
- {
- Alpha = 1;
- if(!DoorOpen)
- {
- InProgress = false;
- }
- IsRotating = false;
- CurrentTime = 0.f;
- }
- FRotator DoorCenterRotation = FMath::Lerp(CurrentRotation, DesiredRotation, Alpha);
- RotationPoint->SetWorldRotation(DoorCenterRotation);
- }
- else
- {
- CurrentTime += DeltaTime;
- float Alpha = CurrentTime / DoorMovementTime;
- if(Alpha >= 1)
- {
- Alpha = 1;
- if(DoorOpen)
- {
- InProgress = false;
- }else
- {
- IsRotating = true;
- }
- CurrentTime = 0.f;
- }
- FVector DoorPosition = FMath::Lerp(CurrentPosition, DesiredPosition, Alpha);
- Door->SetWorldLocation(DoorPosition);
- }
- }
- }
- void AHeavyDoorActor::InteractWithDoor_Implementation()
- {
- if(!InProgress)
- {
- if(DoorOpen)
- {
- CloseDoor();
- DoorOpen = false;
- }
- else
- {
- OpenDoor();
- DoorOpen = true;
- }
- }
- }
- void AHeavyDoorActor::OpenDoor()
- {
- UE_LOG(LogTemp, Warning, TEXT("DOOR OPENING"))
- InProgress = true;
- CurrentPosition = DoorClosedPosition;
- DesiredPosition = DoorOpenPosition;
- CurrentRotation = DoorClosedRotation;
- DesiredRotation = DoorOpenRotation;
- CurrentTime = 0;
- IsRotating = true;
- }
- void AHeavyDoorActor::CloseDoor()
- {
- UE_LOG(LogTemp, Warning, TEXT("DOOR CLOSING"))
- InProgress = true;
- CurrentPosition = DoorOpenPosition;
- DesiredPosition = DoorClosedPosition;
- CurrentRotation = DoorOpenRotation;
- DesiredRotation = DoorClosedRotation;
- CurrentTime = 0;
- IsRotating = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement