Connect shelter to weather protection

This commit is contained in:
2026-05-16 01:12:41 -07:00
parent 8625583faa
commit 06508061da
6 changed files with 115 additions and 3 deletions
@@ -2,6 +2,8 @@
#include "AgrarianSurvivalComponent.h"
#include "AgrarianGameState.h"
#include "AgrarianShelterActor.h"
#include "Components/BoxComponent.h"
#include "Engine/World.h"
#include "Net/UnrealNetwork.h"
@@ -32,6 +34,8 @@ void UAgrarianSurvivalComponent::TickComponent(float DeltaTime, ELevelTick TickT
Survival.Hunger -= HungerDecayPerMinute * Minutes;
Survival.Thirst -= ThirstDecayPerMinute * Minutes;
Survival.Stamina += StaminaRecoveryPerSecond * DeltaTime;
CurrentWeatherProtection = CalculateCurrentWeatherProtection();
CareHistory.ShelterQuality = FMath::FInterpTo(CareHistory.ShelterQuality, CurrentWeatherProtection, DeltaTime, 0.02f);
if (Survival.Stamina <= LowStaminaExhaustionThreshold)
{
@@ -57,7 +61,8 @@ void UAgrarianSurvivalComponent::TickComponent(float DeltaTime, ELevelTick TickT
{
if (const AAgrarianGameState* AgrarianGameState = World->GetGameState<AAgrarianGameState>())
{
const float ExposureDelta = (AgrarianGameState->AmbientTemperatureC - 18.0f) * 0.002f * DeltaTime;
const float ExposureProtectionMultiplier = 1.0f - FMath::Clamp(CurrentWeatherProtection, 0.0f, 1.0f);
const float ExposureDelta = (AgrarianGameState->AmbientTemperatureC - 18.0f) * 0.002f * DeltaTime * ExposureProtectionMultiplier;
Survival.BodyTemperature += FMath::Clamp(ExposureDelta, -0.035f, 0.02f);
}
}
@@ -74,7 +79,7 @@ void UAgrarianSurvivalComponent::TickComponent(float DeltaTime, ELevelTick TickT
if (Survival.BodyTemperature < 35.0f)
{
Survival.Health -= ColdDamagePerMinute * Minutes;
Survival.Health -= ColdDamagePerMinute * Minutes * (1.0f - FMath::Clamp(CurrentWeatherProtection, 0.0f, 1.0f));
}
if (Survival.SicknessSeverity >= 60.0f)
@@ -92,6 +97,7 @@ void UAgrarianSurvivalComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProp
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UAgrarianSurvivalComponent, Survival);
DOREPLIFETIME(UAgrarianSurvivalComponent, CareHistory);
DOREPLIFETIME(UAgrarianSurvivalComponent, CurrentWeatherProtection);
}
bool UAgrarianSurvivalComponent::IsAlive() const
@@ -229,6 +235,32 @@ void UAgrarianSurvivalComponent::ReduceExhaustion(float Amount)
}
}
float UAgrarianSurvivalComponent::CalculateCurrentWeatherProtection() const
{
const AActor* Owner = GetOwner();
if (!Owner)
{
return 0.0f;
}
TArray<AActor*> OverlappingShelterActors;
Owner->GetOverlappingActors(OverlappingShelterActors, AAgrarianShelterActor::StaticClass());
float BestProtection = 0.0f;
for (const AActor* Actor : OverlappingShelterActors)
{
const AAgrarianShelterActor* Shelter = Cast<AAgrarianShelterActor>(Actor);
if (!Shelter || !Shelter->ProtectionVolume || !Shelter->ProtectionVolume->IsOverlappingActor(Owner))
{
continue;
}
BestProtection = FMath::Max(BestProtection, FMath::Clamp(Shelter->WeatherProtection, 0.0f, 1.0f));
}
return BestProtection;
}
void UAgrarianSurvivalComponent::OnRep_Survival()
{
BroadcastSurvivalChanged();