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 "Kismet/GameplayStatics.h"
16#include "NiagaraFunctionLibrary.h"
17#include "NiagaraComponent.h"
18#include "NiagaraSystem.h"
19
21{
22 PrimaryActorTick.bCanEverTick = false;
23 SensorsManager = CreateDefaultSubobject<USensorsManagerComponent>(TEXT("SensorsManagerComp"));
24 InteractableName = NSLOCTEXT("Agrarsense", "VehicleInteractableName", "Vehicle");
25}
26
28{
29 Super::BeginPlay();
30
31 TogglePhysics(true);
32
33 // Spawn Transform and Collision sensors for each vehicle
34 FTransform transform = GetTransform();
35
36 // Create Collision sensor
37 FString VehicleCollisionSensorID = ActorID + "/collision";
38 CollisionSensor = USensorFactory::SpawnCollisionSensor(transform, this, VehicleCollisionSensorID, "collision", true);
39
40 // Create Transform sensor
41 FString VehicleTransformSensorID = ActorID + "/transform";
42 TransformSensor = USensorFactory::SpawnTransformSensor(transform, this, VehicleTransformSensorID, "transform", true);
43
44 float NiagaraComponentHeight = 500.0f;
45 float OverlapBoundsSize = 5000.0f;
46
48 {
49 // Adjust certain variables if vehicle is a drone
50 OverlapBoundsSize = 2500.0f;
51 NiagaraComponentHeight = 200.0f;
52 }
53
54 FVector RelativePosition = FVector(0.0f, 0.0f, 0.0f);
56 {
57 // TODO make better solution for this.
58 RelativePosition = FVector(150.0f, -120.0f, 250.0f);
59 }
60
61 // Create OverlapSensor
63 Params.OwningActor = this;
64 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 // Destroy all remaining Actors that are attached to this vehicle, if any
122 TArray<AActor*> AttachedActors;
123 GetAttachedActors(AttachedActors);
124 for (AActor* Actor : AttachedActors)
125 {
126 if (Actor)
127 {
128 Actor->Destroy();
129 }
130 }
131}
132
133void AVehicle::TeleportVehicleTo_Implementation(FVector NewLocation, FRotator NewRotation)
134{
135 SetActorLocationAndRotation(NewLocation, NewRotation, false, nullptr, ETeleportType::TeleportPhysics);
136}
137
139{
140 if (OverlapSensor)
141 {
143 }
144}
145
146FString AVehicle::ExportToJsonFile(const FString& FileName)
147{
148 bool OverrideTransform = IsVehicleInGarage();
149
150 FTransform TransformToUse = GetActorTransform();
151
152 if (OverrideTransform)
153 {
154 TransformToUse = MovedFromGarageTransform;
155 }
156
157 return USimulatorJsonExporter::ExportVehicleAndSensorsToJSON(FileName, this, OverrideTransform, TransformToUse);
158}
void SetVisualizeOverlapArea(bool Visualize)
ACollisionSensor * CollisionSensor
Definition: Vehicle.h:281
FTransform MovedFromGarageTransform
Definition: Vehicle.h:301
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: Vehicle.cpp:86
FText InteractableName
Definition: Vehicle.h:257
virtual void BeginPlay() override
Definition: Vehicle.cpp:27
virtual void PossessedBy(AController *NewController) override
Definition: Vehicle.cpp:79
void ResetCamera()
virtual void TeleportVehicleTo_Implementation(FVector NewLocation, FRotator NewRotation)
Definition: Vehicle.cpp:133
bool IsVehicleInGarage() const
Definition: Vehicle.h:123
FString ActorID
Definition: Vehicle.h:296
FString ExportToJsonFile(const FString &FileName)
Definition: Vehicle.cpp:146
AVehicle()
Definition: Vehicle.cpp:20
void SetVisualizeVehicleOverlapArea(bool Visible)
Definition: Vehicle.cpp:138
AOverlapSensor * OverlapSensor
Definition: Vehicle.h:287
virtual EVehicleTypes GetVehicleType() const
Definition: Vehicle.h:51
void TogglePhysics(bool isOn)
ATransformSensor * TransformSensor
Definition: Vehicle.h:284
UNiagaraComponent * NiagaraComponent
Definition: Vehicle.h:290
TObjectPtr< USensorsManagerComponent > SensorsManager
Definition: Vehicle.h:278
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())