Set up agent components

This commit is contained in:
nsubiron 2018-02-24 13:52:08 +01:00
parent 8364f5db26
commit a26d65eee4
12 changed files with 76 additions and 37 deletions

View File

@ -8,7 +8,7 @@
#include "Agent/AgentComponentVisitor.h" #include "Agent/AgentComponentVisitor.h"
#include "Components/ActorComponent.h" #include "Components/SceneComponent.h"
#include "Templates/SharedPointer.h" #include "Templates/SharedPointer.h"
#include "AgentComponent.generated.h" #include "AgentComponent.generated.h"
@ -18,7 +18,7 @@ class FAgentMap;
/// Actors with an UAgentComponent are registered as agents in the scene and /// Actors with an UAgentComponent are registered as agents in the scene and
/// their status is sent to the client each frame (if requested by the client). /// their status is sent to the client each frame (if requested by the client).
UCLASS(Abstract) UCLASS(Abstract)
class CARLA_API UAgentComponent : public UActorComponent class CARLA_API UAgentComponent : public USceneComponent
{ {
GENERATED_BODY() GENERATED_BODY()

View File

@ -9,11 +9,15 @@
#include "Carla.h" #include "Carla.h"
#include "TrafficSignAgentComponent.h" #include "TrafficSignAgentComponent.h"
#include "Traffic/TrafficSignBase.h"
UTrafficSignAgentComponent::UTrafficSignAgentComponent(const FObjectInitializer &ObjectInitializer) UTrafficSignAgentComponent::UTrafficSignAgentComponent(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer) {} : Super(ObjectInitializer) {}
void UTrafficSignAgentComponent::OnComponentCreated() void UTrafficSignAgentComponent::BeginPlay()
{ {
Super::OnComponentCreated(); TrafficSign = Cast<ATrafficSignBase>(GetOwner());
checkf(TrafficSign != nullptr, TEXT("UTrafficSignAgentComponent can only be attached to ATrafficSignBase"));
Super::BeginPlay();
} }

View File

@ -10,8 +10,8 @@
#include "TrafficSignAgentComponent.generated.h" #include "TrafficSignAgentComponent.generated.h"
/// This component can be added to any AWheeledVehicle to be added as agent. class ATrafficSignBase;
/// See UAgentComponent.
UCLASS() UCLASS()
class CARLA_API UTrafficSignAgentComponent : public UAgentComponent class CARLA_API UTrafficSignAgentComponent : public UAgentComponent
{ {
@ -21,9 +21,15 @@ public:
UTrafficSignAgentComponent(const FObjectInitializer &ObjectInitializer); UTrafficSignAgentComponent(const FObjectInitializer &ObjectInitializer);
const ATrafficSignBase &GetTrafficSign() const
{
check(TrafficSign != nullptr);
return *TrafficSign;
}
protected: protected:
virtual void OnComponentCreated() override; virtual void BeginPlay() override;
virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final
{ {
@ -32,5 +38,6 @@ protected:
private: private:
UPROPERTY()
ATrafficSignBase *TrafficSign = nullptr;
}; };

View File

@ -9,15 +9,15 @@
#include "Carla.h" #include "Carla.h"
#include "VehicleAgentComponent.h" #include "VehicleAgentComponent.h"
#include "WheeledVehicle.h" #include "Vehicle/CarlaWheeledVehicle.h"
UVehicleAgentComponent::UVehicleAgentComponent(const FObjectInitializer &ObjectInitializer) UVehicleAgentComponent::UVehicleAgentComponent(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer) {} : Super(ObjectInitializer) {}
void UVehicleAgentComponent::OnComponentCreated() void UVehicleAgentComponent::BeginPlay()
{ {
Super::OnComponentCreated(); WheeledVehicle = Cast<ACarlaWheeledVehicle>(GetOwner());
checkf(WheeledVehicle != nullptr, TEXT("UVehicleAgentComponent can only be attached to ACarlaWheeledVehicle"));
WheeledVehicle = Cast<AWheeledVehicle>(GetOwner()); Super::BeginPlay();
checkf(WheeledVehicle != nullptr, TEXT("UVehicleAgentComponent can only be attached to AWheeledVehicle"));
} }

View File

@ -10,7 +10,7 @@
#include "VehicleAgentComponent.generated.h" #include "VehicleAgentComponent.generated.h"
class AWheeledVehicle; class ACarlaWheeledVehicle;
UCLASS() UCLASS()
class CARLA_API UVehicleAgentComponent : public UAgentComponent class CARLA_API UVehicleAgentComponent : public UAgentComponent
@ -21,9 +21,15 @@ public:
UVehicleAgentComponent(const FObjectInitializer &ObjectInitializer); UVehicleAgentComponent(const FObjectInitializer &ObjectInitializer);
ACarlaWheeledVehicle &GetVehicle() const
{
check(WheeledVehicle != nullptr);
return *WheeledVehicle;
}
protected: protected:
virtual void OnComponentCreated() override; virtual void BeginPlay() override;
virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final
{ {
@ -32,5 +38,5 @@ protected:
private: private:
AWheeledVehicle *WheeledVehicle = nullptr; ACarlaWheeledVehicle *WheeledVehicle = nullptr;
}; };

View File

@ -9,11 +9,21 @@
#include "Carla.h" #include "Carla.h"
#include "WalkerAgentComponent.h" #include "WalkerAgentComponent.h"
#include "GameFramework/Character.h"
UWalkerAgentComponent::UWalkerAgentComponent(const FObjectInitializer &ObjectInitializer) UWalkerAgentComponent::UWalkerAgentComponent(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer) {} : Super(ObjectInitializer) {}
void UWalkerAgentComponent::OnComponentCreated() float UWalkerAgentComponent::GetForwardSpeed() const
{ {
Super::OnComponentCreated(); /// @todo Is it necessary to compute this speed every tick?
return FVector::DotProduct(Walker->GetVelocity(), Walker->GetActorRotation().Vector()) * 0.036f;
}
void UWalkerAgentComponent::BeginPlay()
{
Walker = Cast<ACharacter>(GetOwner());
checkf(Walker != nullptr, TEXT("UWalkerAgentComponent can only be attached to ACharacter"));
Super::BeginPlay();
} }

View File

@ -10,6 +10,8 @@
#include "WalkerAgentComponent.generated.h" #include "WalkerAgentComponent.generated.h"
class ACharacter;
/// This component can be added to any ACharacter to be added as agent. /// This component can be added to any ACharacter to be added as agent.
/// See UAgentComponent. /// See UAgentComponent.
UCLASS() UCLASS()
@ -21,9 +23,18 @@ public:
UWalkerAgentComponent(const FObjectInitializer &ObjectInitializer); UWalkerAgentComponent(const FObjectInitializer &ObjectInitializer);
/// Return forward speed in km/h.
float GetForwardSpeed() const;
FVector GetBoundingBoxExtent() const
{
/// @todo Perhaps the box it is not the same for every walker...
return {45.0f, 35.0f, 100.0f};
}
protected: protected:
virtual void OnComponentCreated() override; virtual void BeginPlay() override;
virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final
{ {
@ -32,5 +43,6 @@ protected:
private: private:
UPROPERTY()
ACharacter *Walker = nullptr;
}; };

View File

@ -35,7 +35,8 @@ static ETrafficSignState ToTrafficSignState(ETrafficLightState State) {
// -- ATrafficLightBase -------------------------------------------------------- // -- ATrafficLightBase --------------------------------------------------------
// ============================================================================= // =============================================================================
ATrafficLightBase::ATrafficLightBase() : Super() ATrafficLightBase::ATrafficLightBase(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer)
{ {
PrimaryActorTick.bCanEverTick = false; PrimaryActorTick.bCanEverTick = false;
} }

View File

@ -22,7 +22,7 @@ class CARLA_API ATrafficLightBase : public ATrafficSignBase {
public: public:
ATrafficLightBase(); ATrafficLightBase(const FObjectInitializer &ObjectInitializer);
protected: protected:

View File

@ -7,16 +7,13 @@
#include "Carla.h" #include "Carla.h"
#include "TrafficSignBase.h" #include "TrafficSignBase.h"
#include "Game/CarlaGameState.h" ATrafficSignBase::ATrafficSignBase(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer) {
RootComponent =
ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneRootComponent"));
RootComponent->SetMobility(EComponentMobility::Static);
ATrafficSignBase::ATrafficSignBase() : Super() {} TrafficSignAgentComponent =
CreateDefaultSubobject<UTrafficSignAgentComponent>(TEXT("TrafficSignAgentComponent"));
void ATrafficSignBase::BeginPlay() TrafficSignAgentComponent->SetupAttachment(RootComponent);
{
auto *GameState = GetWorld()->GetGameState<ACarlaGameState>();
if (GameState != nullptr) {
GameState->RegisterTrafficSign(this);
} else {
UE_LOG(LogCarla, Error, TEXT("Missing CARLA game state!"));
}
} }

View File

@ -32,9 +32,7 @@ class CARLA_API ATrafficSignBase : public AActor {
public: public:
ATrafficSignBase(); ATrafficSignBase(const FObjectInitializer &ObjectInitializer);
virtual void BeginPlay() override;
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
ETrafficSignState GetTrafficSignState() const ETrafficSignState GetTrafficSignState() const
@ -52,4 +50,7 @@ private:
UPROPERTY(Category = "Traffic Sign", EditAnywhere) UPROPERTY(Category = "Traffic Sign", EditAnywhere)
ETrafficSignState TrafficSignState = ETrafficSignState::UNKNOWN; ETrafficSignState TrafficSignState = ETrafficSignState::UNKNOWN;
UPROPERTY(Category = "Traffic Sign", VisibleAnywhere)
UTrafficSignAgentComponent *TrafficSignAgentComponent;
}; };

View File

@ -26,6 +26,7 @@ ACarlaWheeledVehicle::ACarlaWheeledVehicle(const FObjectInitializer& ObjectIniti
VehicleBounds->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName); VehicleBounds->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
VehicleAgentComponent = CreateDefaultSubobject<UVehicleAgentComponent>(TEXT("VehicleAgentComponent")); VehicleAgentComponent = CreateDefaultSubobject<UVehicleAgentComponent>(TEXT("VehicleAgentComponent"));
VehicleAgentComponent->SetupAttachment(RootComponent);
GetVehicleMovementComponent()->bReverseAsBrake = false; GetVehicleMovementComponent()->bReverseAsBrake = false;
} }