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 TeleportToFollowLocation (FVector TargetLocation, const float DistanceToLocation, const float HeightOffset)
 
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:278
UNiagaraComponent * NiagaraComponent
Definition: Spectator.h:147
void SetNiagaraComponentVisibility(bool Visible)
Definition: Spectator.cpp:187

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:151

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:138

References OnFollowTargetChanged, and Target.

◆ GetFloatingPawnMovement()

UFloatingPawnMovement * ASpectator::GetFloatingPawnMovement ( )

Get UFloatingPawnMovement from this pawn.

Returns
UFloatingPawnMovement* Pointer, or nullptr if not found.

Definition at line 216 of file Spectator.cpp.

217{
218 return Cast<UFloatingPawnMovement>(GetMovementComponent());
219}

Referenced by GetMaxSpeed(), and SetMaxSpeed().

◆ GetMaxSpeed()

float ASpectator::GetMaxSpeed ( )

Get FloatingPawnMovement current max speed.

Definition at line 204 of file Spectator.cpp.

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

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 91 of file Spectator.h.

92 {
93 return Target.Get();
94 }

◆ IsNiagaraComponentVisible()

bool ASpectator::IsNiagaraComponentVisible ( )
inline

Checks if spectator niagara component is currently visible.

Definition at line 100 of file Spectator.h.

101 {
103 {
104 return NiagaraComponent->IsVisible();
105 }
106
107 return false;
108 }

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 115 of file Spectator.h.

116 {
117 return GetTargetActor() != nullptr;
118 }
AActor * GetTargetActor()
Definition: Spectator.h:91

◆ SetMaxSpeed()

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

Set FloatingPawnMovement max speed.

Definition at line 195 of file Spectator.cpp.

196{
197 UFloatingPawnMovement* FloatingMovement = GetFloatingPawnMovement();
198 if (FloatingMovement)
199 {
200 FloatingMovement->MaxSpeed = MaxSpeed;
201 }
202}

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 187 of file Spectator.cpp.

188{
190 {
191 NiagaraComponent->SetVisibility(Visible);
192 }
193}

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 167 of file Spectator.cpp.

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

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:167

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 155 of file Spectator.cpp.

156{
157 if (!IsValid(Actor))
158 {
159 return;
160 }
161
162 FVector TargetLocation = Actor->GetActorLocation();
163
164 TeleportToFollowLocation(TargetLocation, DistanceToActor, HeightOffset);
165}
void TeleportToFollowLocation(FVector TargetLocation, const float DistanceToLocation, const float HeightOffset)
Definition: Spectator.cpp:133

References TeleportToFollowLocation().

◆ 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.

◆ TeleportToFollowLocation()

void ASpectator::TeleportToFollowLocation ( FVector  TargetLocation,
const float  DistanceToLocation,
const float  HeightOffset 
)

Teleports the spectator to the follow location of a specific location

Definition at line 133 of file Spectator.cpp.

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

Referenced by TeleportToActorFollowLocation().

◆ TeleportToLevelTeleportLocation()

void ASpectator::TeleportToLevelTeleportLocation ( FLevelTeleportLocation  TeleportLocation)

Teleport specator to level teleport location

Parameters
TeleportLocation

Definition at line 222 of file Spectator.cpp.

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

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

Member Data Documentation

◆ NiagaraComponent

UNiagaraComponent* ASpectator::NiagaraComponent = nullptr
protected

Definition at line 147 of file Spectator.h.

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

◆ OnFollowTargetChanged

FFollowTargetChanged ASpectator::OnFollowTargetChanged

Event triggered when followed actor changed

Definition at line 138 of file Spectator.h.

Referenced by FollowActor(), and StopFollowingAnyActor().

◆ Target

TWeakObjectPtr<AActor> ASpectator::Target
private

Definition at line 151 of file Spectator.h.

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


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