94 lines
2.7 KiB
C++
94 lines
2.7 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "GameInfoManager.h"
|
|
|
|
#include "QuestManager.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
|
|
FString UGameInfoManager::SaveGameSlotName = TEXT("GameInfo");
|
|
void UGameInfoManager::Initialize(FSubsystemCollectionBase& Collection)
|
|
{
|
|
Super::Initialize(Collection);
|
|
if (!LoadGameInfo())
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("本地存档不存在加载失败"));
|
|
}
|
|
}
|
|
|
|
void UGameInfoManager::SaveGameInfo()
|
|
{
|
|
if (IsValid(PlayerInfo.Get()))
|
|
{
|
|
// 保存任务数据
|
|
if (UQuestManager* QuestManager = GetGameInstance()->GetSubsystem<UQuestManager>())
|
|
{
|
|
PlayerInfo->QuestSaveData = QuestManager->GetQuestSaveData();
|
|
}
|
|
|
|
if (UGameplayStatics::SaveGameToSlot(PlayerInfo.Get(), SaveGameSlotName, 0))
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("存档保存成功"));
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("存档保存失败"));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("存档保存失败, PlayerInfo为空"));
|
|
}
|
|
}
|
|
|
|
bool UGameInfoManager::LoadGameInfo()
|
|
{
|
|
if (UGameplayStatics::DoesSaveGameExist(SaveGameSlotName, 0))
|
|
{
|
|
PlayerInfo = Cast<UPlayerInfoSaveGame>(UGameplayStatics::LoadGameFromSlot(SaveGameSlotName, 0));
|
|
|
|
// 加载任务数据
|
|
if (PlayerInfo && GetGameInstance())
|
|
{
|
|
if (UQuestManager* QuestManager = GetGameInstance()->GetSubsystem<UQuestManager>())
|
|
{
|
|
QuestManager->LoadFromSaveData(PlayerInfo->QuestSaveData);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void UGameInfoManager::CreateGameInfo(UContainerInfo* ShipContainer, UContainerInfo* PlayerContainer)
|
|
{
|
|
PlayerInfo = NewObject<UPlayerInfoSaveGame>(this);
|
|
PlayerInfo->ShipContainerItems = ShipContainer->GetSaveData();
|
|
PlayerInfo->ShipContainerShapeID = ShipContainer->ContainerShape->GetPrimaryAssetId();
|
|
|
|
PlayerInfo->PlayerContainerItems = PlayerContainer->GetSaveData();
|
|
PlayerInfo->PlayerContainerShapeID = PlayerContainer->ContainerShape->GetPrimaryAssetId();
|
|
SaveGameInfo();
|
|
}
|
|
|
|
void UGameInfoManager::CreateGameInfoAndSave(UShapeAsset* ShipContainerShape, UShapeAsset* PlayerContainerShape)
|
|
{
|
|
//创建仓库信息
|
|
UContainerInfo* ShipContainer = NewObject<UContainerInfo>();
|
|
ShipContainer->InitContainerByShape(ShipContainerShape);
|
|
|
|
UContainerInfo* PlayerContainer = NewObject<UContainerInfo>();
|
|
PlayerContainer->InitContainerByShape(PlayerContainerShape);
|
|
|
|
//创建要保存的额存档信息
|
|
PlayerInfo = NewObject<UPlayerInfoSaveGame>(this);
|
|
PlayerInfo->ShipContainerItems = ShipContainer->GetSaveData();
|
|
PlayerInfo->ShipContainerShapeID = ShipContainer->ContainerShape->GetPrimaryAssetId();
|
|
|
|
PlayerInfo->PlayerContainerItems = PlayerContainer->GetSaveData();
|
|
PlayerInfo->PlayerContainerShapeID = PlayerContainer->ContainerShape->GetPrimaryAssetId();
|
|
|
|
SaveGameInfo();
|
|
}
|