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 // Create OverlapSensor
36 FSensorSpawnParameters SpawnParams;
37 SpawnParams.Transform = GetActorTransform();
38 SpawnParams.SensorIdentifier = "spectator/overlap";
39 SpawnParams.SensorName = "overlap";
40 SpawnParams.SimulateSensor = true;
41 SpawnParams.Parent = this;
42
43 FVector RelativePosition = FVector(0.0f, 0.0f, 0.0f);
44 float OverlapBoundsSize = 15.0f;
45
46 FOverlapSensorParameters SensorParams;
47 SensorParams.OwningActor = this;
48 SensorParams.Size = FVector(OverlapBoundsSize, OverlapBoundsSize, OverlapBoundsSize);
49 SensorParams.RelativePosition = RelativePosition;
50 SensorParams.AllChannels = true;
51
52 OverlapSensor = USensorFactory::SpawnOverlapSensor(SpawnParams, SensorParams);
53}
static void HideComponentForAllCameras(UPrimitiveComponent *PrimitiveComponent)
Definition: Sensor.cpp:315
AOverlapSensor * OverlapSensor
Definition: Spectator.h:158
UNiagaraComponent * NiagaraComponent
Definition: Spectator.h:155
void SetNiagaraComponentVisibility(bool Visible)
Definition: Spectator.cpp:214
static AOverlapSensor * SpawnOverlapSensor(const FSensorSpawnParameters SpawnParameters, FOverlapSensorParameters SensorParameters)

References FOverlapSensorParameters::AllChannels, ASensor::HideComponentForAllCameras(), NiagaraComponent, OverlapSensor, FOverlapSensorParameters::OwningActor, FSensorSpawnParameters::Parent, FOverlapSensorParameters::RelativePosition, FSensorSpawnParameters::SensorIdentifier, FSensorSpawnParameters::SensorName, SetNiagaraComponentVisibility(), FSensorSpawnParameters::SimulateSensor, FOverlapSensorParameters::Size, USensorFactory::SpawnOverlapSensor(), and FSensorSpawnParameters::Transform.

◆ EndPlay()

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

Definition at line 55 of file Spectator.cpp.

56{
57 Super::EndPlay(EndPlayReason);
58
60 {
61 NiagaraComponent->UnregisterComponent();
62 NiagaraComponent->DestroyComponent();
63 NiagaraComponent = nullptr;
64 }
65
66 if (OverlapSensor)
67 {
68 OverlapSensor->Destroy();
69 OverlapSensor = nullptr;
70 }
71
72 Target.Reset();
73}
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 114 of file Spectator.cpp.

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

244{
245 return Cast<UFloatingPawnMovement>(GetMovementComponent());
246}

Referenced by GetMaxSpeed(), and SetMaxSpeed().

◆ GetMaxSpeed()

float ASpectator::GetMaxSpeed ( )

Get FloatingPawnMovement current max speed.

Definition at line 231 of file Spectator.cpp.

232{
233 UFloatingPawnMovement* FloatingMovement = GetFloatingPawnMovement();
234 if (FloatingMovement)
235 {
236 return FloatingMovement->MaxSpeed;
237 }
238
239 // Else return default value of 1500.0f (set in BP_spectator blueprint)
240 return 1500.0f;
241}
UFloatingPawnMovement * GetFloatingPawnMovement()
Definition: Spectator.cpp:243

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

223{
224 UFloatingPawnMovement* FloatingMovement = GetFloatingPawnMovement();
225 if (FloatingMovement)
226 {
227 FloatingMovement->MaxSpeed = MaxSpeed;
228 }
229}

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

215{
217 {
218 NiagaraComponent->SetVisibility(Visible);
219 }
220}

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

195{
196 // Detach from any previous target
197 DetachFromActor(FDetachmentTransformRules::KeepRelativeTransform);
198
199 Target.Reset();
200
201 if (EnableInputs)
202 {
203 APlayerController* PlayerController = Cast<APlayerController>(GetController());
204
205 if (PlayerController)
206 {
207 EnableInput(PlayerController);
208 }
209 }
210
211 OnFollowTargetChanged.Broadcast(false, Target.Get());
212}

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

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

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

183{
184 if (!IsValid(Actor))
185 {
186 return;
187 }
188
189 FVector TargetLocation = Actor->GetActorLocation();
190
191 TeleportToFollowLocation(TargetLocation, DistanceToActor, HeightOffset);
192}
void TeleportToFollowLocation(FVector TargetLocation, const float DistanceToLocation, const float HeightOffset)
Definition: Spectator.cpp:160

References TeleportToFollowLocation().

◆ TeleportToActorLocation()

void ASpectator::TeleportToActorLocation ( const AActor *  Actor)

Teleports the spectator to the location of a specific actor.

Definition at line 105 of file Spectator.cpp.

106{
107 if (Actor)
108 {
109 FTransform Transform = Actor->GetActorTransform();
111 }
112}
void TeleportSpectator(const FTransform &Transform)
Definition: Spectator.cpp:75

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

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

Referenced by TeleportToActorFollowLocation().

◆ TeleportToLevelTeleportLocation()

void ASpectator::TeleportToLevelTeleportLocation ( FLevelTeleportLocation  TeleportLocation)

Teleport specator to level teleport location

Parameters
TeleportLocation

Definition at line 249 of file Spectator.cpp.

250{
251
252 if (!IsPlayerControlled())
253 {
254 return;
255 }
256
257 FTransform Transform = TeleportLocation.TeleportLocationTransform;
258
259 // Detach from any Actor and enable inputs.
261
262 SetActorLocationAndRotation(Transform.GetLocation(), Transform.GetRotation().Rotator());
263
264 // Set the player controller rotation to match the incoming Transform angle,
265 // This actually sets the camera angle.
266 APlayerController* PlayerController = Cast<APlayerController>(GetController());
267 if (PlayerController)
268 {
269 PlayerController->SetControlRotation(Transform.Rotator());
270 }
271}

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: