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 ()
 
AOverlapSensorGetOverlapSensor () const
 
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
 
AOverlapSensorOverlapSensor = 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 25 of file Spectator.h.

Member Function Documentation

◆ BeginPlay()

void ASpectator::BeginPlay ( )
overrideprotectedvirtual

Definition at line 17 of file Spectator.cpp.

18{
19 Super::BeginPlay();
20
21 // Load and setup rain/snow fall niagara system for this spectator
22 UNiagaraSystem* NiagaraSystem = LoadObject<UNiagaraSystem>(nullptr, TEXT("/Game/Agrarsense/Particles/WaterAndSnow/NS_Particles.NS_Particles"));
23 if (NiagaraSystem)
24 {
25 NiagaraComponent = UNiagaraFunctionLibrary::SpawnSystemAttached(NiagaraSystem, this->GetRootComponent(),
26 FName("NiagaraEmitterSocketName"),
27 FVector(0.0, 0.0, 150.0f), FRotator::ZeroRotator, EAttachLocation::KeepRelativeOffset, true);
28
29 ASensor::HideComponentForAllCameras(Cast<UPrimitiveComponent>(NiagaraComponent));
30 }
31
32 // Disable niagara component
34
35 FVector RelativePosition = FVector(0.0f, 0.0f, 0.0f);
36 float OverlapBoundsSize = 15.0f;
37
38 // Create OverlapSensor
40 Params.OwningActor = this;
41 Params.Size = FVector(OverlapBoundsSize, OverlapBoundsSize, OverlapBoundsSize);
42 Params.RelativePosition = RelativePosition;
43 Params.AllChannels = true;
44 FString OverlapSensorID = "spectator/overlap";
45 OverlapSensor = USensorFactory::SpawnOverlapSensor(GetActorTransform(), Params, OverlapSensorID, "overlap", this);
46}
static void HideComponentForAllCameras(UPrimitiveComponent *PrimitiveComponent)
Definition: Sensor.cpp:308
AOverlapSensor * OverlapSensor
Definition: Spectator.h:158
UNiagaraComponent * NiagaraComponent
Definition: Spectator.h:155
void SetNiagaraComponentVisibility(bool Visible)
Definition: Spectator.cpp:207
static AOverlapSensor * SpawnOverlapSensor(const FTransform &transform, FOverlapSensorParameters Parameters, const FString sensorIdentifier, const FString sensorName, AActor *Parent=nullptr)

References FOverlapSensorParameters::AllChannels, ASensor::HideComponentForAllCameras(), NiagaraComponent, OverlapSensor, FOverlapSensorParameters::OwningActor, FOverlapSensorParameters::RelativePosition, SetNiagaraComponentVisibility(), FOverlapSensorParameters::Size, and USensorFactory::SpawnOverlapSensor().

◆ EndPlay()

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

Definition at line 48 of file Spectator.cpp.

49{
50 Super::EndPlay(EndPlayReason);
51
53 {
54 NiagaraComponent->UnregisterComponent();
55 NiagaraComponent->DestroyComponent();
56 NiagaraComponent = nullptr;
57 }
58
59 if (OverlapSensor)
60 {
61 OverlapSensor->Destroy();
62 OverlapSensor = nullptr;
63 }
64
65 Target.Reset();
66}
TWeakObjectPtr< AActor > Target
Definition: Spectator.h:162

References NiagaraComponent, OverlapSensor, 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 107 of file Spectator.cpp.

