Partway done with refactor to lx Player Controller

This commit is contained in:
2026-03-03 17:44:04 -05:00
parent 0b014f9390
commit ad6540a6b1
6 changed files with 128 additions and 105 deletions

View File

@@ -1,6 +1,7 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#include "LuprexGameModeBase.h"
#include "PlayerControllerBase.h"
#include "LockedWrapper.h"
#include "lpx-drvutil.hpp"
#include "Misc/Paths.h"
@@ -56,10 +57,6 @@ void ALuprexGameModeBase::ResetToInitialState()
// Clear the PlayerID
PlayerId = 0;
// Clear the look-at state;
CurrentLookAt.Init();
MustCallLookAtChanged = false;
// Reset the clocks.
EngineSeconds = 0.0;
}
@@ -171,7 +168,8 @@ void ALuprexGameModeBase::OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLev
UpdateConsoleOutput();
UpdateTangibles();
UpdatePossessedTangible();
UpdateLookAt();
AlxPlayerControllerBase *PC = Cast<AlxPlayerControllerBase>(GetWorld()->GetFirstPlayerController());
if (PC != nullptr) PC->UpdateLookAt();
}
}
@@ -258,52 +256,3 @@ ALuprexGameModeBase *ALuprexGameModeBase::FromContext(const UObject *context) {
}
void ALuprexGameModeBase::SetLookAt(const UObject *Context, const FHitResult &HitResult)
{
ALuprexGameModeBase *Mode = FromContext(Context);
if (Mode->CurrentLookAt.HitObjectHandle != HitResult.HitObjectHandle)
{
Mode->MustCallLookAtChanged = true;
}
Mode->CurrentLookAt = HitResult;
}
void ALuprexGameModeBase::SetLookAtChanged(const UObject *Context)
{
ALuprexGameModeBase *Mode = FromContext(Context);
Mode->MustCallLookAtChanged = true;
}
FVector2D ALuprexGameModeBase::GetLookAtPixel(const UObject *Context)
{
ALuprexGameModeBase *Mode = FromContext(Context);
APlayerController *pc = Context->GetWorld()->GetFirstPlayerController();
if (pc == nullptr) return FVector2D();
FVector2D ScreenPosition;
if (!UGameplayStatics::ProjectWorldToScreen(pc, Mode->CurrentLookAt.Location, ScreenPosition, false))
{
return FVector2D();
}
return ScreenPosition;
}
void ALuprexGameModeBase::UpdateLookAt() {
// Make sure the world is fully configured before we attempt to cast rays.
UlxTangible *possessed = GetGameInstance()->GetSubsystem<UlxTangibleManager>()->GetPossessedTangible();
if (possessed == nullptr) return;
APlayerController *pc = GetWorld()->GetFirstPlayerController();
if (pc == nullptr) return;
APawn *pawn = pc->GetPawn();
if (pawn == nullptr) return;
if (possessed->GetActor() != pawn) return;
APlayerCameraManager *cam = pc->PlayerCameraManager;
if (cam == nullptr) return;
CalculateLookAt(pc);
if (MustCallLookAtChanged) {
MustCallLookAtChanged = false;
LookAtChanged();
}
}

View File

@@ -3,7 +3,6 @@
#pragma once
#include "CoreMinimal.h"
#include "Engine/HitResult.h"
#include "GameFramework/GameModeBase.h"
#include "LuprexSockets.h"
#include "BreakToDebugger.h"
@@ -37,32 +36,6 @@ public:
UFUNCTION(BlueprintCallable, Category = "Luprex|Miscellaneous")
int64 GetPlayerId();
UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context"), Category = "Luprex|Look-At Detection")
static void SetLookAt(const UObject *Context, const FHitResult &HitResult);
UFUNCTION(BlueprintPure, meta = (WorldContext = "Context"),Category = "Luprex|Look-At Detection")
static const FHitResult &GetLookAt(const UObject *Context) { return FromContext(Context)->CurrentLookAt; }
UFUNCTION(BlueprintPure, meta = (WorldContext = "Context"),Category = "Luprex|Look-At Detection")
static const AActor *GetLookAtActor(const UObject *Context) { return FromContext(Context)->CurrentLookAt.GetActor(); }
UFUNCTION(BlueprintPure, meta = (WorldContext = "Context"),Category = "Luprex|Look-At Detection")
static FVector2D GetLookAtPixel(const UObject *Context);
UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context"), Category = "Luprex|Look-At Detection")
static void SetLookAtChanged(const UObject* Context);
//
// Look-At Related Events
//
UFUNCTION(BlueprintImplementableEvent, Category = "Luprex|Look-At Detection")
void CalculateLookAt(APlayerController *PlayerController);
UFUNCTION(BlueprintImplementableEvent, Category = "Luprex|Look-At Detection")
void LookAtChanged();
// Transfer console output from the Luprex engine to unreal.
void UpdateConsoleOutput();
@@ -75,10 +48,6 @@ public:
// Tell the player controller to possess that tangible.
void UpdatePossessedTangible();
// Call 'CalculateLookAt', but only if everything is valid.
// It is up to the blueprint code to actually determine what we're looking at.
void UpdateLookAt();
// If the engine wants a new copy of the lua source code,
// provide it.
void UpdateLuaSourceCode();
@@ -94,12 +63,6 @@ public:
// Get the current Luprex Game Mode Base, given a Context object.
static ALuprexGameModeBase *FromContext(const UObject *Context);
// The actor that the player is looking at, current frame.
UPROPERTY()
FHitResult CurrentLookAt;
bool MustCallLookAtChanged = false;
// The sensitivity level at which a log message triggers a debugger breakpoint.
UPROPERTY(EditAnywhere, Category="Debugging Tools")
ElxBreakToDebuggerThreshold BreakToDebuggerLogVerbosity;

