Agrarsense
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
ASpectator Class Reference

#include <Spectator.h>

Inheritance diagram for ASpectator:
Inheritance graph
[legend]
Collaboration diagram for ASpectator:
Collaboration graph
[legend]

Public Member Functions

void TeleportSpectator (const FTransform &Transform)
 
void TeleportToActorLocation (const AActor *Actor)
 
void FollowActor (AActor *Actor, bool DisableInputs=true, const float DistanceToActor=150.0f, const float HeightOffset=10.0f)
 
void TeleportToActorFollowLocation (AActor *Actor, const float DistanceToActor=1500.0f, const float HeightOffset=500.0f)
 
void StopFollowingAnyActor (bool EnableInputs=true)
 
void SetNiagaraComponentVisibility (bool Visible)
 
void SetMaxSpeed (float MaxSpeed=1500.0f)
 
float GetMaxSpeed ()
 
AActor * GetTargetActor ()
 
bool IsNiagaraComponentVisible ()
 
bool IsSpectatorFollowingAnyActor ()
 
UFloatingPawnMovement * GetFloatingPawnMovement ()
 
void TeleportToLevelTeleportLocation (FLevelTeleportLocation TeleportLocation)
 

Public Attributes

FFollowTargetChanged OnFollowTargetChanged
 

Protected Member Functions

virtual void BeginPlay () override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 

Protected Attributes

UNiagaraComponent * NiagaraComponent = nullptr
 

Private Attributes

TWeakObjectPtr< AActor > Target
 

Detailed Description

Spectator Actor class for flying (WASD) movement. BP_Spectator blueprint is based on this Actor.

Definition at line 23 of file Spectator.h.

Member Function Documentation

◆ BeginPlay()

void ASpectator::BeginPlay ( )
overrideprotectedvirtual

Definition at line 15 of file Spectator.cpp.

16{
17 Super::BeginPlay();
18
19 // Load and setup rain/snow fall niagara system for this spectator
20 UNiagaraSystem* NiagaraSystem = LoadObject<UNiagaraSystem>(nullptr, TEXT("/Game/Agrarsense/Particles/WaterAndSnow/NS_Particles.NS_Particles"));
21 if (NiagaraSystem)
22 {
23 NiagaraComponent = UNiagaraFunctionLibrary::SpawnSystemAttached(NiagaraSystem, this->GetRootComponent(),
24 FName("NiagaraEmitterSocketName"),
25 FVector(0.0, 0.0, 150.0f), FRotator::ZeroRotator, EAttachLocation::KeepRelativeOffset, true);
26
27 ASensor::HideComponentForAllCameras(Cast<UPrimitiveComponent>(NiagaraComponent));
28 }
29
30 // Disable niagara component
32}
static void HideComponentForAllCameras(UPrimitiveComponent *PrimitiveComponent)
Definition: Sensor.cpp:255
UNiagaraComponent * NiagaraComponent
Definition: Spectator.h:140
void SetNiagaraComponentVisibility(bool Visible)
Definition: Spectator.cpp:183

References ASensor::HideComponentForAllCameras(), NiagaraComponent, and SetNiagaraComponentVisibility().

◆ EndPlay()

void ASpectator::EndPlay ( const EEndPlayReason::Type  EndPlayReason)
overrideprotectedvirtual

Definition at line 34 of file Spectator.cpp.

35{
36 Super::EndPlay(EndPlayReason);
37
39 {
40 NiagaraComponent->UnregisterComponent();
41 NiagaraComponent->DestroyComponent();
42 NiagaraComponent = nullptr;
43 }
44
45 Target.Reset();
46}
TWeakObjectPtr< AActor > Target
Definition: Spectator.h:144

References NiagaraComponent, and Target.

◆ FollowActor()

void ASpectator::FollowActor ( AActor *  Actor,
bool  DisableInputs = true,
const float  DistanceToActor = 150.0f,
const float  HeightOffset = 10.0f 
)

Makes the spectator a child of the given actor and starts following it.

Definition at line 87 of file Spectator.cpp.

88{
89 Target = Actor;
90
91 if (!Target.IsValid())
92 {
93 return;
94 }
95
96 // Detach from any previous target
97 DetachFromActor(FDetachmentTransformRules::KeepRelativeTransform);
98
99 // Set the offset to position the spectator slightly above and behind the actor
100 FVector Offset = FVector(0.0f, 0.0f, HeightOffset);
101
102 FVector TargetActorLocation = Actor->GetActorLocation();
103
104 FVector DirectionToActor = TargetActorLocation - GetActorLocation();
105 DirectionToActor.Normalize();
106
107 // Face the camera slightly down toward the actor
108 APlayerController* PlayerController = Cast<APlayerController>(GetController());
109 if (PlayerController)
110 {
111 if (DisableInputs)
112 {
113 DisableInput(PlayerController);
114 }
115 FRotator NewRotation = DirectionToActor.ToOrientationRotator();
116 PlayerController->SetControlRotation(NewRotation);
117 }
118
119 // Calculate the new location to be closer behind the actor
120 FVector NewLocation = TargetActorLocation + DirectionToActor * -DistanceToActor + Offset;
121
122 // Attach to the new target actor's root component with the offset
123 USceneComponent* Root = Actor->GetRootComponent();
124 if (Root)
125 {
126 AttachToComponent(Root, FAttachmentTransformRules::KeepRelativeTransform);
127 SetActorLocation(NewLocation);
128 }
129
130 OnFollowTargetChanged.Broadcast(true, Target.Get());
131}
FFollowTargetChanged OnFollowTargetChanged
Definition: Spectator.h:131

