Create LevelManager

This commit is contained in:
baz 2024-03-11 20:01:56 +00:00
parent 29246357dc
commit a183445aba
5 changed files with 91 additions and 4 deletions

View File

@ -5,6 +5,7 @@
#include <Kismet/GameplayStatics.h> #include <Kismet/GameplayStatics.h>
#include "NakatomiGameInstance.h"
#include "PlayerCharacter.h" #include "PlayerCharacter.h"
void ALevelEndTriggerVolume::BeginPlay() void ALevelEndTriggerVolume::BeginPlay()
@ -28,9 +29,9 @@ void ALevelEndTriggerVolume::OnOverlapBegin(UPrimitiveComponent* OverlappedCompo
{ {
GetCollisionComponent()->OnComponentBeginOverlap.Clear(); GetCollisionComponent()->OnComponentBeginOverlap.Clear();
if (!NextGameLevel.IsNull()) if (auto gameInstance = Cast<UNakatomiGameInstance>(UGameplayStatics::GetGameInstance(GetWorld())))
{ {
UGameplayStatics::OpenLevelBySoftObjectPtr(GetWorld(), NextGameLevel); gameInstance->GetCurrentLevelManager()->LoadNextLevel();
} }
this->Destroy(); this->Destroy();

View File

@ -6,4 +6,21 @@
UNakatomiAIAttackTokenManager* UNakatomiGameInstance::GetAIAttackTokenManager() UNakatomiAIAttackTokenManager* UNakatomiGameInstance::GetAIAttackTokenManager()
{ {
return IsValid(AIAttackTokenManager) ? AIAttackTokenManager : (AIAttackTokenManager = NewObject<UNakatomiAIAttackTokenManager>(this, TEXT("AI Attack Token Manager"))); return IsValid(AIAttackTokenManager) ? AIAttackTokenManager : (AIAttackTokenManager = NewObject<UNakatomiAIAttackTokenManager>(this, TEXT("AI Attack Token Manager")));
} }
UNakatomiLevelManager* UNakatomiGameInstance::GetCurrentLevelManager()
{
if (IsValid(currentLevelManager))
{
return currentLevelManager;
}
UE_LOG(LogTemp, Warning, TEXT("No Level manager set."))
return nullptr;
}
void UNakatomiGameInstance::SetCurrentLevelManager(UNakatomiLevelManager* NewLevelManager)
{
currentLevelManager = NewLevelManager;
}

View File

@ -5,6 +5,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "Engine/GameInstance.h" #include "Engine/GameInstance.h"
#include "NakatomiAIAttackTokenManager.h" #include "NakatomiAIAttackTokenManager.h"
#include "NakatomiLevelManager.h"
#include "NakatomiGameInstance.generated.h" #include "NakatomiGameInstance.generated.h"
/** /**
@ -15,6 +16,10 @@ class NAKATOMI_API UNakatomiGameInstance : public UGameInstance
{ {
GENERATED_BODY() GENERATED_BODY()
private:
UNakatomiLevelManager* currentLevelManager;
private: private:
UPROPERTY() UPROPERTY()
@ -24,5 +29,10 @@ public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
UNakatomiAIAttackTokenManager* GetAIAttackTokenManager(); UNakatomiAIAttackTokenManager* GetAIAttackTokenManager();
UFUNCTION(BlueprintCallable)
UNakatomiLevelManager* GetCurrentLevelManager();
UFUNCTION(BlueprintCallable)
void SetCurrentLevelManager(UNakatomiLevelManager* NewLevelManager);
}; };

View File

@ -0,0 +1,28 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "NakatomiLevelManager.h"
#include "NakatomiGameInstance.h"
#include "Kismet/GameplayStatics.h"
UNakatomiLevelManager::UNakatomiLevelManager()
{
FWorldDelegates::OnPostWorldCreation.AddUObject(this, &UNakatomiLevelManager::BeginPlay);
}
void UNakatomiLevelManager::BeginPlay(UWorld* world)
{
if (auto gameInstance = Cast<UNakatomiGameInstance>(UGameplayStatics::GetGameInstance(GetWorld())))
{
gameInstance->SetCurrentLevelManager(this);
}
}
void UNakatomiLevelManager::LoadNextLevel()
{
if (!NextLevel.IsNull())
{
UGameplayStatics::OpenLevelBySoftObjectPtr(GetWorld(), NextLevel);
}
}

View File

@ -0,0 +1,31 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "NakatomiLevelManager.generated.h"
/**
*
*/
UCLASS()
class NAKATOMI_API UNakatomiLevelManager : public UObject
{
GENERATED_BODY()
UNakatomiLevelManager();
private:
UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true"))
TSoftObjectPtr<UWorld> NextLevel;
public:
UFUNCTION()
void LoadNextLevel();
private:
UFUNCTION()
void BeginPlay(UWorld* world);
};