108{
109 Target = Actor;
110
111 if (!Target.IsValid())
112 {
113 return;
114 }
115
116 // Detach from any previous target
117 DetachFromActor(FDetachmentTransformRules::KeepRelativeTransform);
118
119 // Set the offset to position the spectator slightly above and behind the actor
120 FVector Offset = FVector(0.0f, 0.0f, HeightOffset);
121
122 FVector TargetActorLocation = Actor->GetActorLocation();
123
124 FVector DirectionToActor = TargetActorLocation - GetActorLocation();
125 DirectionToActor.Normalize();
126
127 // Face the camera slightly down toward the actor
128 APlayerController* PlayerController = Cast<APlayerController>(GetController());
129 if (PlayerController)
130 {
131 if (DisableInputs)
132 {
133 DisableInput(PlayerController);
134 }
135 FRotator NewRotation = DirectionToActor.ToOrientationRotator();
136 PlayerController->SetControlRotation(NewRotation);
137 }
138
139 // Calculate the new location to be closer behind the actor
140 FVector NewLocation = TargetActorLocation + DirectionToActor * -DistanceToActor + Offset;
141
142 // Attach to the new target actor's root component with the offset
143 USceneComponent* Root = Actor->GetRootComponent();
144 if (Root)
145 {
146 AttachToComponent(Root, FAttachmentTransformRules::KeepRelativeTransform);
147 SetActorLocation(NewLocation);
148 }
149
150 OnFollowTargetChanged.Broadcast(true, Target.Get());
151}
FFollowTargetChanged OnFollowTargetChanged
Definition: Spectator.h:146

References OnFollowTargetChanged, and Target.

◆ GetFloatingPawnMovement()

UFloatingPawnMovement * ASpectator::GetFloatingPawnMovement ( )

Get UFloatingPawnMovement from this pawn.

Returns
UFloatingPawnMovement* Pointer, or nullptr if not found.

Definition at line 236 of file Spectator.cpp.

237{
238 return Cast<UFloatingPawnMovement>(GetMovementComponent());
239}

Referenced by GetMaxSpeed(), and SetMaxSpeed().

◆ GetMaxSpeed()

float ASpectator::GetMaxSpeed ( )

Get FloatingPawnMovement current max speed.

Definition at line 224 of file Spectator.cpp.

225{
226 UFloatingPawnMovement* FloatingMovement = GetFloatingPawnMovement();
227 if (FloatingMovement)
228 {
229 return FloatingMovement->MaxSpeed;
230 }
231
232 // Else return default value of 1500.0f (set in BP_spectator blueprint)
233 return 1500.0f;
234}
UFloatingPawnMovement * GetFloatingPawnMovement()
Definition: Spectator.cpp:236

References GetFloatingPawnMovement().

Referenced by UAgrarsenseSettings::SetSpectatorMaxSpeed().

◆ GetOverlapSensor()

AOverlapSensor * ASpectator::GetOverlapSensor ( ) const
inline

Definition at line 123 of file Spectator.h.

124 {
125 return OverlapSensor;
126 }

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

94 {
95 return Target.Get();
96 }

◆ IsNiagaraComponentVisible()

bool ASpectator::IsNiagaraComponentVisible ( )
inline

Checks if spectator niagara component is currently visible.

Definition at line 102 of file Spectator.h.

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

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

118 {
119 return GetTargetActor() != nullptr;
120 }
AActor * GetTargetActor()
Definition: Spectator.h:93

◆ SetMaxSpeed()

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

Set FloatingPawnMovement max speed.

Definition at line 215 of file Spectator.cpp.

216{
217 UFloatingPawnMovement* FloatingMovement = GetFloatingPawnMovement();
218 if (FloatingMovement)
219 {
220 FloatingMovement->MaxSpeed = MaxSpeed;
221 }
222}

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

208{
210 {
211 NiagaraComponent->SetVisibility(Visible);
212 }
213}

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

188{
189 // Detach from any previous target
190 DetachFromActor(FDetachmentTransformRules::KeepRelativeTransform);
191
192 Target.Reset();
193
194 if (EnableInputs)
195 {
196 APlayerController* PlayerController = Cast<APlayerController>(GetController());
197
198 if (PlayerController)
199 {
200 EnableInput(PlayerController);
201 }
202 }
203
204 OnFollowTargetChanged.Broadcast(false, Target.Get());
205}

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

