Lost_Edge/Source/Lost_Edge/Private/CommonFunctions.cpp

121 lines
3.3 KiB
C++

// Oleg Petruny proprietary.
#include "CommonFunctions.h"
#include "GameFramework/Actor.h"
#include "InputCoreTypes.h"
#include "Kismet/GameplayStatics.h"
#include "UObject/Object.h"
#include "CustomGameInstanceBase.h"
#include "Levels/LevelBase.h"
#include "PlayerBase.h"
bool UCommonFunctions::IsNonGameObject(UObject* object)
{
if(object->HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject))
return true;
return false;
}
FText UCommonFunctions::GetKeyDisplayName(FKey key)
{
if(key == EKeys::MouseWheelAxis)
{
return NSLOCTEXT("KeyDisplayNames", "MouseWheelAxis", "Mouse Wheel");
}
return key.GetDisplayName(false);
}
void UCommonFunctions::DestroyActorRecursively(AActor* actor)
{
TArray<AActor*> childs;
actor->GetAttachedActors(childs, true, true);
for(auto child : childs)
child->Destroy();
actor->Destroy();
}
ALevelBase* UCommonFunctions::GetCurrentLevelScript(UObject* obj)
{
if(obj)
if(auto world = obj->GetWorld())
if(auto level = world->GetCurrentLevel())
if(auto script = level->GetLevelScriptActor())
return Cast<ALevelBase>(script);
return nullptr;
}
APlayerBase* UCommonFunctions::GetPlayer(UObject* obj)
{
if(obj)
if(auto pc = UGameplayStatics::GetPlayerController(obj->GetWorld(), 0))
if(auto pawn = pc->GetPawn())
return Cast<APlayerBase>(pawn);
return nullptr;
}
namespace SlowMotion
{
FTimerHandle timer;
float targetDilation;
FWorldDilationChangedDelegate worldDilationChangedDelegate;
}
void UCommonFunctions::EnterSlowMotion(float duration)
{
if(SlowMotion::targetDilation == SlowMotion::slowDilation)
return;
SlowMotion::targetDilation = SlowMotion::slowDilation;
auto GI = UCustomGameInstanceBase::GetGameInstance();
if(!GI)
return;
auto world = GI->GetWorld();
world->GetTimerManager().SetTimer(SlowMotion::timer, []() { SlowMotionTick(); }, 0.01f, true); // wrapped in lambda due to some template function class referencing
}
void UCommonFunctions::ExitSlowMotion(float duration)
{
if(SlowMotion::targetDilation == SlowMotion::normalDilation)
return;
SlowMotion::targetDilation = SlowMotion::normalDilation;
auto GI = UCustomGameInstanceBase::GetGameInstance();
if(!GI)
return;
auto world = GI->GetWorld();
world->GetTimerManager().SetTimer(SlowMotion::timer, []() { SlowMotionTick(); }, 0.01f, true);
}
FWorldDilationChangedDelegate& UCommonFunctions::GetWorldDilationChangedDelegate()
{
return SlowMotion::worldDilationChangedDelegate;
}
void UCommonFunctions::SlowMotionTick()
{
const UWorld* world = UCustomGameInstanceBase::GetGameInstance()->GetWorld();
const float currentDilation = UGameplayStatics::GetGlobalTimeDilation(world);
float newDilation = FMath::FInterpTo(currentDilation, SlowMotion::targetDilation, 1, SlowMotion::interpolationSpeed);
UGameplayStatics::SetGlobalTimeDilation(world, newDilation);
SlowMotion::worldDilationChangedDelegate.Broadcast(newDilation);
if(FMath::IsNearlyEqual(newDilation, SlowMotion::targetDilation, UE_KINDA_SMALL_NUMBER))
{
UGameplayStatics::SetGlobalTimeDilation(world, SlowMotion::targetDilation);
world->GetTimerManager().ClearTimer(SlowMotion::timer);
}
}