9#include "Kismet/GameplayStatics.h"
22UTrafficLightComponent::UTrafficLightComponent()
29 const double epsilon = 0.00001;
31 auto References = GetAllReferencesToThisSignal(Map);
33 for (
auto& Reference : References)
35 auto RoadId = Reference.first;
36 const auto* SignalReference = Reference.second;
37 for(
auto &validity : SignalReference->GetValidities())
45 std::optional<carla::road::element::Waypoint> opt_signal_waypoint = Map.
GetWaypoint(
46 RoadId, lane, SignalReference->GetS());
47 if(opt_signal_waypoint){
48 signal_waypoint = opt_signal_waypoint.value();
55 if (predecessors.size() == 1) {
56 auto predecessor = predecessors.front();
58 signal_waypoint = predecessor;
66 float BoxWidth =
static_cast<float>(0.5f*Map.
GetLaneWidth(signal_waypoint)*0.5);
67 float BoxLength = 1.5f;
68 float BoxHeight = 1.0f;
70 BoxWidth = std::max(0.01f, BoxWidth);
73 float AdditionalDistance = 1.5f;
76 signal_waypoint.
s = FMath::Clamp(signal_waypoint.
s - (BoxLength + AdditionalDistance),
77 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
81 signal_waypoint.
s = FMath::Clamp(signal_waypoint.
s + (BoxLength + AdditionalDistance),
82 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
90 GenerateTrafficLightBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight));
96void UTrafficLightComponent::GenerateTrafficLightBox(
const FTransform BoxTransform,
97 const FVector BoxSize)
99 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
100 BoxComponent->OnComponentBeginOverlap.AddDynamic(
this, &UTrafficLightComponent::OnBeginOverlapTriggerBox);
101 BoxComponent->OnComponentEndOverlap.AddDynamic(
this, &UTrafficLightComponent::OnEndOverlapTriggerBox);
102 AddEffectTriggerVolume(BoxComponent);
107 LightState = NewState;
108 LightChangeDispatcher.Broadcast();
109 if (GetOwner() ==
nullptr)
113 if (GetOwner()->Implements<UTrafficLightInterface>())
115 ITrafficLightInterface::Execute_LightChanged(GetOwner(), LightState);
123 for (
auto Controller : Vehicles)
125 if (Controller !=
nullptr)
127 Controller->SetTrafficLightState(LightState);
132void UTrafficLightComponent::OnComponentDestroyed(
bool bDestroyingHierarchy)
134 if (TrafficLightController !=
nullptr)
136 TrafficLightController->RemoveTrafficLight(
this);
137 TrafficLightController =
nullptr;
139 Super::OnComponentDestroyed(bDestroyingHierarchy);
147void UTrafficLightComponent::SetFrozenGroup(
bool InFreeze)
151 GetGroup()->SetFrozenGroup(InFreeze);
158 TrafficLightController !=
nullptr ?
159 TrafficLightController->GetGroup() :
166 TrafficLightController !=
nullptr ?
167 TrafficLightController->GetGroup() :
173 return TrafficLightController;
178 return TrafficLightController;
181void UTrafficLightComponent::OnBeginOverlapTriggerBox(UPrimitiveComponent *OverlappedComp,
183 UPrimitiveComponent *OtherComp,
184 int32 OtherBodyIndex,
186 const FHitResult &SweepResult)
192 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
193 if (VehicleController)
196 Vehicles.Add(VehicleController);
197 VehicleController->
SetTrafficLight(Cast<ATrafficLightBase>(GetOwner()));
202void UTrafficLightComponent::OnEndOverlapTriggerBox(UPrimitiveComponent *OverlappedComp,
204 UPrimitiveComponent *OtherComp,
205 int32 OtherBodyIndex)
211 Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
212 if (VehicleController)
216 Vehicles.Remove(VehicleController);
223 TrafficLightController = Controller;
Base class for CARLA wheeled vehicles.
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
void LightChangedCompatibility(ETrafficLightState NewLightState)
Class which implements the state changing of traffic lights.
Wheeled vehicle controller with optional AI.
void SetTrafficLightState(ETrafficLightState InTrafficLightState)
Set traffic light state currently affecting this vehicle.
void SetTrafficLight(ATrafficLightBase *InTrafficLight)
Set traffic light currently affecting this vehicle.
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
Maps a controller from OpenDrive.
static std::vector< int > GenerateRange(int a, int b)
double GetDistance() const
bool IsJunction(RoadId road_id) const
double GetLaneWidth(Waypoint waypoint) const
std::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
geom::Transform ComputeTransform(Waypoint waypoint) const
std::vector< Waypoint > GetPredecessors(Waypoint waypoint) const
const Lane & GetLane(Waypoint waypoint) const
========================================================================