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 "Components/ActorComponent.h"
#include "Components/SceneComponent.h"
#include "Templates/SharedPointer.h"
#include "AgentComponent.generated.h"
@ -18,7 +18,7 @@ class FAgentMap;
/// 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).
UCLASS(Abstract)
class CARLA_API UAgentComponent : public UActorComponent
class CARLA_API UAgentComponent : public USceneComponent
{
GENERATED_BODY()

View File

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

View File

@ -9,15 +9,15 @@
#include "Carla.h"
#include "VehicleAgentComponent.h"
#include "WheeledVehicle.h"
#include "Vehicle/CarlaWheeledVehicle.h"
UVehicleAgentComponent::UVehicleAgentComponent(const FObjectInitializer &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());
checkf(WheeledVehicle != nullptr, TEXT("UVehicleAgentComponent can only be attached to AWheeledVehicle"));
Super::BeginPlay();
}

View File

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

View File

@ -9,11 +9,21 @@
#include "Carla.h"
#include "WalkerAgentComponent.h"
#include "GameFramework/Character.h"
UWalkerAgentComponent::UWalkerAgentComponent(const FObjectInitializer &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"
class ACharacter;
/// This component can be added to any ACharacter to be added as agent.
/// See UAgentComponent.
UCLASS()
@ -21,9 +23,18 @@ public:
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:
virtual void OnComponentCreated() override;
virtual void BeginPlay() override;
virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final
{
@ -32,5 +43,6 @@ protected:
private:
UPROPERTY()
ACharacter *Walker = nullptr;
};

View File

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

View File

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

View File

@ -7,16 +7,13 @@
#include "Carla.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() {}
void ATrafficSignBase::BeginPlay()
{
auto *GameState = GetWorld()->GetGameState<ACarlaGameState>();
if (GameState != nullptr) {
GameState->RegisterTrafficSign(this);
} else {
UE_LOG(LogCarla, Error, TEXT("Missing CARLA game state!"));
}
TrafficSignAgentComponent =
CreateDefaultSubobject<UTrafficSignAgentComponent>(TEXT("TrafficSignAgentComponent"));
TrafficSignAgentComponent->SetupAttachment(RootComponent);
}

View File

@ -32,9 +32,7 @@ class CARLA_API ATrafficSignBase : public AActor {
public:
ATrafficSignBase();
virtual void BeginPlay() override;
ATrafficSignBase(const FObjectInitializer &ObjectInitializer);
UFUNCTION(BlueprintCallable)
ETrafficSignState GetTrafficSignState() const
@ -52,4 +50,7 @@ private:
UPROPERTY(Category = "Traffic Sign", EditAnywhere)
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);
VehicleAgentComponent = CreateDefaultSubobject<UVehicleAgentComponent>(TEXT("VehicleAgentComponent"));
VehicleAgentComponent->SetupAttachment(RootComponent);
GetVehicleMovementComponent()->bReverseAsBrake = false;
}