-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatingPlatform.cpp
78 lines (62 loc) · 2.05 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Fill out your copyright notice in the Description page of Project Settings.
#include "FloatingPlatform.h"
#include "Components/StaticMeshComponent.h"
#include "TimerManager.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;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
RootComponent = Mesh;
StartPoint = FVector(0.f);
EndPoint = FVector(0.f);
bInterping = false;
InterpSpeed = 4.0f;
InterpTime = 1.f;
//removed worldtimermanager from constructor to beginplay
//vector to scalar distance
Distance = (EndPoint - StartPoint).Size();
}
// Called when the game starts or when spawned
void AFloatingPlatform::BeginPlay()
{
Super::BeginPlay();
StartPoint = GetActorLocation();
EndPoint += StartPoint;
bInterping = false;
GetWorldTimerManager().SetTimer(InterpTimer, this, &AFloatingPlatform::ToggleInterp, InterpTime);
Distance = (EndPoint - StartPoint).Size();
}
// Called every frame
void AFloatingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bInterping)
{
FVector CurrentLocation = GetActorLocation();
FVector Interp = FMath::VInterpTo(CurrentLocation, EndPoint, DeltaTime, InterpSpeed);
SetActorLocation(Interp);
// instance location - start point for midway locations
float DistanceTravelled = (GetActorLocation() - StartPoint).Size();
if (Distance - DistanceTravelled <= 1.f)
{
ToggleInterp();// stop interp very near to the destination
GetWorldTimerManager().SetTimer(InterpTimer, this, &AFloatingPlatform::ToggleInterp, InterpTime);
// swap vectors for reverse interp
SwapVectors(StartPoint, EndPoint);
}
}
}
//interp toggle function
void AFloatingPlatform::ToggleInterp()
{
bInterping = !bInterping;
}
// swap vectors
void AFloatingPlatform::SwapVectors(FVector& VOne, FVector& VTwo)
{
FVector Temp = VOne;
VOne = VTwo;
VTwo = Temp;
}