Add CameraManagerSubsystem

This commit is contained in:
baz 2024-06-05 01:09:52 +01:00
parent 0142134d33
commit f1a71a0a0e
4 changed files with 144 additions and 3 deletions

View File

@ -0,0 +1,67 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "CameraManagerSubsystem.h"
void UCameraManagerSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
CurrentCameraVolume = nullptr;
CameraVolumeQueue.Empty();
}
void UCameraManagerSubsystem::Deinitialize()
{
Super::Deinitialize();
CurrentCameraVolume = nullptr;
CameraVolumeQueue.Empty();
}
void UCameraManagerSubsystem::SetCurrentCameraVolume(ACameraVolume* CameraVolume)
{
if (CurrentCameraVolume != CameraVolume && !CameraVolumeQueue.Contains(CameraVolume))
{
if (CurrentCameraVolume)
{
CurrentCameraVolume->SetIsActive(false);
}
QueueCameraVolume(CameraVolume);
CurrentCameraVolume = CameraVolume;
CurrentCameraVolume->MoveCameraToLocation();
}
}
void UCameraManagerSubsystem::RemoveCameraVolume(ACameraVolume* CameraVolume)
{
if (CameraVolume == CurrentCameraVolume)
{
CameraVolumeQueue.Remove(CameraVolume);
CurrentCameraVolume->SetIsActive(false);
CurrentCameraVolume = nullptr;
}
if (CameraVolumeQueue.Num() > 0)
{
CurrentCameraVolume = CameraVolumeQueue.Last();
CurrentCameraVolume->MoveCameraToLocation();
CurrentCameraVolume->SetIsActive(true);
}
}
void UCameraManagerSubsystem::QueueCameraVolume(ACameraVolume* CameraVolume)
{
if (!CameraVolumeQueue.Contains(CameraVolume))
{
CameraVolumeQueue.Push(CameraVolume);
}
}
void UCameraManagerSubsystem::DequeueCameraVolume(ACameraVolume* CameraVolume)
{
if (CameraVolumeQueue.Contains(CameraVolume))
{
CameraVolumeQueue.Remove(CameraVolume);
}
}

View File

@ -0,0 +1,41 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "CameraVolume.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "CameraManagerSubsystem.generated.h"
/**
*
*/
UCLASS()
class TANK_API UCameraManagerSubsystem : public UGameInstanceSubsystem
{
GENERATED_BODY()
UPROPERTY()
TArray<ACameraVolume*> CameraVolumeQueue;
UPROPERTY()
ACameraVolume* CurrentCameraVolume = nullptr;
public:
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
virtual void Deinitialize() override;
UFUNCTION(BlueprintCallable)
void SetCurrentCameraVolume(ACameraVolume* CameraVolume);
UFUNCTION(BlueprintCallable)
void RemoveCameraVolume(ACameraVolume* CameraVolume);
private:
UFUNCTION(BlueprintCallable)
void QueueCameraVolume(ACameraVolume* CameraVolume);
UFUNCTION(BlueprintCallable)
void DequeueCameraVolume(ACameraVolume* CameraVolume);
};

View File

@ -2,6 +2,8 @@
#include "CameraVolume.h" #include "CameraVolume.h"
#include "CameraManagerSubsystem.h"
#include "TankPlayerCharacter.h" #include "TankPlayerCharacter.h"
#include "Kismet/KismetMathLibrary.h" #include "Kismet/KismetMathLibrary.h"
@ -35,15 +37,34 @@ void ACameraVolume::Tick(float DeltaSeconds)
} }
} }
void ACameraVolume::MoveCameraToLocation()
{
if (TankPlayerCharacter)
{
TankPlayerCharacter->CameraComponent->SetWorldLocation(ArrowComponent->GetComponentTransform().GetLocation());
TankPlayerCharacter->CameraComponent->SetWorldRotation(ArrowComponent->GetComponentTransform().GetRotation());
}
}
bool ACameraVolume::GetIsActive()
{
return IsActive;
}
void ACameraVolume::SetIsActive(bool bActive)
{
IsActive = bActive;
}
void ACameraVolume::OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor) void ACameraVolume::OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor)
{ {
if (ATankPlayerCharacter* PlayerCharacter = Cast<ATankPlayerCharacter>(OtherActor)) if (ATankPlayerCharacter* PlayerCharacter = Cast<ATankPlayerCharacter>(OtherActor))
{ {
PlayerCharacter->CameraComponent->SetWorldLocation(ArrowComponent->GetComponentTransform().GetLocation());
PlayerCharacter->CameraComponent->SetWorldRotation(ArrowComponent->GetComponentTransform().GetRotation());
TankPlayerCharacter = PlayerCharacter; TankPlayerCharacter = PlayerCharacter;
IsActive = true; IsActive = true;
auto CameraManagerSubsystem = GetGameInstance()->GetSubsystem<UCameraManagerSubsystem>();
CameraManagerSubsystem->SetCurrentCameraVolume(this);
} }
} }
@ -51,4 +72,7 @@ void ACameraVolume::OnEndOverlap(AActor* OverlappedActor, AActor* OtherActor)
{ {
IsActive = false; IsActive = false;
TankPlayerCharacter = nullptr; TankPlayerCharacter = nullptr;
auto CameraManagerSubsystem = GetGameInstance()->GetSubsystem<UCameraManagerSubsystem>();
CameraManagerSubsystem->RemoveCameraVolume(this);
} }

View File

@ -45,6 +45,15 @@ protected:
public: public:
virtual void Tick(float DeltaSeconds) override; virtual void Tick(float DeltaSeconds) override;
UFUNCTION()
void MoveCameraToLocation();
UFUNCTION()
bool GetIsActive();
UFUNCTION()
void SetIsActive(bool bActive);
private: private:
UFUNCTION() UFUNCTION()
void OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor); void OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor);