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)
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
class UAgrarianSaveGame;
|
||||
class UAgrarianPersistentActorComponent;
|
||||
class AAgrarianGameCharacter;
|
||||
class AAgrarianResourceNode;
|
||||
|
||||
UCLASS()
|
||||
class UAgrarianPersistenceSubsystem : public UGameInstanceSubsystem
|
||||
@@ -58,6 +59,12 @@ public:
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Persistence")
|
||||
int32 RestorePlayers(const UAgrarianSaveGame* SaveGame) const;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Persistence")
|
||||
int32 CaptureResourceNodes(UAgrarianSaveGame* SaveGame) const;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Persistence")
|
||||
int32 RestoreResourceNodes(const UAgrarianSaveGame* SaveGame) const;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Persistence")
|
||||
bool SaveCurrentWorld() const;
|
||||
|
||||
@@ -67,5 +74,6 @@ public:
|
||||
protected:
|
||||
void FindPersistentComponents(TArray<UAgrarianPersistentActorComponent*>& OutComponents) const;
|
||||
void FindAgrarianPlayers(TArray<AAgrarianGameCharacter*>& OutPlayers) const;
|
||||
void FindResourceNodes(TArray<AAgrarianResourceNode*>& OutResourceNodes) const;
|
||||
FString GetPlayerPersistenceId(const AAgrarianGameCharacter* Character) const;
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "AgrarianGameCharacter.h"
|
||||
#include "AgrarianInventoryComponent.h"
|
||||
#include "AgrarianItemDefinitionAsset.h"
|
||||
#include "AgrarianSaveGame.h"
|
||||
#include "Components/StaticMeshComponent.h"
|
||||
#include "TimerManager.h"
|
||||
#include "Net/UnrealNetwork.h"
|
||||
@@ -94,6 +95,40 @@ void AAgrarianResourceNode::OnRep_RemainingHarvests()
|
||||
UpdateDepletedState();
|
||||
}
|
||||
|
||||
FName AAgrarianResourceNode::GetResourcePersistenceId() const
|
||||
{
|
||||
return PersistenceNodeId != NAME_None ? PersistenceNodeId : GetFName();
|
||||
}
|
||||
|
||||
FAgrarianSavedResourceNode AAgrarianResourceNode::CaptureResourceSaveState() const
|
||||
{
|
||||
FAgrarianSavedResourceNode SavedNode;
|
||||
SavedNode.ResourceNodeId = GetResourcePersistenceId();
|
||||
SavedNode.RemainingHarvests = RemainingHarvests;
|
||||
SavedNode.bRespawnsForMvp = bRespawnsForMvp;
|
||||
return SavedNode;
|
||||
}
|
||||
|
||||
void AAgrarianResourceNode::ApplyResourceSaveState(const FAgrarianSavedResourceNode& SavedNode)
|
||||
{
|
||||
if (!HasAuthority() || SavedNode.ResourceNodeId == NAME_None || SavedNode.ResourceNodeId != GetResourcePersistenceId())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RemainingHarvests = FMath::Clamp(SavedNode.RemainingHarvests, 0, FMath::Max(1, MaxHarvests));
|
||||
if (RemainingHarvests > 0)
|
||||
{
|
||||
if (UWorld* World = GetWorld())
|
||||
{
|
||||
World->GetTimerManager().ClearTimer(RespawnTimerHandle);
|
||||
}
|
||||
}
|
||||
|
||||
UpdateDepletedState();
|
||||
ScheduleRespawnIfNeeded();
|
||||
}
|
||||
|
||||
bool AAgrarianResourceNode::HasRequiredTool(const AAgrarianGameCharacter* Interactor) const
|
||||
{
|
||||
if (RequiredToolItemId == NAME_None)
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "GameFramework/Actor.h"
|
||||
#include "TimerManager.h"
|
||||
#include "AgrarianInteractable.h"
|
||||
#include "AgrarianSaveGame.h"
|
||||
#include "AgrarianTypes.h"
|
||||
#include "AgrarianResourceNode.generated.h"
|
||||
|
||||
@@ -32,6 +33,9 @@ public:
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource")
|
||||
TObjectPtr<UAgrarianItemDefinitionAsset> YieldItemDefinition;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource|Persistence")
|
||||
FName PersistenceNodeId = NAME_None;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, ReplicatedUsing = OnRep_RemainingHarvests, Category = "Agrarian|Resource", meta = (ClampMin = "0"))
|
||||
int32 RemainingHarvests = 5;
|
||||
|
||||
@@ -60,6 +64,15 @@ public:
|
||||
virtual bool CanInteract_Implementation(const AAgrarianGameCharacter* Interactor) const override;
|
||||
virtual void Interact_Implementation(AAgrarianGameCharacter* Interactor) override;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Resource|Persistence")
|
||||
FName GetResourcePersistenceId() const;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Resource|Persistence")
|
||||
FAgrarianSavedResourceNode CaptureResourceSaveState() const;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Resource|Persistence")
|
||||
void ApplyResourceSaveState(const FAgrarianSavedResourceNode& SavedNode);
|
||||
|
||||
protected:
|
||||
UFUNCTION()
|
||||
void OnRep_RemainingHarvests();
|
||||
|
||||
@@ -46,6 +46,21 @@ struct FAgrarianSavedWorldActor
|
||||
TMap<FName, float> NumberState;
|
||||
};
|
||||
|
||||
USTRUCT(BlueprintType)
|
||||
struct FAgrarianSavedResourceNode
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Save")
|
||||
FName ResourceNodeId = NAME_None;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Save")
|
||||
int32 RemainingHarvests = 0;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Save")
|
||||
bool bRespawnsForMvp = false;
|
||||
};
|
||||
|
||||
UCLASS()
|
||||
class UAgrarianSaveGame : public USaveGame
|
||||
{
|
||||
@@ -72,4 +87,7 @@ public:
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Save")
|
||||
TArray<FAgrarianSavedWorldActor> WorldActors;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Save")
|
||||
TArray<FAgrarianSavedResourceNode> ResourceNodes;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user