From 24a7d23c967a98fceae5c32327c72b3eb9ccd4da Mon Sep 17 00:00:00 2001 From: nsubiron Date: Wed, 8 Mar 2017 19:21:12 +0000 Subject: [PATCH] Add game modes, states and controller --- Source/Carla/Game/CarlaGameMode.cpp | 15 ++++ Source/Carla/Game/CarlaGameMode.h | 19 +++++ Source/Carla/Game/CarlaGameState.cpp | 8 ++ Source/Carla/Game/CarlaGameState.h | 19 +++++ Source/Carla/Game/CarlaPlayerState.cpp | 8 ++ Source/Carla/Game/CarlaPlayerState.h | 19 +++++ Source/Carla/Game/CarlaServerController.cpp | 81 +++++++++++++++++++++ Source/Carla/Game/CarlaServerController.h | 46 ++++++++++++ 8 files changed, 215 insertions(+) create mode 100644 Source/Carla/Game/CarlaGameMode.cpp create mode 100644 Source/Carla/Game/CarlaGameMode.h create mode 100644 Source/Carla/Game/CarlaGameState.cpp create mode 100644 Source/Carla/Game/CarlaGameState.h create mode 100644 Source/Carla/Game/CarlaPlayerState.cpp create mode 100644 Source/Carla/Game/CarlaPlayerState.h create mode 100644 Source/Carla/Game/CarlaServerController.cpp create mode 100644 Source/Carla/Game/CarlaServerController.h diff --git a/Source/Carla/Game/CarlaGameMode.cpp b/Source/Carla/Game/CarlaGameMode.cpp new file mode 100644 index 000000000..348304e98 --- /dev/null +++ b/Source/Carla/Game/CarlaGameMode.cpp @@ -0,0 +1,15 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#include "Carla.h" +#include "CarlaGameMode.h" + +#include "CarlaGameState.h" +#include "CarlaPlayerState.h" +#include "CarlaServerController.h" + +ACarlaGameMode::ACarlaGameMode() +{ + PlayerControllerClass = ACarlaServerController::StaticClass(); + GameStateClass = ACarlaGameState::StaticClass(); + PlayerStateClass = ACarlaPlayerState::StaticClass(); +} diff --git a/Source/Carla/Game/CarlaGameMode.h b/Source/Carla/Game/CarlaGameMode.h new file mode 100644 index 000000000..cd2ff5455 --- /dev/null +++ b/Source/Carla/Game/CarlaGameMode.h @@ -0,0 +1,19 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#pragma once + +#include "GameFramework/GameModeBase.h" +#include "CarlaGameMode.generated.h" + +/** + * + */ +UCLASS() +class CARLA_API ACarlaGameMode : public AGameModeBase +{ + GENERATED_BODY() + +public: + + ACarlaGameMode(); +}; diff --git a/Source/Carla/Game/CarlaGameState.cpp b/Source/Carla/Game/CarlaGameState.cpp new file mode 100644 index 000000000..1e84695c4 --- /dev/null +++ b/Source/Carla/Game/CarlaGameState.cpp @@ -0,0 +1,8 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#include "Carla.h" +#include "CarlaGameState.h" + + + + diff --git a/Source/Carla/Game/CarlaGameState.h b/Source/Carla/Game/CarlaGameState.h new file mode 100644 index 000000000..9cd335222 --- /dev/null +++ b/Source/Carla/Game/CarlaGameState.h @@ -0,0 +1,19 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#pragma once + +#include "GameFramework/GameStateBase.h" +#include "CarlaGameState.generated.h" + +/** + * + */ +UCLASS() +class CARLA_API ACarlaGameState : public AGameStateBase +{ + GENERATED_BODY() + + + + +}; diff --git a/Source/Carla/Game/CarlaPlayerState.cpp b/Source/Carla/Game/CarlaPlayerState.cpp new file mode 100644 index 000000000..249aea7bf --- /dev/null +++ b/Source/Carla/Game/CarlaPlayerState.cpp @@ -0,0 +1,8 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#include "Carla.h" +#include "CarlaPlayerState.h" + + + + diff --git a/Source/Carla/Game/CarlaPlayerState.h b/Source/Carla/Game/CarlaPlayerState.h new file mode 100644 index 000000000..2b84dcabc --- /dev/null +++ b/Source/Carla/Game/CarlaPlayerState.h @@ -0,0 +1,19 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#pragma once + +#include "GameFramework/PlayerState.h" +#include "CarlaPlayerState.generated.h" + +/** + * + */ +UCLASS() +class CARLA_API ACarlaPlayerState : public APlayerState +{ + GENERATED_BODY() + + + + +}; diff --git a/Source/Carla/Game/CarlaServerController.cpp b/Source/Carla/Game/CarlaServerController.cpp new file mode 100644 index 000000000..e1706d352 --- /dev/null +++ b/Source/Carla/Game/CarlaServerController.cpp @@ -0,0 +1,81 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#include "Carla.h" +#include "CarlaServerController.h" + +#include "Camera/CameraComponent.h" +#include "GameFramework/Pawn.h" +#include "GameFramework/SpringArmComponent.h" +// #include "CarlaPlayerCameraManager.h" + +ACarlaServerController::ACarlaServerController() +{ + // PlayerCameraManagerClass = ACarlaPlayerCameraManager::StaticClass(); + bAutoManageActiveCameraTarget = false; + + // Create the spring arm component + SpringArm = CreateDefaultSubobject(TEXT("SpringArm0")); + SpringArm->TargetOffset = FVector(0.f, 0.f, 200.f); + SpringArm->SetRelativeRotation(FRotator(-15.f, 0.f, 0.f)); + SpringArm->SetupAttachment(RootComponent); + SpringArm->TargetArmLength = 600.0f; + SpringArm->bEnableCameraRotationLag = true; + SpringArm->CameraRotationLagSpeed = 7.f; + SpringArm->bInheritPitch = false; + SpringArm->bInheritRoll = false; + + // Do not collide, may clip into level. + SpringArm->bDoCollisionTest = false; + + // Create the camera component + PlayerCamera = CreateDefaultSubobject(TEXT("Camera0")); + PlayerCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName); + PlayerCamera->bUsePawnControlRotation = false; + PlayerCamera->FieldOfView = 90.f; +} + +ACarlaServerController::~ACarlaServerController() {} + +void ACarlaServerController::SetupInputComponent() +{ + Super::SetupInputComponent(); + check(InputComponent); + InputComponent->BindAxis("CameraZoom", this, &ACarlaServerController::ChangeCameraZoom); + InputComponent->BindAxis("CameraUp", this, &ACarlaServerController::ChangeCameraUp); + InputComponent->BindAxis("CameraRight", this, &ACarlaServerController::ChangeCameraRight); + InputComponent->BindAction("RestartLevel", IE_Pressed, this, &ACarlaServerController::RestartLevel); +} + +void ACarlaServerController::Possess(APawn *aPawn) +{ + Super::Possess(aPawn); + if (aPawn != nullptr) { + SpringArm->AttachToComponent( + aPawn->GetRootComponent(), + FAttachmentTransformRules::KeepRelativeTransform); + } +} + +void ACarlaServerController::CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult) +{ + PlayerCamera->GetCameraView(DeltaTime, OutResult); +} + +void ACarlaServerController::ChangeCameraZoom(float Value) +{ + SpringArm->TargetArmLength = FMath::Clamp(SpringArm->TargetArmLength + Value, 200.0f, 1e4f); +} + +void ACarlaServerController::ChangeCameraUp(float Value) +{ + auto rotation = SpringArm->GetRelativeTransform().Rotator(); + rotation.Pitch = FMath::Clamp(rotation.Pitch - Value, -80.0f, 0.0f); + SpringArm->SetRelativeRotation(rotation); +} + +void ACarlaServerController::ChangeCameraRight(float Value) +{ + auto rotation = SpringArm->GetRelativeTransform().Rotator(); + rotation.Yaw -= Value; + SpringArm->SetRelativeRotation(rotation); +} diff --git a/Source/Carla/Game/CarlaServerController.h b/Source/Carla/Game/CarlaServerController.h new file mode 100644 index 000000000..da4a70041 --- /dev/null +++ b/Source/Carla/Game/CarlaServerController.h @@ -0,0 +1,46 @@ +// CARLA, Copyright (C) 2017 Computer Vision Center (CVC) + +#pragma once + +#include "GameFramework/PlayerController.h" +#include "CarlaServerController.generated.h" + +class USpringArmComponent; +class UCameraComponent; + +/** + * + */ +UCLASS() +class CARLA_API ACarlaServerController : public APlayerController +{ + GENERATED_BODY() + +public: + + ACarlaServerController(); + + ~ACarlaServerController(); + + virtual void SetupInputComponent() override; + + virtual void Possess(APawn *aPawn) override; + + // virtual void UnPossess() override; + + virtual void CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult) override; + +private: + + void ChangeCameraZoom(float Value); + + void ChangeCameraUp(float Value); + + void ChangeCameraRight(float Value); + + UPROPERTY() + USpringArmComponent *SpringArm; + + UPROPERTY() + UCameraComponent *PlayerCamera; +};