CARLA
CarlaLightSubsystem.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #include "CarlaLightSubsystem.h"
9 #include "Kismet/GameplayStatics.h"
10 
11 //using cr = carla::rpc;
12 
13 void UCarlaLightSubsystem::Initialize(FSubsystemCollectionBase& Collection)
14 {
15  // TODO: Subscribe to map change
16 }
17 
19 {
20 
21 }
22 
23 void UCarlaLightSubsystem::RegisterLight(UCarlaLight* CarlaLight)
24 {
25  if(CarlaLight)
26  {
27  auto LightId = CarlaLight->GetId();
28  if (Lights.Contains(LightId))
29  {
30  UE_LOG(LogCarla, Warning, TEXT("Light Id overlapping"));
31  return;
32  }
33  Lights.Add(LightId, CarlaLight);
34  }
36 }
37 
38 void UCarlaLightSubsystem::UnregisterLight(UCarlaLight* CarlaLight)
39 {
40  if(CarlaLight)
41  {
42  Lights.Remove(CarlaLight->GetId());
43  }
45 }
46 
48 {
49  for (auto ClientPair : ClientStates)
50  {
51  if(ClientPair.Value)
52  {
53  return true;
54  }
55  }
56  return false;
57 }
58 
59 std::vector<carla::rpc::LightState> UCarlaLightSubsystem::GetLights(FString Client)
60 {
61  std::vector<carla::rpc::LightState> result;
62 
63  ClientStates.FindOrAdd(Client) = false;
64 
65  for(auto& Light : Lights)
66  {
67  UCarlaLight* CarlaLight = Light.Value;
68 
69  result.push_back(CarlaLight->GetLightState());
70  }
71  return result;
72 }
73 
75  FString Client,
76  std::vector<carla::rpc::LightState> LightsToSet,
77  bool DiscardClient)
78 {
79  bool* ClientState = ClientStates.Find(Client);
80 
81  if(ClientState) {
82  for(auto& LightState : LightsToSet) {
83  UCarlaLight* CarlaLight = Lights.FindRef(LightState._id);
84  if(CarlaLight) {
85  CarlaLight->SetLightState(LightState);
86  }
87  }
88  *ClientState = true;
89 
90  if(DiscardClient)
91  {
92  ClientStates.Remove(Client);
93  }
94  }
95 
96 }
97 
98 UCarlaLight* UCarlaLightSubsystem::GetLight(int Id)
99 {
100  if (Lights.Contains(Id))
101  {
102  return Lights[Id];
103  }
104  return nullptr;
105 }
106 
107 void UCarlaLightSubsystem::SetDayNightCycle(const bool active) {
108  TArray<AActor*> WeatherActors;
109  UGameplayStatics::GetAllActorsOfClass(GetWorld(), AWeather::StaticClass(), WeatherActors);
110  if (WeatherActors.Num())
111  {
112  if (AWeather* WeatherActor = Cast<AWeather>(WeatherActors[0]))
113  {
114  WeatherActor->SetDayNightCycle(active);
115  }
116  }
117 }
118 
119 void UCarlaLightSubsystem::SetClientStatesdirty(FString ClientThatUpdate)
120 {
121  for(auto& Client : ClientStates)
122  {
123  if(Client.Key != ClientThatUpdate)
124  {
125  Client.Value = true;
126  }
127 
128  }
129 }
UCarlaLight * GetLight(int Id)
TMap< int, UCarlaLight *> Lights
void UnregisterLight(UCarlaLight *CarlaLight)
void SetDayNightCycle(const bool active)
void Deinitialize() override
void Initialize(FSubsystemCollectionBase &Collection) override
void SetClientStatesdirty(FString ClientThatUpdate)
void SetLights(FString Client, std::vector< carla::rpc::LightState > LightsToSet, bool DiscardClient=false)
TMap< int, UCarlaLight *> & GetLights()
void RegisterLight(UCarlaLight *CarlaLight)
TMap< FString, bool > ClientStates