diff --git a/Source/Nakatomi/NakatomiGameInstance.cpp b/Source/Nakatomi/NakatomiGameInstance.cpp index b91c284..9951662 100644 --- a/Source/Nakatomi/NakatomiGameInstance.cpp +++ b/Source/Nakatomi/NakatomiGameInstance.cpp @@ -3,6 +3,9 @@ #include "NakatomiGameInstance.h" +#include "PlayerCharacter.h" +#include "Kismet/GameplayStatics.h" + UNakatomiAIAttackTokenManager* UNakatomiGameInstance::GetAIAttackTokenManager() { return IsValid(AIAttackTokenManager) ? AIAttackTokenManager : (AIAttackTokenManager = NewObject(this, TEXT("AI Attack Token Manager"))); @@ -19,3 +22,89 @@ void UNakatomiGameInstance::SetCurrentLevelManager(UNakatomiLevelManager* NewLev { currentLevelManager = NewLevelManager; } + +UNakatomiSaveGame* UNakatomiGameInstance::LoadGameFromSlot(FString SaveSlotName) +{ + if (UNakatomiSaveGame* Save = Cast(UGameplayStatics::LoadGameFromSlot(SaveSlotName, 0))) + { + SaveGameObject = Save; + return SaveGameObject; + } + + return nullptr; +} + +bool UNakatomiGameInstance::SaveGame(bool ResetDefaults) +{ + if (SaveGameObject == nullptr) + { + UE_LOG(LogTemp, Warning, TEXT("SaveGame called without a loaded save game object.")); + return false; + } + + if (ResetDefaults) + { + SaveGameObject->ResetPlayerValuesToDefault(); + } + else + { + APlayerCharacter* Player = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); + SaveGameObject->PlayerHealth = Player->GetCurrentHealthCount(); + SaveGameObject->WeaponInventory = Player->WeaponInventory; + SaveGameObject->CurrentInventorySlot = Player->GetCurrentInventorySlot(); + } + + SaveGameObject->LevelName = GetWorld()->GetMapName(); + SaveGameObject->LevelName.RemoveFromStart(GetWorld()->StreamingLevelsPrefix); + + return UGameplayStatics::SaveGameToSlot(SaveGameObject, SaveGameObject->PlayerName, 0); +} + +UNakatomiSaveGame* UNakatomiGameInstance::GetSaveGameObject() +{ + return SaveGameObject; +} + +// TODO: This should either fail or append number if a save already exists with the same name +UNakatomiSaveGame* UNakatomiGameInstance::CreateNewSaveGame(FString PlayerName) +{ + if (UNakatomiSaveGame* Save = Cast( + UGameplayStatics::CreateSaveGameObject(UNakatomiSaveGame::StaticClass()))) + { + SaveGameObject->PlayerName = PlayerName; + + if (UGameplayStatics::SaveGameToSlot(Save, PlayerName, 0)) + { + SaveGameObject = Save; + return SaveGameObject; + } + } + + return nullptr; +} + +TArray UNakatomiGameInstance::GetAllSaveFilesFromDisk() +{ + TArray Files; + + IFileManager& FileManager = IFileManager::Get(); + FString SavedGamesFolder = FString{FPaths::ProjectSavedDir()} + "SaveGames/"; + + TArray FileNames; + FileManager.FindFiles(FileNames, *SavedGamesFolder, TEXT("*.sav")); + + for (FString FileName : FileNames) + { + FileName.RemoveFromEnd(".sav"); + + if (UNakatomiSaveGame* saveGameObject = Cast(UGameplayStatics::LoadGameFromSlot(FileName, 0))) + { + Files.Add({FileName, + saveGameObject->PlayerName, + saveGameObject->LevelName, + FileManager.GetTimeStamp(*(SavedGamesFolder + FileName + ".sav")).ToString()}); + } + } + + return Files; +} diff --git a/Source/Nakatomi/NakatomiGameInstance.h b/Source/Nakatomi/NakatomiGameInstance.h index ddde7d2..6bd4528 100644 --- a/Source/Nakatomi/NakatomiGameInstance.h +++ b/Source/Nakatomi/NakatomiGameInstance.h @@ -6,6 +6,8 @@ #include "Engine/GameInstance.h" #include "NakatomiAIAttackTokenManager.h" #include "NakatomiLevelManager.h" +#include "NakatomiSaveFileInfo.h" +#include "NakatomiSaveGame.h" #include "NakatomiGameInstance.generated.h" /** @@ -25,6 +27,9 @@ private: UPROPERTY() UNakatomiAIAttackTokenManager* AIAttackTokenManager; + UPROPERTY() + UNakatomiSaveGame* SaveGameObject = nullptr; + public: UFUNCTION(BlueprintCallable) @@ -35,4 +40,19 @@ public: UFUNCTION(BlueprintCallable) void SetCurrentLevelManager(UNakatomiLevelManager* NewLevelManager); + + UFUNCTION(BlueprintCallable) + UNakatomiSaveGame* LoadGameFromSlot(FString SaveSlotName); + + UFUNCTION(BlueprintCallable) + bool SaveGame(bool ResetDefaults); + + UFUNCTION(BlueprintCallable) + UNakatomiSaveGame* GetSaveGameObject(); + + UFUNCTION(BlueprintCallable) + UNakatomiSaveGame* CreateNewSaveGame(FString PlayerName); + + UFUNCTION(BlueprintCallable) + TArray GetAllSaveFilesFromDisk(); };