lyra_game_ue/Source/LyraGame/Player/LyraLocalPlayer.cpp
Goran Lazarevski 3bcab085f8 Initial commit
2025-03-20 11:06:26 +01:00

167 lines
4.8 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "Player/LyraLocalPlayer.h"
#include "AudioMixerBlueprintLibrary.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"
#include "Settings/LyraSettingsLocal.h"
#include "Settings/LyraSettingsShared.h"
#include "CommonUserSubsystem.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraLocalPlayer)
class UObject;
ULyraLocalPlayer::ULyraLocalPlayer()
{
}
void ULyraLocalPlayer::PostInitProperties()
{
Super::PostInitProperties();
if (ULyraSettingsLocal* LocalSettings = GetLocalSettings())
{
LocalSettings->OnAudioOutputDeviceChanged.AddUObject(this, &ULyraLocalPlayer::OnAudioOutputDeviceChanged);
}
}
void ULyraLocalPlayer::SwitchController(class APlayerController* PC)
{
Super::SwitchController(PC);
OnPlayerControllerChanged(PlayerController);
}
bool ULyraLocalPlayer::SpawnPlayActor(const FString& URL, FString& OutError, UWorld* InWorld)
{
const bool bResult = Super::SpawnPlayActor(URL, OutError, InWorld);
OnPlayerControllerChanged(PlayerController);
return bResult;
}
void ULyraLocalPlayer::InitOnlineSession()
{
OnPlayerControllerChanged(PlayerController);
Super::InitOnlineSession();
}
void ULyraLocalPlayer::OnPlayerControllerChanged(APlayerController* NewController)
{
// Stop listening for changes from the old controller
FGenericTeamId OldTeamID = FGenericTeamId::NoTeam;
if (ILyraTeamAgentInterface* ControllerAsTeamProvider = Cast<ILyraTeamAgentInterface>(LastBoundPC.Get()))
{
OldTeamID = ControllerAsTeamProvider->GetGenericTeamId();
ControllerAsTeamProvider->GetTeamChangedDelegateChecked().RemoveAll(this);
}
// Grab the current team ID and listen for future changes
FGenericTeamId NewTeamID = FGenericTeamId::NoTeam;
if (ILyraTeamAgentInterface* ControllerAsTeamProvider = Cast<ILyraTeamAgentInterface>(NewController))
{
NewTeamID = ControllerAsTeamProvider->GetGenericTeamId();
ControllerAsTeamProvider->GetTeamChangedDelegateChecked().AddDynamic(this, &ThisClass::OnControllerChangedTeam);
LastBoundPC = NewController;
}
ConditionalBroadcastTeamChanged(this, OldTeamID, NewTeamID);
}
void ULyraLocalPlayer::SetGenericTeamId(const FGenericTeamId& NewTeamID)
{
// Do nothing, we merely observe the team of our associated player controller
}
FGenericTeamId ULyraLocalPlayer::GetGenericTeamId() const
{
if (ILyraTeamAgentInterface* ControllerAsTeamProvider = Cast<ILyraTeamAgentInterface>(PlayerController))
{
return ControllerAsTeamProvider->GetGenericTeamId();
}
else
{
return FGenericTeamId::NoTeam;
}
}
FOnLyraTeamIndexChangedDelegate* ULyraLocalPlayer::GetOnTeamIndexChangedDelegate()
{
return &OnTeamChangedDelegate;
}
ULyraSettingsLocal* ULyraLocalPlayer::GetLocalSettings() const
{
return ULyraSettingsLocal::Get();
}
ULyraSettingsShared* ULyraLocalPlayer::GetSharedSettings() const
{
if (!SharedSettings)
{
// On PC it's okay to use the sync load because it only checks the disk
// This could use a platform tag to check for proper save support instead
bool bCanLoadBeforeLogin = PLATFORM_DESKTOP;
if (bCanLoadBeforeLogin)
{
SharedSettings = ULyraSettingsShared::LoadOrCreateSettings(this);
}
else
{
// We need to wait for user login to get the real settings so return temp ones
SharedSettings = ULyraSettingsShared::CreateTemporarySettings(this);
}
}
return SharedSettings;
}
void ULyraLocalPlayer::LoadSharedSettingsFromDisk(bool bForceLoad)
{
FUniqueNetIdRepl CurrentNetId = GetCachedUniqueNetId();
if (!bForceLoad && SharedSettings && CurrentNetId == NetIdForSharedSettings)
{
// Already loaded once, don't reload
return;
}
ensure(ULyraSettingsShared::AsyncLoadOrCreateSettings(this, ULyraSettingsShared::FOnSettingsLoadedEvent::CreateUObject(this, &ULyraLocalPlayer::OnSharedSettingsLoaded)));
}
void ULyraLocalPlayer::OnSharedSettingsLoaded(ULyraSettingsShared* LoadedOrCreatedSettings)
{
// The settings are applied before it gets here
if (ensure(LoadedOrCreatedSettings))
{
// This will replace the temporary or previously loaded object which will GC out normally
SharedSettings = LoadedOrCreatedSettings;
NetIdForSharedSettings = GetCachedUniqueNetId();
}
}
void ULyraLocalPlayer::OnAudioOutputDeviceChanged(const FString& InAudioOutputDeviceId)
{
FOnCompletedDeviceSwap DevicesSwappedCallback;
DevicesSwappedCallback.BindUFunction(this, FName("OnCompletedAudioDeviceSwap"));
UAudioMixerBlueprintLibrary::SwapAudioOutputDevice(GetWorld(), InAudioOutputDeviceId, DevicesSwappedCallback);
}
void ULyraLocalPlayer::OnCompletedAudioDeviceSwap(const FSwapAudioOutputResult& SwapResult)
{
if (SwapResult.Result == ESwapAudioOutputDeviceResultState::Failure)
{
}
}
void ULyraLocalPlayer::OnControllerChangedTeam(UObject* TeamAgent, int32 OldTeam, int32 NewTeam)
{
ConditionalBroadcastTeamChanged(this, IntegerToGenericTeamId(OldTeam), IntegerToGenericTeamId(NewTeam));
}