From 3bfe5ec9d1073534ccc519bc10cef15ae208096a Mon Sep 17 00:00:00 2001 From: Manuel Wagner Date: Wed, 2 Nov 2022 11:42:10 +0100 Subject: [PATCH] Added autopilot spectator --- Source/LyraGame/GameModes/LyraGameMode.cpp | 2 + Source/LyraGame/LyraGame.Build.cs | 2 + .../LyraGame/Player/LyraPlayerController.cpp | 27 +++++++++++ Source/LyraGame/Player/LyraPlayerController.h | 1 + Source/LyraGame/Player/LyraSpectatorPawn.cpp | 48 +++++++++++++++++++ Source/LyraGame/Player/LyraSpectatorPawn.h | 22 +++++++++ 6 files changed, 102 insertions(+) create mode 100644 Source/LyraGame/Player/LyraSpectatorPawn.cpp create mode 100644 Source/LyraGame/Player/LyraSpectatorPawn.h diff --git a/Source/LyraGame/GameModes/LyraGameMode.cpp b/Source/LyraGame/GameModes/LyraGameMode.cpp index d064454a..8e34a4e1 100644 --- a/Source/LyraGame/GameModes/LyraGameMode.cpp +++ b/Source/LyraGame/GameModes/LyraGameMode.cpp @@ -19,6 +19,7 @@ #include "Development/LyraDeveloperSettings.h" #include "Player/LyraPlayerSpawningManagerComponent.h" #include "TimerManager.h" +#include "Player/LyraSpectatorPawn.h" ALyraGameMode::ALyraGameMode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) @@ -29,6 +30,7 @@ ALyraGameMode::ALyraGameMode(const FObjectInitializer& ObjectInitializer) ReplaySpectatorPlayerControllerClass = ALyraReplayPlayerController::StaticClass(); PlayerStateClass = ALyraPlayerState::StaticClass(); DefaultPawnClass = ALyraCharacter::StaticClass(); + SpectatorClass = ALyraSpectatorPawn::StaticClass(); HUDClass = ALyraHUD::StaticClass(); } diff --git a/Source/LyraGame/LyraGame.Build.cs b/Source/LyraGame/LyraGame.Build.cs index 6e910b42..8f738ab4 100644 --- a/Source/LyraGame/LyraGame.Build.cs +++ b/Source/LyraGame/LyraGame.Build.cs @@ -71,6 +71,8 @@ public class LyraGame : ModuleRules "ClientPilot", "AudioModulation", "EngineSettings", + "UnrealEd", + "LevelEditor", } ); diff --git a/Source/LyraGame/Player/LyraPlayerController.cpp b/Source/LyraGame/Player/LyraPlayerController.cpp index 57b8d31f..0234906d 100644 --- a/Source/LyraGame/Player/LyraPlayerController.cpp +++ b/Source/LyraGame/Player/LyraPlayerController.cpp @@ -14,7 +14,9 @@ #include "GameFramework/Pawn.h" #include "AbilitySystemGlobals.h" #include "CommonInputSubsystem.h" +#include "LevelEditorSubsystem.h" #include "LyraLocalPlayer.h" +#include "Character/LyraCharacter.h" #include "Settings/LyraSettingsShared.h" #include "Development/LyraDeveloperSettings.h" @@ -44,6 +46,31 @@ void ALyraPlayerController::EndPlay(const EEndPlayReason::Type EndPlayReason) Super::EndPlay(EndPlayReason); } +void ALyraPlayerController::Tick(float DeltaSeconds) +{ + Super::Tick(DeltaSeconds); + + + ULevelEditorSubsystem* LevelEditorSubsystem = GEditor->GetEditorSubsystem(); + AActor* ActorToPilot = LevelEditorSubsystem->GetPilotLevelActor(); + if (Cast(ActorToPilot)) + return; + + for (ALyraCharacter* LyraCharacter : TActorRange(GetWorld())) + { + if(Cast(LyraCharacter->GetController())) + continue; + + ActorToPilot = LyraCharacter; + break; + } + + if (!IsValid(ActorToPilot)) + return; + + LevelEditorSubsystem->PilotLevelActor(ActorToPilot); +} + void ALyraPlayerController::ReceivedPlayer() { Super::ReceivedPlayer(); diff --git a/Source/LyraGame/Player/LyraPlayerController.h b/Source/LyraGame/Player/LyraPlayerController.h index ecfe6577..26ff228d 100644 --- a/Source/LyraGame/Player/LyraPlayerController.h +++ b/Source/LyraGame/Player/LyraPlayerController.h @@ -61,6 +61,7 @@ public: virtual void PreInitializeComponents() override; virtual void BeginPlay() override; virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override; + virtual void Tick(float DeltaSeconds) override; //~End of AActor interface //~AController interface diff --git a/Source/LyraGame/Player/LyraSpectatorPawn.cpp b/Source/LyraGame/Player/LyraSpectatorPawn.cpp new file mode 100644 index 00000000..71abc185 --- /dev/null +++ b/Source/LyraGame/Player/LyraSpectatorPawn.cpp @@ -0,0 +1,48 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "LyraSpectatorPawn.h" + +#include "Editor.h" +#include "EngineUtils.h" +#include "LevelEditorSubsystem.h" +#include "Character/LyraCharacter.h" +#include "Character/LyraPawn.h" + + +// Sets default values +ALyraSpectatorPawn::ALyraSpectatorPawn() +{ + // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. + PrimaryActorTick.bCanEverTick = true; +} + +void ALyraSpectatorPawn::BeginPlay() +{ + Super::BeginPlay(); + + ULevelEditorSubsystem* LevelEditorSubsystem = GEditor->GetEditorSubsystem(); + LevelEditorSubsystem->EjectPilotLevelActor(); +} + +// Called every frame +void ALyraSpectatorPawn::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + + ULevelEditorSubsystem* LevelEditorSubsystem = GEditor->GetEditorSubsystem(); + AActor* ActorToPilot = LevelEditorSubsystem->GetPilotLevelActor(); + if (Cast(ActorToPilot)) + return; + + for (ALyraCharacter* Character : TActorRange(GetWorld())) + { + ActorToPilot = Character; + break; + } + + if (!IsValid(ActorToPilot)) + return; + + LevelEditorSubsystem->PilotLevelActor(ActorToPilot); +} diff --git a/Source/LyraGame/Player/LyraSpectatorPawn.h b/Source/LyraGame/Player/LyraSpectatorPawn.h new file mode 100644 index 00000000..41c8116b --- /dev/null +++ b/Source/LyraGame/Player/LyraSpectatorPawn.h @@ -0,0 +1,22 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Pawn.h" +#include "GameFramework/SpectatorPawn.h" +#include "LyraSpectatorPawn.generated.h" + +UCLASS() +class LYRAGAME_API ALyraSpectatorPawn : public ASpectatorPawn +{ + GENERATED_BODY() + +public: + ALyraSpectatorPawn(); + + virtual void BeginPlay() override; + + virtual void Tick(float DeltaTime) override; + +};