References OnFollowTargetChanged, and Target.

◆ GetFloatingPawnMovement()

UFloatingPawnMovement * ASpectator::GetFloatingPawnMovement ( )

Get UFloatingPawnMovement from this pawn.

Returns
UFloatingPawnMovement* Pointer, or nullptr if not found.

Definition at line 212 of file Spectator.cpp.

213{
214 return Cast<UFloatingPawnMovement>(GetMovementComponent());
215}

Referenced by GetMaxSpeed(), and SetMaxSpeed().

◆ GetMaxSpeed()

float ASpectator::GetMaxSpeed ( )

Get FloatingPawnMovement current max speed.

Definition at line 200 of file Spectator.cpp.

201{
202 UFloatingPawnMovement* FloatingMovement = GetFloatingPawnMovement();
203 if (FloatingMovement)
204 {
205 return FloatingMovement->MaxSpeed;
206 }
207
208 // Else return default value of 1500.0f (set in BP_spectator blueprint)
209 return 1500.0f;
210}
UFloatingPawnMovement * GetFloatingPawnMovement()
Definition: Spectator.cpp:212

References GetFloatingPawnMovement().

Referenced by UAgrarsenseSettings::SetSpectatorMaxSpeed().

◆ GetTargetActor()

AActor * ASpectator::GetTargetActor ( )
inline

Gets the actor that the spectator is currently following.

Returns
Pointer to the actor being followed, or nullptr if not following any.

Definition at line 84 of file Spectator.h.

85 {
86 return Target.Get();
87 }

◆ IsNiagaraComponentVisible()

bool ASpectator::IsNiagaraComponentVisible ( )
inline

Checks if spectator niagara component is currently visible.

Definition at line 93 of file Spectator.h.

94 {
96 {
97 return NiagaraComponent->IsVisible();
98 }
99
100 return false;
101 }

Referenced by UAgrarsenseSettings::SetShowSpectatorRainAndSnowfall().

◆ IsSpectatorFollowingAnyActor()

bool ASpectator::IsSpectatorFollowingAnyActor ( )
inline

Checks if the spectator is currently following any actor.

Returns
True if following an actor, false otherwise.

Definition at line 108 of file Spectator.h.

109 {
110 return GetTargetActor() != nullptr;
111 }
AActor * GetTargetActor()
Definition: Spectator.h:84

◆ SetMaxSpeed()

void ASpectator::SetMaxSpeed ( float  MaxSpeed = 1500.0f)

Set FloatingPawnMovement max speed.

Definition at line 191 of file Spectator.cpp.

192{
193 UFloatingPawnMovement* FloatingMovement = GetFloatingPawnMovement();
194 if (FloatingMovement)
195 {
196 FloatingMovement->MaxSpeed = MaxSpeed;
197 }
198}

References GetFloatingPawnMovement().

Referenced by UAgrarsenseSettings::SetSpectatorMaxSpeed().

◆ SetNiagaraComponentVisibility()

void ASpectator::SetNiagaraComponentVisibility ( bool  Visible)

Set Spectator niagara component visibility (rain/snow fall).

Parameters
VisibleIf true, enables spectator Niagara component. Else disables it.

Definition at line 183 of file Spectator.cpp.

184{
186 {
187 NiagaraComponent->SetVisibility(Visible);
188 }
189}

References NiagaraComponent.

Referenced by BeginPlay(), and UAgrarsenseSettings::SetShowSpectatorRainAndSnowfall().

◆ StopFollowingAnyActor()

void ASpectator::StopFollowingAnyActor ( bool  EnableInputs = true)

Stops the spectator from following any actor.

Parameters
EnableInputsIf true, enables spectator inputs after stopping.

Definition at line 163 of file Spectator.cpp.

164{
165 // Detach from any previous target
166 DetachFromActor(FDetachmentTransformRules::KeepRelativeTransform);
167
168 Target.Reset();
169
170 if (EnableInputs)
171 {
172 APlayerController* PlayerController = Cast<APlayerController>(GetController());
173
174 if (PlayerController)
175 {
176 EnableInput(PlayerController);
177 }
178 }
179
180 OnFollowTargetChanged.Broadcast(false, Target.Get());
181}

References OnFollowTargetChanged, and Target.

Referenced by TeleportSpectator(), and TeleportToLevelTeleportLocation().

