Files
2025-08-15 10:40:09 +02:00

173 lines
4.9 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "WheatherSystem.h"
#include <Camera/CameraComponent.h>
// Sets default values
AWheatherSystem::AWheatherSystem()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
RainComponent = CreateDefaultSubobject<UNiagaraComponent>(TEXT("RainComponent"));
SnowComponent = CreateDefaultSubobject<UNiagaraComponent>(TEXT("SnowComponent"));
RainComponent->bAutoActivate = false;
SnowComponent->bAutoActivate = false;
RainComponent->Deactivate();
SnowComponent->Deactivate();
SetActorLocation(FVector(0, 0, 0));
if (Sun != NULL && DayCurve != NULL && Moon != NULL) {
FRotator* SunRotation = new FRotator(0.0, 0.0, 0.0);
FRotator* MoonRotation = new FRotator(0.0, 0.0, 0.0);
SunRotation->Pitch = DayCurve->GetFloatValue(CurrentTime);
MoonRotation->Pitch = DayCurve->GetFloatValue(CurrentTime) * -1;
Moon->SetActorRotation(*MoonRotation);
Sun->SetActorRotation(*SunRotation);
UpdateWeather();
}
}
// Called when the game starts or when spawned
void AWheatherSystem::BeginPlay()
{
Super::BeginPlay();
OneHoure = (DayLengthMiutes * 60) / 24;
APawn* PlayerPawn = UGameplayStatics::GetPlayerPawn(this, 0);
USceneComponent* PlayerRoot = PlayerPawn->GetRootComponent();
UCameraComponent* PlayerCamera = PlayerPawn->FindComponentByClass<UCameraComponent>();
RainComponent->AttachToComponent(PlayerCamera, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::KeepRelative, EAttachmentRule::KeepRelative, true));
SnowComponent->AttachToComponent(PlayerCamera, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::KeepRelative, EAttachmentRule::KeepRelative, true));
const FNiagaraParameterStore& RainParameterStore = RainComponent->GetOverrideParameters();
const FNiagaraParameterStore& SnowParameterStore = SnowComponent->GetOverrideParameters();
FNiagaraVariable Variable(FNiagaraTypeDefinition::GetFloatDef(), "SpawnRate");
RainParameterStore.GetParameterValue(MaxRain, Variable);
SnowParameterStore.GetParameterValue(MaxSnow, Variable);
}
// Called every frame
void AWheatherSystem::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
double newTime = CurrentTime + (DeltaTime / OneHoure);
if (newTime > 24) {
newTime = newTime - 24;
}
CurrentTime = newTime;
if (Sun != NULL && DayCurve != NULL && Moon != NULL) {
FRotator* SunRotation = new FRotator(0.0, 0.0, 0.0);
FRotator* MoonRotation = new FRotator(0.0, 0.0, 0.0);
// Sonnenbewegung
SunRotation->Pitch = DayCurve->GetFloatValue(CurrentTime);
// Mondbewegung
MoonRotation->Pitch = SunRotation->Pitch - 180.0f;
//Rotationen anwenden
Sun->SetActorRotation(*SunRotation);
Moon->SetActorRotation(*MoonRotation);
UpdateWeather();
}
}
void AWheatherSystem::UpdateWeather() {
if (SunlightItensity != NULL && SkylightIntensity != NULL && WeatherTypeCurve != NULL) {
float weatherType = WeatherTypeCurve->GetFloatValue(CurrentTime);
Sun->GetLightComponent()->SetIntensity(
FMath::Lerp(
SunlightItensity->GetVectorValue(CurrentTime).X,
SunlightItensity->GetVectorValue(CurrentTime).Y,
weatherType));
SkyLight->GetLightComponent()->SetIntensity(
FMath::Lerp(
SkylightIntensity->GetVectorValue(CurrentTime).X,
SkylightIntensity->GetVectorValue(CurrentTime).Y,
weatherType));
}
UpdateRain();
UpdateSnow();
UpdateFog();
Sun->GetLightComponent()->SetEnableLightShaftBloom(CurrentTime > 6.0 && CurrentTime < 18.0);
Sun->GetComponent()->SetEnableLightShaftOcclusion(CurrentTime > 6.0 && CurrentTime < 18.0);
}
void AWheatherSystem::UpdateRain()
{
if (bRain){
if (RainTimelineCurve != NULL) {
float CurrentIntensity = RainTimelineCurve->GetFloatValue(CurrentTime);
if (CurrentIntensity <= 0.000001) {
RainComponent->Deactivate();
}
else {
RainComponent->Activate();
RainComponent->SetFloatParameter("SpawnRate", MaxRain * CurrentIntensity);
}
}
}
else {
RainComponent->Deactivate();
}
}
void AWheatherSystem::UpdateSnow()
{
if (bSnow){
if (SnowTimelineCurve != NULL) {
float CurrentIntensity = SnowTimelineCurve->GetFloatValue(CurrentTime);
if (CurrentIntensity <= 0.000001) {
SnowComponent->Deactivate();
}
else {
SnowComponent->Activate();
SnowComponent->SetFloatParameter("SpawnRate", MaxSnow * CurrentIntensity);
}
}
}
else {
SnowComponent->Deactivate();
}
}
void AWheatherSystem::UpdateFog()
{
if (Fog != NULL) {
UExponentialHeightFogComponent* FogComponent = Fog->GetComponent();
if (bFog) {
if (FogTimelineCurve != NULL) {
FogComponent->SetFogDensity(FogTimelineCurve->GetFloatValue(CurrentTime));
}
}
else {
FogComponent->SetFogDensity(0.005);
}
if (FogHeightFalloffCurve != NULL) {
FogComponent->SetFogHeightFalloff(FogHeightFalloffCurve->GetFloatValue(CurrentTime));
}
}
}