Implement Line Trace through Crosshairs

This commit is contained in:
2024-09-24 18:02:33 -04:00
parent d43629a7c6
commit b2aa395c8d
14 changed files with 58 additions and 203 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
Content/Luprex/lxGameMode.uasset LFS Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,6 +1,6 @@
{ {
"FileVersion": 3, "FileVersion": 3,
"EngineAssociation": "5.2", "EngineAssociation": "7FEDF449-AA25-4EAD-95E8-57052240796D",
"Category": "", "Category": "",
"Description": "", "Description": "",
"Modules": [ "Modules": [

View File

@@ -92,7 +92,7 @@ class INTEGRATION_API UlxAnimationStepLibrary : public UObject
GENERATED_BODY() GENERATED_BODY()
public: public:
UFUNCTION(BlueprintCallable, BlueprintPure, Category = Luprex) UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Luprex")
static FString AnimationStepDebugString(const FlxAnimationStep& step); static FString AnimationStepDebugString(const FlxAnimationStep& step);
UFUNCTION(BlueprintCallable, Meta = (DefaultToSelf = "into"), Category = Luprex) UFUNCTION(BlueprintCallable, Meta = (DefaultToSelf = "into"), Category = Luprex)

View File

@@ -6,7 +6,6 @@
#include "DebugPrint.h" #include "DebugPrint.h"
#include "Tangible.h" #include "Tangible.h"
#include "TangibleManager.h" #include "TangibleManager.h"
#include "LookAtDetector.h"
#include "CommonTypes.h" #include "CommonTypes.h"
#include "AnimQueue.h" #include "AnimQueue.h"
#include <string> #include <string>
@@ -84,8 +83,9 @@ void AIntegrationGameModeBase::ResetToInitialState()
// Clear the PlayerID // Clear the PlayerID
PlayerId = 0; PlayerId = 0;
// Clear the actor selector; // Clear the look-at state;
LookAtDetector = nullptr; PreviousLookAt = nullptr;
CurrentLookAt = nullptr;
// Reset the clocks. // Reset the clocks.
EngineSeconds = 0.0; EngineSeconds = 0.0;
@@ -225,7 +225,7 @@ void AIntegrationGameModeBase::OnWorldPreActorTick(UWorld* InWorld, ELevelTick I
UpdateConsoleOutput(); UpdateConsoleOutput();
UpdateTangibles(); UpdateTangibles();
UpdatePossessedTangible(); UpdatePossessedTangible();
if (LookAtDetector != nullptr) LookAtDetector->Update(); UpdateLookAt();
} }
} }
@@ -330,40 +330,22 @@ AIntegrationGameModeBase *AIntegrationGameModeBase::GetFromContext(UObject *cont
return GetFromWorld(context->GetWorld()); return GetFromWorld(context->GetWorld());
} }
void AIntegrationGameModeBase::SetLookAtDetector(UlxLookAtActorBase *selector) { void AIntegrationGameModeBase::UpdateLookAt() {
LookAtDetector = selector; // Rotate the variables.
PreviousLookAt = CurrentLookAt;
CurrentLookAt = nullptr;
// Make sure the world is fully configured before we attempt to cast rays.
UlxTangible *possessed = TangibleManager->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(pawn, pc, cam);
} }
// /** Gets the game mode that owns this component, this will always return null on the client */
// template <class T>
// T* GetGameMode() const
// {
// // Note: Intentionally getting the game mode from the world instead of the game state as it can be null during game state initialization
// static_assert(TPointerIsConvertibleFromTo<T, AGameModeBase>::Value, "'T' template parameter to GetGameMode must be derived from AGameModeBase");
// const UWorld* World = GetWorld();
// return World ? World->GetAuthGameMode<T>() : nullptr;
// }
// // in GameplayStatics
// /** Returns the current GameModeBase or Null if it can't be retrieved, such as on the client */
// UFUNCTION(BlueprintPure, Category="Game", meta=(WorldContext="WorldContextObject"))
// static ENGINE_API class AGameModeBase* GetGameMode(const UObject* WorldContextObject);
// // Give the URL a chance to override it
// if (AGameModeBase* GameModeBase = GetGameMode<AGameModeBase>())
// {
// EffectiveBotCount = UGameplayStatics::GetIntOption(GameModeBase->OptionsString, TEXT("NumBots"), EffectiveBotCount);
// }
// // In UWorld
// /**
// * Returns the current Game Mode instance cast to the template type.
// * This can only return a valid pointer on the server and may be null if the cast fails. Will always return null on a client.
// */
// template< class T >
// T* GetAuthGameMode() const
// {
// return Cast<T>(AuthorityGameMode);
// }

