This repository has been archived on 2026-05-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
AgrarianGameArchive/Source/AgrarianGame/AgrarianSurvivalComponent.cpp
T

271 lines
7.4 KiB
C++

// Copyright Pacificao. All Rights Reserved.
#include "AgrarianSurvivalComponent.h"
#include "AgrarianGameState.h"
#include "Engine/World.h"
#include "Net/UnrealNetwork.h"
UAgrarianSurvivalComponent::UAgrarianSurvivalComponent()
{
PrimaryComponentTick.bCanEverTick = true;
SetIsReplicatedByDefault(true);
}
void UAgrarianSurvivalComponent::BeginPlay()
{
Super::BeginPlay();
ClampSurvival();
ClampCareHistory();
BroadcastSurvivalChanged();
}
void UAgrarianSurvivalComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (!GetOwner() || !GetOwner()->HasAuthority() || !IsAlive())
{
return;
}
const float Minutes = DeltaTime / 60.0f;
Survival.Hunger -= HungerDecayPerMinute * Minutes;
Survival.Thirst -= ThirstDecayPerMinute * Minutes;
Survival.Stamina += StaminaRecoveryPerSecond * DeltaTime;
if (Survival.Stamina <= LowStaminaExhaustionThreshold)
{
Survival.Exhaustion += ExhaustionGainPerLowStaminaSecond * DeltaTime;
}
else if (Survival.Hunger > 10.0f && Survival.Thirst > 10.0f && Survival.BodyTemperature >= 35.0f)
{
Survival.Exhaustion -= ExhaustionRecoveryPerSecond * DeltaTime;
}
if (Survival.SicknessSeverity > 0.0f)
{
Survival.Exhaustion += (Survival.SicknessSeverity / 100.0f) * 0.08f * DeltaTime;
CareHistory.IllnessBurden += (Survival.SicknessSeverity / 100.0f) * 0.001f * DeltaTime;
if (Survival.Hunger > 20.0f && Survival.Thirst > 20.0f && Survival.BodyTemperature >= 35.0f)
{
Survival.SicknessSeverity -= SicknessRecoveryPerSecond * DeltaTime * FMath::Max(0.25f, CareHistory.TreatmentQuality);
}
}
if (const UWorld* World = GetWorld())
{
if (const AAgrarianGameState* AgrarianGameState = World->GetGameState<AAgrarianGameState>())
{
const float ExposureDelta = (AgrarianGameState->AmbientTemperatureC - 18.0f) * 0.002f * DeltaTime;
Survival.BodyTemperature += FMath::Clamp(ExposureDelta, -0.035f, 0.02f);
}
}
if (Survival.Hunger <= 0.0f)
{
Survival.Health -= StarvationDamagePerMinute * Minutes;
}
if (Survival.Thirst <= 0.0f)
{
Survival.Health -= DehydrationDamagePerMinute * Minutes;
}
if (Survival.BodyTemperature < 35.0f)
{
Survival.Health -= ColdDamagePerMinute * Minutes;
}
if (Survival.SicknessSeverity >= 60.0f)
{
Survival.Health -= SicknessDamagePerMinute * (Survival.SicknessSeverity / 100.0f) * Minutes;
}
ClampSurvival();
ClampCareHistory();
BroadcastSurvivalChanged();
}
void UAgrarianSurvivalComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UAgrarianSurvivalComponent, Survival);
DOREPLIFETIME(UAgrarianSurvivalComponent, CareHistory);
}
bool UAgrarianSurvivalComponent::IsAlive() const
{
return Survival.Health > 0.0f;
}
void UAgrarianSurvivalComponent::ApplyDamage(float Amount)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival.Health -= FMath::Max(0.0f, Amount);
ClampSurvival();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::RestoreHealth(float Amount)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival.Health += FMath::Max(0.0f, Amount);
ClampSurvival();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::AddFood(float Amount)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival.Hunger += FMath::Max(0.0f, Amount);
ClampSurvival();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::AddWater(float Amount)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival.Thirst += FMath::Max(0.0f, Amount);
ClampSurvival();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::AddWarmth(float DegreesCelsius)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival.BodyTemperature += DegreesCelsius;
ClampSurvival();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::AddInjury(float Severity)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival.InjurySeverity += FMath::Max(0.0f, Severity);
CareHistory.InjuryBurden += FMath::Max(0.0f, Severity) / 100.0f;
Survival.Health -= Severity * 5.0f;
ClampSurvival();
ClampCareHistory();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::AddSickness(float Severity)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
const float PositiveSeverity = FMath::Max(0.0f, Severity);
Survival.SicknessSeverity += PositiveSeverity;
CareHistory.IllnessBurden += PositiveSeverity / 100.0f;
ClampSurvival();
ClampCareHistory();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::ReduceSickness(float Amount)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival.SicknessSeverity -= FMath::Max(0.0f, Amount);
ClampSurvival();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::ApplySavedState(const FAgrarianSurvivalSnapshot& SavedSurvival, const FAgrarianCareHistorySnapshot& SavedCareHistory)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival = SavedSurvival;
CareHistory = SavedCareHistory;
ClampSurvival();
ClampCareHistory();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::SpendStamina(float Amount)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
const float PositiveAmount = FMath::Max(0.0f, Amount);
Survival.Stamina -= PositiveAmount;
Survival.Exhaustion += PositiveAmount * 0.05f;
ClampSurvival();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::AddExhaustion(float Amount)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival.Exhaustion += FMath::Max(0.0f, Amount);
ClampSurvival();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::ReduceExhaustion(float Amount)
{
if (GetOwner() && GetOwner()->HasAuthority())
{
Survival.Exhaustion -= FMath::Max(0.0f, Amount);
ClampSurvival();
BroadcastSurvivalChanged();
}
}
void UAgrarianSurvivalComponent::OnRep_Survival()
{
BroadcastSurvivalChanged();
}
void UAgrarianSurvivalComponent::OnRep_CareHistory()
{
ClampCareHistory();
BroadcastSurvivalChanged();
}
void UAgrarianSurvivalComponent::ClampSurvival()
{
Survival.Health = FMath::Clamp(Survival.Health, 0.0f, 100.0f);
Survival.Stamina = FMath::Clamp(Survival.Stamina, 0.0f, 100.0f);
Survival.Exhaustion = FMath::Clamp(Survival.Exhaustion, 0.0f, 100.0f);
Survival.Hunger = FMath::Clamp(Survival.Hunger, 0.0f, 100.0f);
Survival.Thirst = FMath::Clamp(Survival.Thirst, 0.0f, 100.0f);
Survival.BodyTemperature = FMath::Clamp(Survival.BodyTemperature, 30.0f, 42.0f);
Survival.InjurySeverity = FMath::Clamp(Survival.InjurySeverity, 0.0f, 100.0f);
Survival.SicknessSeverity = FMath::Clamp(Survival.SicknessSeverity, 0.0f, 100.0f);
}
void UAgrarianSurvivalComponent::ClampCareHistory()
{
CareHistory.NutritionQuality = FMath::Clamp(CareHistory.NutritionQuality, 0.0f, 1.0f);
CareHistory.IllnessBurden = FMath::Clamp(CareHistory.IllnessBurden, 0.0f, 1.0f);
CareHistory.InjuryBurden = FMath::Clamp(CareHistory.InjuryBurden, 0.0f, 1.0f);
CareHistory.SleepQuality = FMath::Clamp(CareHistory.SleepQuality, 0.0f, 1.0f);
CareHistory.ShelterQuality = FMath::Clamp(CareHistory.ShelterQuality, 0.0f, 1.0f);
CareHistory.StressBurden = FMath::Clamp(CareHistory.StressBurden, 0.0f, 1.0f);
CareHistory.WorkloadBurden = FMath::Clamp(CareHistory.WorkloadBurden, 0.0f, 1.0f);
CareHistory.TreatmentQuality = FMath::Clamp(CareHistory.TreatmentQuality, 0.0f, 1.0f);
}
void UAgrarianSurvivalComponent::BroadcastSurvivalChanged()
{
OnSurvivalChanged.Broadcast(Survival);
}