69{
70 if (!IsPlayerControlled())
71 {
72 return;
73 }
74
75 // Detach from any Actor and enable inputs.
77
78 FVector TargetLocation = Transform.GetLocation();
79 FVector Direction = GetActorLocation() - TargetLocation;
80 Direction.Normalize();
81
82 // 20 units away from the target so we are not top if it..
83 FVector NewLocation = TargetLocation + Direction * 20.0f;
84
85 FRotator NewRotation = (TargetLocation - NewLocation).ToOrientationRotator();
86
87 SetActorLocationAndRotation(NewLocation, NewRotation);
88
89 // Set the player controller rotation to match the incoming Transform angle,
90 // This actually sets the camera angle.
91 APlayerController* PlayerController = Cast<APlayerController>(GetController());
92 if (PlayerController)
93 {
94 PlayerController->SetControlRotation(Transform.Rotator());
95 }
96}
void StopFollowingAnyActor(bool EnableInputs=true)
Definition: Spectator.cpp:187

References StopFollowingAnyActor(), and Transform.

Referenced by USimulatorJsonParser::HandleSpectatorMovement(), UROSCommands::HandleTeleportSpectator(), 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 175 of file Spectator.cpp.

176{
177 if (!IsValid(Actor))
178 {
179 return;
180 }
181
182 FVector TargetLocation = Actor->GetActorLocation();
183
184 TeleportToFollowLocation(TargetLocation, DistanceToActor, HeightOffset);
185}
void TeleportToFollowLocation(FVector TargetLocation, const float DistanceToLocation, const float HeightOffset)
Definition: Spectator.cpp:153

References TeleportToFollowLocation().

◆ TeleportToActorLocation()

void ASpectator::TeleportToActorLocation ( const AActor *  Actor)

Teleports the spectator to the location of a specific actor.

Definition at line 98 of file Spectator.cpp.

99{
100 if (Actor)
101 {
102 FTransform Transform = Actor->GetActorTransform();
104 }
105}
void TeleportSpectator(const FTransform &Transform)
Definition: Spectator.cpp:68

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

154{
155 // Set the offset to position the spectator slightly above and behind the actor
156 FVector Offset = FVector(0.0f, 0.0f, HeightOffset);
157
158 FVector DirectionToActor = TargetLocation - GetActorLocation();
159 DirectionToActor.Normalize();
160
161 // Face the camera slightly down toward the actor
162 APlayerController* PlayerController = Cast<APlayerController>(GetController());
163 if (PlayerController)
164 {
165 FRotator NewRotation = DirectionToActor.ToOrientationRotator();
166 PlayerController->SetControlRotation(NewRotation);
167 }
168
169 // Calculate the new location to be closer behind the actor
170 FVector NewLocation = TargetLocation + DirectionToActor * -DistanceToLocation + Offset;
171
172 SetActorLocation(NewLocation);
173}

Referenced by TeleportToActorFollowLocation().

◆ TeleportToLevelTeleportLocation()

void ASpectator::TeleportToLevelTeleportLocation ( FLevelTeleportLocation  TeleportLocation)

Teleport specator to level teleport location

Parameters
TeleportLocation

Definition at line 242 of file Spectator.cpp.

243{
244
245 if (!IsPlayerControlled())
246 {
247 return;
248 }
249
250 FTransform Transform = TeleportLocation.TeleportLocationTransform;
251
252 // Detach from any Actor and enable inputs.
254
255 SetActorLocationAndRotation(Transform.GetLocation(), Transform.GetRotation().Rotator());
256
257 // Set the player controller rotation to match the incoming Transform angle,
258 // This actually sets the camera angle.
259 APlayerController* PlayerController = Cast<APlayerController>(GetController());
260 if (PlayerController)
261 {
262 PlayerController->SetControlRotation(Transform.Rotator());
263 }
264}

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

Member Data Documentation

◆ NiagaraComponent

UNiagaraComponent* ASpectator::NiagaraComponent = nullptr
protected

Definition at line 155 of file Spectator.h.

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

◆ OnFollowTargetChanged

FFollowTargetChanged ASpectator::OnFollowTargetChanged

Event triggered when followed actor changed

Definition at line 146 of file Spectator.h.

Referenced by FollowActor(), and StopFollowingAnyActor().

◆ OverlapSensor

AOverlapSensor* ASpectator::OverlapSensor = nullptr
protected

Definition at line 158 of file Spectator.h.

Referenced by BeginPlay(), and EndPlay().

◆ Target

TWeakObjectPtr<AActor> ASpectator::Target
private

Definition at line 162 of file Spectator.h.

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


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