LifePlay-Revival/Source/LifePlay/LPOutdoorRoomSubsystem.cpp

70 lines
2.3 KiB
C++
Raw Permalink Normal View History

// Portage UE 5.5 - voir LPOutdoorRoomSubsystem.h
#include "LPOutdoorRoomSubsystem.h"
#include "Components/SceneComponent.h"
#include "Engine/GameInstance.h"
#include "GameFramework/Actor.h"
#include "LifePlay.h"
#include "UObject/UnrealType.h"
namespace
{
// Interieurs : 0,2 a 0,4 ; exterieurs : 0,01. Le seuil laisse une marge des deux cotes.
constexpr double OutdoorScaleZ = 0.05;
const FName RoomPropertyName(TEXT("Room"));
const FName CeilingPropertyName(TEXT("WallT"));
template <typename T>
T* GetObjectProperty(const UObject* Container, FName Name)
{
const FObjectPropertyBase* Prop = Container ? CastField<FObjectPropertyBase>(Container->GetClass()->FindPropertyByName(Name)) : nullptr;
return Prop ? Cast<T>(Prop->GetObjectPropertyValue_InContainer(Container)) : nullptr;
}
}
void ULPOutdoorRoomSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
TickHandle = FTSTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateUObject(this, &ULPOutdoorRoomSubsystem::Tick));
}
void ULPOutdoorRoomSubsystem::Deinitialize()
{
FTSTicker::GetCoreTicker().RemoveTicker(TickHandle);
Super::Deinitialize();
}
bool ULPOutdoorRoomSubsystem::Tick(float DeltaTime)
{
const AActor* Room = GetObjectProperty<AActor>(GetGameInstance(), RoomPropertyName);
USceneComponent* Root = IsValid(Room) ? Room->GetRootComponent() : nullptr;
USceneComponent* Ceiling = IsValid(Room) ? GetObjectProperty<USceneComponent>(Room, CeilingPropertyName) : nullptr;
// Plafond masque precedemment mais qui n'est plus celui de la piece courante : le rendre.
if (HiddenCeiling.IsValid() && HiddenCeiling.Get() != Ceiling)
{
HiddenCeiling->SetHiddenInGame(false);
HiddenCeiling.Reset();
}
if (!Root || !Ceiling)
{
return true;
}
const bool bOutdoor = Root->GetRelativeScale3D().Z < OutdoorScaleZ;
if (bOutdoor && !Ceiling->bHiddenInGame)
{
Ceiling->SetHiddenInGame(true);
HiddenCeiling = Ceiling;
UE_LOG(LogLifePlay, Log, TEXT("Piece exterieure (echelle Z %.3f) : plafond WallT masque"), Root->GetRelativeScale3D().Z);
}
else if (!bOutdoor && HiddenCeiling.Get() == Ceiling)
{
Ceiling->SetHiddenInGame(false);
HiddenCeiling.Reset();
UE_LOG(LogLifePlay, Log, TEXT("Piece interieure (echelle Z %.3f) : plafond WallT rendu visible"), Root->GetRelativeScale3D().Z);
}
return true;
}