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);
66 Params.RelativePosition = RelativePosition;
67 FString OverlapSensorID = ActorID + "/overlap";
68 OverlapSensor = USensorFactory::SpawnOverlapSensor(transform, Params, OverlapSensorID, "overlap");
69
70 // Load and setup rain/snow fall niagara system
71 UNiagaraSystem* NiagaraSystem = LoadObject<UNiagaraSystem>(nullptr, TEXT("/Game/Agrarsense/Particles/WaterAndSnow/NS_Particles.NS_Particles"));
72 if (NiagaraSystem)
73 {
74 NiagaraComponent = UNiagaraFunctionLibrary::SpawnSystemAttached(NiagaraSystem, this->GetRootComponent(),
75 FName("NiagaraEmitterSocketName"),
76 FVector(0.0, 0.0, NiagaraComponentHeight), FRotator::ZeroRotator, EAttachLocation::KeepRelativeOffset, true);
77 }
78}
79
80void AVehicle::PossessedBy(AController* NewController)
81{
82 Super::PossessedBy(NewController);
83
85}
86
87void AVehicle::EndPlay(const EEndPlayReason::Type EndPlayReason)
88{
89 Super::EndPlay(EndPlayReason);
90
92 {
93 TransformSensor->Destroy();
94 TransformSensor = nullptr;
95 }
96
98 {
99 CollisionSensor->Destroy();
100 CollisionSensor = nullptr;
101 }
102
103 if (OverlapSensor)
104 {
105 OverlapSensor->Destroy();
106 OverlapSensor = nullptr;
107 }
108
109 // Destroy attached sensors when vehicle is destroyed
110 if (IsValid(SensorsManager) && EndPlayReason == EEndPlayReason::Destroyed)
111 {
112 SensorsManager->DestroyAllSensors();
113 }
114
116 {
117 NiagaraComponent->UnregisterComponent();
118 NiagaraComponent->DestroyComponent();
119 NiagaraComponent = nullptr;
120 }
121
122 // Destroy all remaining Actors that are attached to this vehicle, if any
123 TArray<AActor*> AttachedActors;
124 GetAttachedActors(AttachedActors);
125 for (AActor* Actor : AttachedActors)
126 {
127 if (Actor)
128 {
129 Actor->Destroy();
130 }
131 }
132}
133
134void AVehicle::TeleportVehicleTo_Implementation(FVector NewLocation, FRotator NewRotation)
135{
136 SetActorLocationAndRotation(NewLocation, NewRotation, false, nullptr, ETeleportType::TeleportPhysics);
137}
138
140{
141 if (OverlapSensor)
142 {
144 }
145}
146
147FString AVehicle::ExportToJsonFile(const FString& FileName)
148{
149 bool OverrideTransform = IsVehicleInGarage();
150
151 FTransform TransformToUse = GetActorTransform();
152
153 if (OverrideTransform)
154 {
155 TransformToUse = MovedFromGarageTransform;
156 }
157
158 return USimulatorJsonExporter::ExportVehicleAndSensorsToJSON(FileName, this, OverrideTransform, TransformToUse);
159}
void SetVisualizeOverlapArea(bool Visualize)
ACollisionSensor * CollisionSensor
Definition: Vehicle.h:260
FTransform MovedFromGarageTransform
Definition: Vehicle.h:280
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: Vehicle.cpp:87
FText InteractableName
Definition: Vehicle.h:241
virtual void BeginPlay() override
Definition: Vehicle.cpp:27
virtual void PossessedBy(AController *NewController) override
Definition: Vehicle.cpp:80
void ResetCamera()
virtual void TeleportVehicleTo_Implementation(FVector NewLocation, FRotator NewRotation)
Definition: Vehicle.cpp:134
bool IsVehicleInGarage() const
Definition: Vehicle.h:131
FString ActorID
Definition: Vehicle.h:275
FString ExportToJsonFile(const FString &FileName)
Definition: Vehicle.cpp:147
AVehicle()
Definition: Vehicle.cpp:20
void SetVisualizeVehicleOverlapArea(bool Visible)
Definition: Vehicle.cpp:139
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())