-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenDoor.cpp
More file actions
136 lines (111 loc) · 3.6 KB
/
OpenDoor.cpp
File metadata and controls
136 lines (111 loc) · 3.6 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//Copyright Christine Coomans 2021
#include "OpenDoor.h"
#include "Components/PrimitiveComponent.h"
#include "Components/AudioComponent.h"
#include "Engine/World.h"
#include "GameFramework/Actor.h"
#include "GameFramework/PlayerController.h"
// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
// Set this component to be initialized when the game starts, and to be ticked every frame.
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UOpenDoor::BeginPlay()
{
Super::BeginPlay();
//set absolute values for door rotation upon game start
InitialYaw = GetOwner()->GetActorRotation().Yaw;
CurrentYaw = InitialYaw;
OpenAngle += InitialYaw;
//Finds Pressure Plate
FindPressurePlate();
//Finds Audio
FindAudioComponent();
}
// Audio
void UOpenDoor::FindAudioComponent()
{
AudioComponent = GetOwner()->FindComponentByClass<UAudioComponent>();
if(!AudioComponent){
UE_LOG(LogTemp, Error, TEXT("%s Missing Audio Component."), *GetOwner()->GetName());
}
}
// Pressure Plate
void UOpenDoor::FindPressurePlate()
{
if(!PressurePlate){
UE_LOG(LogTemp, Error, TEXT("%s has the open door component on it, but no pressureplate set!"), *GetOwner()->GetName());
}
}
// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
//Opening the door to x degrees upon game start
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
//Calls OpenDoor function
if (TotalMassOfActors() > MassToOpenDoors)
{
//Opens Door In Trigger Zone
OpenDoor(DeltaTime);
//Sets Timer Count Seconds Door Is Open
DoorLastOpened = GetWorld()->GetTimeSeconds();
}
else {
//Close Door After A Few Seconds
if (GetWorld()->GetTimeSeconds() - DoorLastOpened > DoorCloseDelay) {
CloseDoor(DeltaTime);
}
}
}
// Opens the door
void UOpenDoor::OpenDoor(float DeltaTime)
{
//Gets current location/rotation value of door for console
UE_LOG(LogTemp, Warning, TEXT("%s"), *GetOwner()->GetActorRotation().ToString());
//Gets set yaw(opening degree) value for door for console
UE_LOG(LogTemp, Warning, TEXT("Yaw is %f"), GetOwner()->GetActorRotation().Yaw);
//updates current door rotation to rotate (DeltaTime * DoorOpenSpeed will solve fps crash problems)
CurrentYaw = FMath::Lerp(CurrentYaw, OpenAngle, DeltaTime * DoorOpenSpeed);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
//rotates the door x degrees during x seconds
GetOwner()->SetActorRotation(DoorRotation);
//Plays Open Door Audio
CloseDoorSound = false;
if (!AudioComponent) { return;}
if (!OpenDoorSound) {
AudioComponent->Play();
OpenDoorSound = true;
}
}
// Opens the door
void UOpenDoor::CloseDoor(float DeltaTime)
{
//updates current door rotation to rotate shut much faster than it opens
CurrentYaw = FMath::Lerp(CurrentYaw, InitialYaw, DeltaTime * DoorCloseSpeed);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
//rotates the door x degrees during x seconds
GetOwner()->SetActorRotation(DoorRotation);
//Plays Open Door Audio
OpenDoorSound = false;
if (!AudioComponent) { return;}
if (!CloseDoorSound) {
AudioComponent->Play();
CloseDoorSound = true;
}
}
//Total Mass Needed To Open Door
float UOpenDoor::TotalMassOfActors() const
{
float TotalMass = 0.f;
TArray<AActor*> OverlappingActors;
if(!PressurePlate) { return TotalMass;}
PressurePlate->GetOverlappingActors(OverlappingActors);
for(AActor* Actor : OverlappingActors) {
TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
}
return TotalMass;
}