View File

@@ -46,7 +46,16 @@ public:
int64 GetPlayerId(); int64 GetPlayerId();
UFUNCTION(BlueprintCallable, Category = "Luprex") UFUNCTION(BlueprintCallable, Category = "Luprex")
void SetLookAtDetector(UlxLookAtActorBase *det); void SetLookAt(AActor *actor) { CurrentLookAt = actor; }
UFUNCTION(BlueprintCallable, Category = "Luprex")
AActor *GetLookAt() const { return CurrentLookAt; }
UFUNCTION(BlueprintCallable, Category = "Luprex")
bool LookAtChanged() const { return CurrentLookAt != PreviousLookAt; }
UFUNCTION(BlueprintImplementableEvent, Category = "Luprex")
void CalculateLookAt(AActor *Player, APlayerController *PlayerController, APlayerCameraManager *Camera);
// Assemble a lua call. To call into lua: // Assemble a lua call. To call into lua:
// //
@@ -81,6 +90,10 @@ public:
// Maybe call 'BecomePossessed' on the player tangible. // Maybe call 'BecomePossessed' on the player tangible.
void UpdatePossessedTangible(); 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();
// Pre-tick and post-tick functions. // Pre-tick and post-tick functions.
void OnWorldPreActorTick(UWorld* InWorld, ELevelTick InLevelTick, float InDeltaSeconds); void OnWorldPreActorTick(UWorld* InWorld, ELevelTick InLevelTick, float InDeltaSeconds);
void OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLevelTick, float InDeltaSeconds); void OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLevelTick, float InDeltaSeconds);
@@ -97,8 +110,13 @@ public:
UPROPERTY() UPROPERTY()
UlxTangibleManager *TangibleManager; UlxTangibleManager *TangibleManager;
// The actor that the player is looking at, previous frame.
UPROPERTY() UPROPERTY()
UlxLookAtActorBase *LookAtDetector; AActor *PreviousLookAt;
// The actor that the player is looking at, current frame.
UPROPERTY()
AActor *CurrentLookAt;
// This stores the entire text currently visible in the console. // This stores the entire text currently visible in the console.
FlxConsoleOutput ConsoleOutput; FlxConsoleOutput ConsoleOutput;

View File

