Agrarsense
Vehicle.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
7
14
15#include "NiagaraFunctionLibrary.h"
16#include "NiagaraComponent.h"
17#include "NiagaraSystem.h"
18
20{
21 PrimaryActorTick.bCanEverTick = false;
22 SensorsManager = CreateDefaultSubobject<USensorsManagerComponent>(TEXT("SensorsManagerComp"));
23 InteractableName = NSLOCTEXT("Agrarsense", "VehicleInteractableName", "Vehicle");
24}
25
27{
28 Super::BeginPlay();
29
30 TogglePhysics(true);
31
32 // Spawn Transform and Collision sensors for each vehicle
33 FTransform transform = GetTransform();
34
35 // Create Collision sensor
36 FString VehicleCollisionSensorID = ActorID + "/collision";
37 CollisionSensor = USensorFactory::SpawnCollisionSensor(transform, this, VehicleCollisionSensorID, "collision", true);
38
39 // Create Transform sensor
40 FString VehicleTransformSensorID = ActorID + "/transform";
41 TransformSensor = USensorFactory::SpawnTransformSensor(transform, this, VehicleTransformSensorID, "transform", true);
42
43 float NiagaraComponentHeight = 500.0f;
44 float OverlapBoundsSize = 5000.0f;
45
47 {
48 // Adjust certain variables if vehicle is a drone
49 OverlapBoundsSize = 2500.0f;
50 NiagaraComponentHeight = 200.0f;
51 }
52
53 FVector RelativePosition = FVector(0.0f, 0.0f, 0.0f);
55 {
56 // TODO make better solution for this.
57 RelativePosition = FVector(150.0f, -120.0f, 250.0f);
58 }
59
60 // Create OverlapSensor
62 Params.OwningActor = this;
63 Params.Size = FVector(OverlapBoundsSize, OverlapBoundsSize, OverlapBoundsSize);
65 Params.RelativePosition = RelativePosition;
66 FString OverlapSensorID = ActorID + "/overlap";
67 OverlapSensor = USensorFactory::SpawnOverlapSensor(transform, Params, OverlapSensorID, "overlap");
68
69 // Load and setup rain/snow fall niagara system
70 UNiagaraSystem* NiagaraSystem = LoadObject<UNiagaraSystem>(nullptr, TEXT("/Game/Agrarsense/Particles/WaterAndSnow/NS_Particles.NS_Particles"));
71 if (NiagaraSystem)
72 {
73 NiagaraComponent = UNiagaraFunctionLibrary::SpawnSystemAttached(NiagaraSystem, this->GetRootComponent(),
74 FName("NiagaraEmitterSocketName"),
75 FVector(0.0, 0.0, NiagaraComponentHeight), FRotator::ZeroRotator, EAttachLocation::KeepRelativeOffset, true);
76 }
77}
78
79void AVehicle::PossessedBy(AController* NewController)
80{
81 Super::PossessedBy(NewController);
82
84}
85
86void AVehicle::EndPlay(const EEndPlayReason::Type EndPlayReason)
87{
88 Super::EndPlay(EndPlayReason);
89
91 {
92 TransformSensor->Destroy();
93 TransformSensor = nullptr;
94 }
95
97 {
98 CollisionSensor->Destroy();
99 CollisionSensor = nullptr;
100 }
101
102 if (OverlapSensor)
103 {
104 OverlapSensor->Destroy();
105 OverlapSensor = nullptr;
106 }
107
108 // Destroy attached sensors when vehicle is destroyed
109 if (IsValid(SensorsManager) && EndPlayReason == EEndPlayReason::Destroyed)
110 {
111 SensorsManager->DestroyAllSensors();
112 }
113
115 {
116 NiagaraComponent->UnregisterComponent();
117 NiagaraComponent->DestroyComponent();
118 NiagaraComponent = nullptr;
119 }
120}
121
122void AVehicle::TeleportVehicleTo_Implementation(FVector NewLocation, FRotator NewRotation)
123{
124 SetActorLocationAndRotation(NewLocation, NewRotation, false, nullptr, ETeleportType::TeleportPhysics);
125}
126
128{
129 if (OverlapSensor)
130 {
132 }
133}
134
135FString AVehicle::ExportToJsonFile(const FString& FileName)
136{
137 bool OverrideTransform = IsVehicleInGarage();
138
139 FTransform TransformToUse = GetActorTransform();
140
141 if (OverrideTransform)
142 {
143 TransformToUse = MovedFromGarageTransform;
144 }
145
146 return USimulatorJsonExporter::ExportVehicleAndSensorsToJSON(FileName, this, OverrideTransform, TransformToUse);
147}
void SetVisualizeOverlapArea(bool Visualize)
ACollisionSensor * CollisionSensor
Definition: Vehicle.h:260
FTransform MovedFromGarageTransform
Definition: Vehicle.h:284
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: Vehicle.cpp:86
FText InteractableName
Definition: Vehicle.h:241
virtual void BeginPlay() override
Definition: Vehicle.cpp:26
virtual void PossessedBy(AController *NewController) override
Definition: Vehicle.cpp:79
void ResetCamera()
virtual void TeleportVehicleTo_Implementation(FVector NewLocation, FRotator NewRotation)
Definition: Vehicle.cpp:122
bool IsVehicleInGarage() const
Definition: Vehicle.h:131
FString ActorID
Definition: Vehicle.h:275
FString ExportToJsonFile(const FString &FileName)
Definition: Vehicle.cpp:135
AVehicle()
Definition: Vehicle.cpp:19
void SetVisualizeVehicleOverlapArea(bool Visible)
Definition: Vehicle.cpp:127
AOverlapSensor * OverlapSensor
Definition: Vehicle.h:266
virtual EVehicleTypes GetVehicleType() const
Definition: Vehicle.h:50
void TogglePhysics(bool isOn)
ATransformSensor * TransformSensor
Definition: Vehicle.h:263
UNiagaraComponent * NiagaraComponent
Definition: Vehicle.h:269
TObjectPtr< USensorsManagerComponent > SensorsManager
Definition: Vehicle.h:257
static ATransformSensor * SpawnTransformSensor(const FTransform &transform, AActor *Owner, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor=true)
static ACollisionSensor * SpawnCollisionSensor(const FTransform &transform, AActor *Owner, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor=true)
static AOverlapSensor * SpawnOverlapSensor(const FTransform &transform, FOverlapSensorParameters Parameters, const FString sensorIdentifier, const FString sensorName)
static FString ExportVehicleAndSensorsToJSON(FString FileName, AVehicle *Vehicle, bool OverrideTransform=false, const FTransform &Transform=FTransform())