Agrarsense
SensorManager.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 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
6#include "SensorManager.h"
7#include "Sensor.h"
8
9#include "Engine/World.h"
10#include "Engine/GameViewportClient.h"
11#include "Engine/GameInstance.h"
12
14
15void USensorManager::Initialize(FSubsystemCollectionBase& Collection)
16{
17 Super::Initialize(Collection);
18
19#if WITH_EDITOR
20 UE_LOG(LogTemp, Warning, TEXT("SensorManager Initilized."));
21#endif
22}
23
25{
26 Super::Deinitialize();
27
28 SensorManagerPtr = nullptr;
29 Sensors.Empty();
30
31#if WITH_EDITOR
32 UE_LOG(LogTemp, Warning, TEXT("SensorManager Destroyed."));
33#endif
34}
35
37{
38 if (SensorManagerPtr && IsValid(SensorManagerPtr))
39 {
40 return SensorManagerPtr;
41 }
42
43 if (GEngine && GEngine->GameViewport)
44 {
45 if (UWorld* World = GEngine->GameViewport->GetWorld())
46 {
47 if (UGameInstance* GameInstance = World->GetGameInstance())
48 {
49 SensorManagerPtr = GameInstance->GetSubsystem<USensorManager>();
50 return SensorManagerPtr;
51 }
52 }
53 }
54
55 return nullptr;
56}
57
59{
60 USensorManager* Manager = Get();
61 if (Manager && SensorPtr)
62 {
63 Manager->Sensors.Add(SensorPtr);
64 Manager->OnSensorSpawned.Broadcast(SensorPtr);
65 }
66}
67
69{
70 USensorManager* Manager = Get();
71 if (Manager && SensorPtr)
72 {
73 Manager->Sensors.Remove(SensorPtr);
74 Manager->OnSensorDestroyed.Broadcast(SensorPtr);
75 }
76}
77
78TArray<ASensor*> USensorManager::GetAllSensors() const
79{
80 return Sensors;
81}
82
83TArray<ASensor*> USensorManager::GetAllSensorsOfType(ESensorTypes SensorType) const
84{
85 TArray<ASensor*> SensorsOfType;
86
87 for (ASensor* Sensor : Sensors)
88 {
89 if (Sensor && Sensor->GetSensorType() == SensorType)
90 {
91 SensorsOfType.Add(Sensor);
92 }
93 }
94
95 return SensorsOfType;
96}
ESensorTypes
Definition: SensorTypes.h:15
Definition: Sensor.h:44
static USensorManager * Get()
FSensorDestroyedDelegate OnSensorDestroyed
Definition: SensorManager.h:68
static void AddSensor(ASensor *SensorPtr)
TArray< ASensor * > Sensors
Definition: SensorManager.h:81
TArray< ASensor * > GetAllSensorsOfType(ESensorTypes SensorType) const
static USensorManager * SensorManagerPtr
Definition: SensorManager.h:78
TArray< ASensor * > GetAllSensors() const
FSensorSpawnedDelegate OnSensorSpawned
Definition: SensorManager.h:61
static void RemoveSensor(ASensor *SensorPtr)
virtual void Initialize(FSubsystemCollectionBase &Collection) override
virtual void Deinitialize() override