Add MVP resource respawn rules

This commit is contained in:
2026-05-17 16:26:31 -07:00
parent fa9d1835f9
commit a5ec210cd8
12 changed files with 199 additions and 9 deletions
@@ -5,6 +5,7 @@
#include "AgrarianInventoryComponent.h"
#include "AgrarianItemDefinitionAsset.h"
#include "Components/StaticMeshComponent.h"
#include "TimerManager.h"
#include "Net/UnrealNetwork.h"
AAgrarianResourceNode::AAgrarianResourceNode()
@@ -21,6 +22,23 @@ AAgrarianResourceNode::AAgrarianResourceNode()
YieldItem.UnitWeight = 1.0f;
}
void AAgrarianResourceNode::BeginPlay()
{
Super::BeginPlay();
if (HasAuthority())
{
MaxHarvests = FMath::Max(1, MaxHarvests);
if (RemainingHarvests > MaxHarvests)
{
MaxHarvests = RemainingHarvests;
}
}
UpdateDepletedState();
ScheduleRespawnIfNeeded();
}
void AAgrarianResourceNode::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
@@ -51,6 +69,7 @@ void AAgrarianResourceNode::Interact_Implementation(AAgrarianGameCharacter* Inte
{
RemainingHarvests--;
UpdateDepletedState();
ScheduleRespawnIfNeeded();
}
}
}
@@ -80,3 +99,35 @@ void AAgrarianResourceNode::UpdateDepletedState()
Mesh->SetCollisionEnabled(RemainingHarvests > 0 ? ECollisionEnabled::QueryAndPhysics : ECollisionEnabled::NoCollision);
}
}
void AAgrarianResourceNode::ScheduleRespawnIfNeeded()
{
if (!HasAuthority() || !bRespawnsForMvp || RemainingHarvests > 0)
{
return;
}
UWorld* World = GetWorld();
if (!World || World->GetTimerManager().IsTimerActive(RespawnTimerHandle))
{
return;
}
World->GetTimerManager().SetTimer(
RespawnTimerHandle,
this,
&AAgrarianResourceNode::RespawnNode,
FMath::Max(1.0f, RespawnDelaySeconds),
false);
}
void AAgrarianResourceNode::RespawnNode()
{
if (!HasAuthority() || !bRespawnsForMvp)
{
return;
}
RemainingHarvests = FMath::Max(1, MaxHarvests);
UpdateDepletedState();
}
@@ -4,6 +4,7 @@
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TimerManager.h"
#include "AgrarianInteractable.h"
#include "AgrarianTypes.h"
#include "AgrarianResourceNode.generated.h"
@@ -19,6 +20,7 @@ class AAgrarianResourceNode : public AActor, public IAgrarianInteractable
public:
AAgrarianResourceNode();
virtual void BeginPlay() override;
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Agrarian|Resource")
@@ -36,6 +38,15 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource", meta = (ClampMin = "1"))
int32 QuantityPerHarvest = 1;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource|Respawn")
bool bRespawnsForMvp = false;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource|Respawn", meta = (ClampMin = "1"))
float RespawnDelaySeconds = 900.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Resource|Respawn", meta = (ClampMin = "1"))
int32 MaxHarvests = 5;
virtual FText GetInteractionText_Implementation(const AAgrarianGameCharacter* Interactor) const override;
virtual bool CanInteract_Implementation(const AAgrarianGameCharacter* Interactor) const override;
virtual void Interact_Implementation(AAgrarianGameCharacter* Interactor) override;
@@ -46,4 +57,8 @@ protected:
FAgrarianItemStack MakeYieldStack() const;
void UpdateDepletedState();
void ScheduleRespawnIfNeeded();
void RespawnNode();
FTimerHandle RespawnTimerHandle;
};