Some changes to game controllers and modes

This commit is contained in:
nsubiron 2017-03-23 10:40:59 +01:00
parent 76982ea96c
commit 9b96eea8f1
7 changed files with 58 additions and 7 deletions

View File

@ -70,6 +70,7 @@ static bool SendAndReadSceneValues(
carla::Scene_Values sceneValues;
sceneValues.possible_positions.reserve(AvailableStartSpots.Num());
for (APlayerStart *StartSpot : AvailableStartSpots) {
check(StartSpot != nullptr);
const FVector &Location = StartSpot->GetActorLocation();
UE_LOG(LogCarla, Log, TEXT("Found start position {%f, %f}"), Location.X, Location.Y);
sceneValues.possible_positions.push_back({Location.X, Location.Y});
@ -141,8 +142,12 @@ CarlaGameController::CarlaGameController() :
Player(nullptr),
Cameras({{nullptr}}) {}
APlayerStart *CarlaGameController::ChoosePlayerStart(
const TArray<APlayerStart *> &AvailableStartSpots)
CarlaGameController::~CarlaGameController()
{
UE_LOG(LogCarla, Log, TEXT("Destroying CarlaGameController..."));
}
void CarlaGameController::Initialize()
{
if (bServerNeedsRestart) {
UE_LOG(LogCarla, Log, TEXT("Initializing CarlaServer"));
@ -150,12 +155,19 @@ APlayerStart *CarlaGameController::ChoosePlayerStart(
if (ReadSceneInit(*Server)) {
bServerNeedsRestart = false;
} else {
RestartLevel(true);
UE_LOG(LogCarla, Warning, TEXT("Read scene init failed, server needs restart"));
}
}
uint32 StartIndex = 0u;
Cameras = {{nullptr}};
}
APlayerStart *CarlaGameController::ChoosePlayerStart(
const TArray<APlayerStart *> &AvailableStartSpots)
{
uint32 StartIndex;
if (!SendAndReadSceneValues(*Server, AvailableStartSpots, StartIndex)) {
RestartLevel(true);
UE_LOG(LogCarla, Warning, TEXT("Read scene values failed, server needs restart"));
StartIndex = 0u;
}
return AvailableStartSpots[StartIndex];
}
@ -182,10 +194,15 @@ void CarlaGameController::RegisterCaptureCamera(const ASceneCaptureCamera &Captu
Cameras.size());
}
void CarlaGameController::BeginPlay()
{
Server->sendEndReset();
}
void CarlaGameController::Tick(float DeltaSeconds)
{
check(Player != nullptr);
if (Server->needsRestart()) {
if (bServerNeedsRestart || Server->needsRestart()) {
RestartLevel(true);
return;
}

View File

@ -17,12 +17,18 @@ public:
CarlaGameController();
~CarlaGameController();
virtual void Initialize() override;
virtual APlayerStart *ChoosePlayerStart(const TArray<APlayerStart *> &AvailableStartSpots) override;
virtual void RegisterPlayer(AController &NewPlayer) override;
virtual void RegisterCaptureCamera(const ASceneCaptureCamera &CaptureCamera) override;
virtual void BeginPlay() override;
virtual void Tick(float DeltaSeconds) override;
private:

View File

@ -15,11 +15,15 @@ public:
virtual ~CarlaGameControllerBase() {}
virtual void Initialize() = 0;
virtual APlayerStart *ChoosePlayerStart(const TArray<APlayerStart *> &AvailableStartSpots) = 0;
virtual void RegisterPlayer(AController &NewPlayer) = 0;
virtual void RegisterCaptureCamera(const ASceneCaptureCamera &CaptureCamera) = 0;
virtual void BeginPlay() = 0;
virtual void Tick(float DeltaSeconds) = 0;
};

View File

@ -39,6 +39,7 @@ void ACarlaGameMode::InitGame(
TEXT("GameInstance is not a UCarlaGameInstance, did you forget to set it in the project settings?"));
GameInstance->InitializeGameControllerIfNotPresent(bUseMockController);
GameController = &GameInstance->GetGameController();
GameController->Initialize();
}
void ACarlaGameMode::RestartPlayer(AController* NewPlayer)
@ -51,6 +52,7 @@ void ACarlaGameMode::RestartPlayer(AController* NewPlayer)
RegisterPlayer(*NewPlayer);
return;
} else if (UnOccupiedStartPoints.Num() > 0u) {
check(GameController != nullptr);
APlayerStart *StartSpot = GameController->ChoosePlayerStart(UnOccupiedStartPoints);
if (StartSpot != nullptr) {
RestartPlayerAtPlayerStart(NewPlayer, UnOccupiedStartPoints[0u]);
@ -61,6 +63,12 @@ void ACarlaGameMode::RestartPlayer(AController* NewPlayer)
UE_LOG(LogCarla, Error, TEXT("No start spot found!"));
}
void ACarlaGameMode::BeginPlay()
{
Super::BeginPlay();
GameController->BeginPlay();
}
void ACarlaGameMode::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);

View File

@ -29,6 +29,8 @@ public:
virtual void RestartPlayer(AController *NewPlayer) override;
virtual void BeginPlay() override;
virtual void Tick(float DeltaSeconds) override;
void RegisterCaptureCamera(ASceneCaptureCamera &CaptureCamera);

View File

@ -3,10 +3,15 @@
#include "Carla.h"
#include "MockGameController.h"
void MockGameController::Initialize()
{
}
APlayerStart *MockGameController::ChoosePlayerStart(
const TArray<APlayerStart *> &AvailableStartSpots)
{
return AvailableStartSpots[0u];
return AvailableStartSpots[FMath::RandRange(0, AvailableStartSpots.Num() - 1)];
}
void MockGameController::RegisterPlayer(AController &NewPlayer)
@ -24,6 +29,11 @@ void MockGameController::RegisterCaptureCamera(const ASceneCaptureCamera &Captur
}
void MockGameController::BeginPlay()
{
}
void MockGameController::Tick(float DeltaSeconds)
{

View File

@ -9,11 +9,15 @@ class CARLA_API MockGameController : public CarlaGameControllerBase
{
public:
virtual void Initialize() override;
virtual APlayerStart *ChoosePlayerStart(const TArray<APlayerStart *> &AvailableStartSpots) override;
virtual void RegisterPlayer(AController &NewPlayer) override;
virtual void RegisterCaptureCamera(const ASceneCaptureCamera &CaptureCamera) override;
virtual void BeginPlay() override;
virtual void Tick(float DeltaSeconds) override;
};