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();
}