@@ -1,81 +0,0 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "LookAtDetector.h"
#include "IntegrationGameModeBase.h"
UlxLookAtActorBase::UlxLookAtActorBase() {
GameMode = nullptr;
Player = nullptr;
PlayerController = nullptr;
CameraManager = nullptr;
PreviousSelectedActor = nullptr;
CurrentSelectedActor = nullptr;
}
void UlxLookAtActorBase::Update() {
GameMode = nullptr;
Player = nullptr;
PlayerController = nullptr;
CameraManager = nullptr;
PreviousSelectedActor = CurrentSelectedActor;
CurrentSelectedActor = nullptr;
// Make sure the world is fully configured before we attempt to cast rays.
AIntegrationGameModeBase *gm = AIntegrationGameModeBase::GetFromWorld(GetWorld());
if (gm == nullptr) return;
UlxTangible *possessed = gm->TangibleManager->GetPossessedTangible();
if (possessed == nullptr) return;
APlayerController *pc = GetWorld()->GetFirstPlayerController();
if (pc == nullptr) return;
APawn *pawn = pc->GetPawn();
if (pawn == nullptr) return;
APlayerCameraManager *cam = pc->PlayerCameraManager;
if (cam == nullptr) return;
GameMode = gm;
Player = pawn;
PlayerController = pc;
CameraManager = cam;
Recalculate();
}
// void AIntegrationGameModeBase::CastCameraRay() {
// // The range we're using is currently hardwired.
// double range = 1000.0;
// // Get the ray in question.
// APlayerCameraManager *camManager = GetWorld()->GetFirstPlayerController()->PlayerCameraManager;
// FVector TraceStart = camManager->GetCameraLocation();
// FVector TraceVector = camManager->GetCameraRotation().Vector();
// FVector TraceEnd = TraceStart + (TraceVector * range);
// // FHitResult will hold all data returned by our line collision query
// FHitResult Hit;
// // You can use FCollisionQueryParams to further configure the query
// // Here we add ourselves to the ignored list so we won't block the trace
// FCollisionQueryParams QueryParams;
// QueryParams.AddIgnoredActor(possessed->GetActor());
// // To run the query, you need a pointer to the current level, which you can get from an Actor with GetWorld()
// // UWorld()->LineTraceSingleByChannel runs a line trace and returns the first actor hit over the provided collision channel.
// GetWorld()->LineTraceSingleByChannel(Hit, TraceStart, TraceEnd, ECC_Visibility, QueryParams);
// // You can use DrawDebug helpers and the log to help visualize and debug your trace queries.
// // DrawDebugLine(GetWorld(), TraceStart, TraceEnd, Hit.bBlockingHit ? FColor::Blue : FColor::Red, false, 2.0f, 0, 5.0f);
// // UE_LOG(LogTemp, Log, TEXT("Tracing line: %s to %s"), *TraceStart.ToCompactString(), *TraceVector.ToCompactString());
// // If the trace hit something, bBlockingHit will be true,
// // and its fields will be filled with detailed info about what was hit
// if (Hit.bBlockingHit && IsValid(Hit.GetActor())) {
// CameraRayChanged = (CameraRayTarget != Hit.GetActor());
// CameraRayTarget = Hit.GetActor();
// } else {
// CameraRayChanged = (CameraRayTarget != nullptr);
// CameraRayTarget = nullptr;
// }
// }

View File

@@ -1,67 +0,0 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Runtime/Engine/Classes/GameFramework/PlayerController.h"
#include "LookAtDetector.generated.h"
class AIntegrationGameModeBase;
/**
*
*/
UCLASS()
class INTEGRATION_API UlxLookAtActorBase : public UObject
{
GENERATED_BODY()
public:
UlxLookAtActorBase();
~UlxLookAtActorBase() {};
// Used by the IntegrationGameModeBase to trigger recalculation.
UFUNCTION(BlueprintCallable, Category = "Luprex")
void Update();
// Used in blueprints to set the result object.
UFUNCTION(BlueprintCallable, Category = "Luprex")
void SetSelectedActor(AActor *actor) { CurrentSelectedActor = actor; }
// Used to find out if the selection recently changed.
UFUNCTION(BlueprintCallable, Category = "Luprex")
bool SelectionChanged() { return (PreviousSelectedActor != CurrentSelectedActor); }
// Defined in blueprints to do the actual recalculation.
UFUNCTION(BlueprintImplementableEvent, Category = "Luprex")
void Recalculate();
public:
public:
// A pointer to an IntegrationGameModeBase.
UPROPERTY(BlueprintReadOnly)
AIntegrationGameModeBase *GameMode;
// A pointer to an actor, the current character.
UPROPERTY(BlueprintReadOnly)
AActor *Player;
// A pointer to the player controller.
UPROPERTY(BlueprintReadOnly)
APlayerController *PlayerController;
// A pointer to the camera manager.
UPROPERTY(BlueprintReadOnly)
APlayerCameraManager *CameraManager;
// The selected actor on the previous frame.
UPROPERTY(BlueprintReadOnly)
AActor *PreviousSelectedActor;
// The selected actor on the current frame.
UPROPERTY(BlueprintReadOnly)
AActor *CurrentSelectedActor;
};