-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatingPlatform.cpp
63 lines (56 loc) · 1.83 KB
/
FloatingPlatform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Fill out your copyright notice in the Description page of Project Settings.
#include "FloatingPlatform.h"
// Sets default values
AFloatingPlatform::AFloatingPlatform()
{
// 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;
RootComponent = CreateDefaultSubobject<USceneComponent>("RootComponent");
PlatformMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlatformMesh"));
PlatformMesh->SetupAttachment(GetRootComponent());
StartPoint = FVector::ZeroVector;
EndPoint = FVector::ZeroVector;
InterpTime = 1.f;
TimeElapsedSinceLastStop = 0.f;
bShouldInterp = true;
WaitingTime = 2.f;
DistanceToTravel = 0.f;
}
// Called when the game starts or when spawned
void AFloatingPlatform::BeginPlay()
{
Super::BeginPlay();
StartPoint = GetActorLocation();
DistanceToTravel = EndPoint.Size();
UnitVectorInDirectionOfTravel = EndPoint.GetSafeNormal();
EndPoint += StartPoint;
}
// Called every frame
void AFloatingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (TimeElapsedSinceLastStop >= InterpTime)
{
TimeElapsedSinceLastStop = 0;
SwapVectors(StartPoint, EndPoint);
bShouldInterp = false;
GetWorldTimerManager().SetTimer(InterpTimer, this, &AFloatingPlatform::ToggleInterp, WaitingTime);
}
if (bShouldInterp)
{
TimeElapsedSinceLastStop += DeltaTime;
float DesiredTotalDistance = DistanceToTravel * TimeElapsedSinceLastStop / InterpTime;
SetActorLocation(StartPoint + UnitVectorInDirectionOfTravel*DesiredTotalDistance);
}
}
void AFloatingPlatform::SwapVectors(FVector& Vec1, FVector& Vec2)
{
FVector Temp = Vec1;
Vec1 = Vec2;
Vec2 = Temp;
UnitVectorInDirectionOfTravel *= -1;
}
void AFloatingPlatform::ToggleInterp()
{
bShouldInterp = !bShouldInterp;
}