View File

@@ -0,0 +1,74 @@
#include "PlayerControllerBase.h"
#include "Common.h"
#include "Tangible.h"
#include "TangibleManager.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/GameInstance.h"
AlxPlayerControllerBase *AlxPlayerControllerBase::FromContext(const UObject *Context)
{
APlayerController *PC = Context->GetWorld()->GetFirstPlayerController();
AlxPlayerControllerBase *Result = Cast<AlxPlayerControllerBase>(PC);
if (Result == nullptr)
{
UE_LOG(LogLuprexIntegration, Fatal, TEXT("Not currently using a Luprex Player Controller."));
}
return Result;
}
const FHitResult &AlxPlayerControllerBase::GetLookAt(const UObject *Context)
{
return FromContext(Context)->CurrentLookAt;
}
const AActor *AlxPlayerControllerBase::GetLookAtActor(const UObject *Context)
{
return FromContext(Context)->CurrentLookAt.GetActor();
}
void AlxPlayerControllerBase::SetLookAt(const UObject *Context, const FHitResult &HitResult)
{
AlxPlayerControllerBase *PC = FromContext(Context);
if (PC->CurrentLookAt.HitObjectHandle != HitResult.HitObjectHandle)
{
PC->MustCallLookAtChanged = true;
}
PC->CurrentLookAt = HitResult;
}
void AlxPlayerControllerBase::SetLookAtChanged(const UObject *Context)
{
AlxPlayerControllerBase *PC = FromContext(Context);
PC->MustCallLookAtChanged = true;
}
FVector2D AlxPlayerControllerBase::GetLookAtPixel(const UObject *Context)
{
AlxPlayerControllerBase *PC = FromContext(Context);
FVector2D ScreenPosition;
if (!UGameplayStatics::ProjectWorldToScreen(PC, PC->CurrentLookAt.Location, ScreenPosition, false))
{
return FVector2D();
}
return ScreenPosition;
}
void AlxPlayerControllerBase::UpdateLookAt()
{
UlxTangibleManager *TM = GetGameInstance()->GetSubsystem<UlxTangibleManager>();
if (TM == nullptr) return;
UlxTangible *Possessed = TM->GetPossessedTangible();
if (Possessed == nullptr) return;
APawn *Pawn = GetPawn();
if (Pawn == nullptr) return;
if (Possessed->GetActor() != Pawn) return;
if (PlayerCameraManager == nullptr) return;
CalculateLookAt();
if (MustCallLookAtChanged)
{
MustCallLookAtChanged = false;
LookAtChanged();
}
}

View File

@@ -0,0 +1,46 @@
#pragma once
#include "CoreMinimal.h"
#include "Engine/HitResult.h"
#include "GameFramework/PlayerController.h"
#include "PlayerControllerBase.generated.h"
UCLASS(BlueprintType, Blueprintable)
class INTEGRATION_API AlxPlayerControllerBase : public APlayerController
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context"), Category = "Luprex|Look-At Detection")
static void SetLookAt(const UObject *Context, const FHitResult &HitResult);
UFUNCTION(BlueprintPure, meta = (WorldContext = "Context"), Category = "Luprex|Look-At Detection")
static const FHitResult &GetLookAt(const UObject *Context);
UFUNCTION(BlueprintPure, meta = (WorldContext = "Context"), Category = "Luprex|Look-At Detection")
static const AActor *GetLookAtActor(const UObject *Context);
UFUNCTION(BlueprintPure, meta = (WorldContext = "Context"), Category = "Luprex|Look-At Detection")
static FVector2D GetLookAtPixel(const UObject *Context);
UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context"), Category = "Luprex|Look-At Detection")
static void SetLookAtChanged(const UObject *Context);
// Blueprint events
UFUNCTION(BlueprintImplementableEvent, Category = "Luprex|Look-At Detection")
void CalculateLookAt();
UFUNCTION(BlueprintImplementableEvent, Category = "Luprex|Look-At Detection")
void LookAtChanged();
// Called by GameMode each tick.
void UpdateLookAt();
// Get the player controller, cast to AlxPlayerControllerBase.
static AlxPlayerControllerBase *FromContext(const UObject *Context);
UPROPERTY()
FHitResult CurrentLookAt;
bool MustCallLookAtChanged = false;
};