◆ TeleportSpectator()

void ASpectator::TeleportSpectator ( const FTransform &  Transform)

Teleports the spectator to the specified transform.

Definition at line 48 of file Spectator.cpp.

49{
50 if (!IsPlayerControlled())
51 {
52 return;
53 }
54
55 // Detach from any Actor and enable inputs.
57
58 FVector TargetLocation = Transform.GetLocation();
59 FVector Direction = GetActorLocation() - TargetLocation;
60 Direction.Normalize();
61
62 // 20 units away from the target so we are not top if it..
63 FVector NewLocation = TargetLocation + Direction * 20.0f;
64
65 FRotator NewRotation = (TargetLocation - NewLocation).ToOrientationRotator();
66
67 SetActorLocationAndRotation(NewLocation, NewRotation);
68
69 // Set the player controller rotation to match the incoming Transform angle,
70 // This actually sets the camera angle.
71 APlayerController* PlayerController = Cast<APlayerController>(GetController());
72 if (PlayerController)
73 {
74 PlayerController->SetControlRotation(Transform.Rotator());
75 }
76}
void StopFollowingAnyActor(bool EnableInputs=true)
Definition: Spectator.cpp:163

References StopFollowingAnyActor(), and Transform.

Referenced by UROSCommands::HandleTeleportSpectator(), USimulatorJsonParser::TeleportSpectatorOrFollowActorIfField(), and TeleportToActorLocation().

◆ TeleportToActorFollowLocation()

void ASpectator::TeleportToActorFollowLocation ( AActor *  Actor,
const float  DistanceToActor = 1500.0f,
const float  HeightOffset = 500.0f 
)

Teleports the spectator to the follow location of a specific actor

Definition at line 133 of file Spectator.cpp.

134{
135 if (!IsValid(Actor))
136 {
137 return;
138 }
139
140 // Set the offset to position the spectator slightly above and behind the actor
141 FVector Offset = FVector(0.0f, 0.0f, HeightOffset);
142
143 FVector TargetActorLocation = Actor->GetActorLocation();
144
145 FVector DirectionToActor = TargetActorLocation - GetActorLocation();
146 DirectionToActor.Normalize();
147
148 // Face the camera slightly down toward the actor
149 APlayerController* PlayerController = Cast<APlayerController>(GetController());
150 if (PlayerController)
151 {
152 FRotator NewRotation = DirectionToActor.ToOrientationRotator();
153 PlayerController->SetControlRotation(NewRotation);
154 }
155
156 // Calculate the new location to be closer behind the actor
157 FVector NewLocation = TargetActorLocation + DirectionToActor * -DistanceToActor + Offset;
158
159 SetActorLocation(NewLocation);
160
161}

◆ TeleportToActorLocation()

void ASpectator::TeleportToActorLocation ( const AActor *  Actor)

Teleports the spectator to the location of a specific actor.

Definition at line 78 of file Spectator.cpp.

79{
80 if (Actor)
81 {
82 FTransform Transform = Actor->GetActorTransform();
84 }
85}
void TeleportSpectator(const FTransform &Transform)
Definition: Spectator.cpp:48

References TeleportSpectator(), and Transform.

◆ TeleportToLevelTeleportLocation()

void ASpectator::TeleportToLevelTeleportLocation ( FLevelTeleportLocation  TeleportLocation)

Teleport specator to level teleport location

Parameters
TeleportLocation

Definition at line 218 of file Spectator.cpp.

219{
220
221 if (!IsPlayerControlled())
222 {
223 return;
224 }
225
226 FTransform Transform = TeleportLocation.TeleportLocationTransform;
227
228 // Detach from any Actor and enable inputs.
230
231 SetActorLocationAndRotation(Transform.GetLocation(), Transform.GetRotation().Rotator());
232
233 // Set the player controller rotation to match the incoming Transform angle,
234 // This actually sets the camera angle.
235 APlayerController* PlayerController = Cast<APlayerController>(GetController());
236 if (PlayerController)
237 {
238 PlayerController->SetControlRotation(Transform.Rotator());
239 }
240}

References StopFollowingAnyActor(), FLevelTeleportLocation::TeleportLocationTransform, and Transform.

Member Data Documentation

◆ NiagaraComponent

UNiagaraComponent* ASpectator::NiagaraComponent = nullptr
protected

Definition at line 140 of file Spectator.h.

Referenced by BeginPlay(), EndPlay(), and SetNiagaraComponentVisibility().

◆ OnFollowTargetChanged

FFollowTargetChanged ASpectator::OnFollowTargetChanged

Event triggered when followed actor changed

Definition at line 131 of file Spectator.h.

Referenced by FollowActor(), and StopFollowingAnyActor().

◆ Target

TWeakObjectPtr<AActor> ASpectator::Target
private

Definition at line 144 of file Spectator.h.

Referenced by EndPlay(), FollowActor(), and StopFollowingAnyActor().


The documentation for this class was generated from the following files: