Add resource node persistence
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "AgrarianGameState.h"
|
||||
#include "AgrarianInventoryComponent.h"
|
||||
#include "AgrarianPersistentActorComponent.h"
|
||||
#include "AgrarianResourceNode.h"
|
||||
#include "AgrarianSaveGame.h"
|
||||
#include "AgrarianSurvivalComponent.h"
|
||||
#include "EngineUtils.h"
|
||||
@@ -240,6 +241,71 @@ int32 UAgrarianPersistenceSubsystem::RestorePlayers(const UAgrarianSaveGame* Sav
|
||||
return RestoredCount;
|
||||
}
|
||||
|
||||
int32 UAgrarianPersistenceSubsystem::CaptureResourceNodes(UAgrarianSaveGame* SaveGame) const
|
||||
{
|
||||
if (!SaveGame)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
TArray<AAgrarianResourceNode*> ResourceNodes;
|
||||
FindResourceNodes(ResourceNodes);
|
||||
|
||||
SaveGame->ResourceNodes.Reset();
|
||||
for (const AAgrarianResourceNode* ResourceNode : ResourceNodes)
|
||||
{
|
||||
if (!ResourceNode)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const FAgrarianSavedResourceNode SavedNode = ResourceNode->CaptureResourceSaveState();
|
||||
if (SavedNode.ResourceNodeId != NAME_None)
|
||||
{
|
||||
SaveGame->ResourceNodes.Add(SavedNode);
|
||||
}
|
||||
}
|
||||
|
||||
return SaveGame->ResourceNodes.Num();
|
||||
}
|
||||
|
||||
int32 UAgrarianPersistenceSubsystem::RestoreResourceNodes(const UAgrarianSaveGame* SaveGame) const
|
||||
{
|
||||
if (!SaveGame)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
TArray<AAgrarianResourceNode*> ResourceNodes;
|
||||
FindResourceNodes(ResourceNodes);
|
||||
|
||||
int32 RestoredCount = 0;
|
||||
for (AAgrarianResourceNode* ResourceNode : ResourceNodes)
|
||||
{
|
||||
if (!ResourceNode)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const FName ResourceNodeId = ResourceNode->GetResourcePersistenceId();
|
||||
const FAgrarianSavedResourceNode* SavedNode = SaveGame->ResourceNodes.FindByPredicate(
|
||||
[ResourceNodeId](const FAgrarianSavedResourceNode& Candidate)
|
||||
{
|
||||
return Candidate.ResourceNodeId == ResourceNodeId;
|
||||
});
|
||||
|
||||
if (!SavedNode)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ResourceNode->ApplyResourceSaveState(*SavedNode);
|
||||
RestoredCount++;
|
||||
}
|
||||
|
||||
return RestoredCount;
|
||||
}
|
||||
|
||||
bool UAgrarianPersistenceSubsystem::SaveCurrentWorld() const
|
||||
{
|
||||
UAgrarianSaveGame* SaveGame = LoadOrCreateSave();
|
||||
@@ -251,6 +317,7 @@ bool UAgrarianPersistenceSubsystem::SaveCurrentWorld() const
|
||||
CaptureWorldState(SaveGame);
|
||||
CapturePlayers(SaveGame);
|
||||
CaptureWorldActors(SaveGame);
|
||||
CaptureResourceNodes(SaveGame);
|
||||
return WriteSave(SaveGame);
|
||||
}
|
||||
|
||||
@@ -268,6 +335,7 @@ bool UAgrarianPersistenceSubsystem::LoadCurrentWorld(int32& RestoredPlayerCount,
|
||||
const bool bRestoredWorldState = RestoreWorldState(SaveGame);
|
||||
RestoredPlayerCount = RestorePlayers(SaveGame);
|
||||
RestoredWorldActorCount = RestoreWorldActors(SaveGame, bClearExistingActors);
|
||||
RestoreResourceNodes(SaveGame);
|
||||
return bRestoredWorldState;
|
||||
}
|
||||
|
||||
@@ -316,6 +384,26 @@ void UAgrarianPersistenceSubsystem::FindAgrarianPlayers(TArray<AAgrarianGameChar
|
||||
}
|
||||
}
|
||||
|
||||
void UAgrarianPersistenceSubsystem::FindResourceNodes(TArray<AAgrarianResourceNode*>& OutResourceNodes) const
|
||||
{
|
||||
OutResourceNodes.Reset();
|
||||
|
||||
UWorld* World = GetWorld();
|
||||
if (!World)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (TActorIterator<AAgrarianResourceNode> ActorIt(World); ActorIt; ++ActorIt)
|
||||
{
|
||||
AAgrarianResourceNode* ResourceNode = *ActorIt;
|
||||
if (ResourceNode && !ResourceNode->IsPendingKillPending())
|
||||
{
|
||||
OutResourceNodes.Add(ResourceNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FString UAgrarianPersistenceSubsystem::GetPlayerPersistenceId(const AAgrarianGameCharacter* Character) const
|
||||
{
|
||||
if (!Character)
|
||||
|
||||
Reference in New Issue
Block a user