Files
Aura-TopDownRPG-GAS/Source/Aura/Private/Player/AuraPlayerController.cpp

207 lines
6.4 KiB
C++
Raw Normal View History

2025-10-14 22:20:59 -04:00
// Copyright Echo Devgroup
#include "Player/AuraPlayerController.h"
2025-10-16 16:52:48 -04:00
#include "AbilitySystemBlueprintLibrary.h"
2025-10-16 14:33:58 -04:00
#include "AuraGameplayTags.h"
2025-10-14 22:20:59 -04:00
#include "EnhancedInputSubsystems.h"
2025-10-16 16:52:48 -04:00
#include "NavigationPath.h"
#include "NavigationSystem.h"
2025-10-16 14:33:58 -04:00
#include "AbilitySystem/AuraAbilitySystemComponent.h"
#include "AbilitySystem/AuraAbilitySystemLibrary.h"
#include "Components/SplineComponent.h"
#include "Input/AuraInputComponent.h"
2025-10-14 22:20:59 -04:00
#include "Interact/EnemyInterface.h"
AAuraPlayerController::AAuraPlayerController()
{
bReplicates = true;
2025-10-16 14:33:58 -04:00
Spline = CreateDefaultSubobject<USplineComponent>("Spline");
2025-10-14 22:20:59 -04:00
}
void AAuraPlayerController::PlayerTick(float DeltaTime)
{
Super::PlayerTick(DeltaTime);
CursorTrace();
2025-10-16 16:52:48 -04:00
AutoRun();
}
void AAuraPlayerController::AutoRun()
{
if (!bAutoRunning) return;
if (APawn* ControlledPawn = GetPawn())
{
const FVector LocationOnSpline = Spline->FindLocationClosestToWorldLocation(ControlledPawn->GetActorLocation(), ESplineCoordinateSpace::World);
const FVector Direction = Spline->FindDirectionClosestToWorldLocation(LocationOnSpline, ESplineCoordinateSpace::World);
ControlledPawn->AddMovementInput(Direction);
const float DistanceToDestination = (LocationOnSpline - CachedDestination).Length();
if (DistanceToDestination <= AutoRunAcceptanceRadius)
{
bAutoRunning = false;
}
}
2025-10-14 22:20:59 -04:00
}
void AAuraPlayerController::CursorTrace()
{
2025-10-16 16:52:48 -04:00
2025-10-14 22:20:59 -04:00
GetHitResultUnderCursor(ECC_Visibility, false, CursorHit);
if (!CursorHit.bBlockingHit) return;
AActor* HitActor = CursorHit.GetActor();
IEnemyInterface* Enemy = Cast<IEnemyInterface>(HitActor);
2025-10-14 22:20:59 -04:00
LastActor = ThisActor;
ThisActor = Enemy;
2025-10-16 16:52:48 -04:00
if (LastActor != ThisActor)
2025-10-14 22:20:59 -04:00
{
2025-10-16 16:52:48 -04:00
if (LastActor) LastActor->UnHighlightActor();
if (ThisActor) ThisActor->HighlightActor();
2025-10-14 22:20:59 -04:00
}
}
2025-10-16 14:33:58 -04:00
void AAuraPlayerController::AbilityInputTagPressed(FGameplayTag InputTag)
{
//For Debug of key pressed
2025-10-17 16:43:49 -04:00
//GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, *InputTag.ToString());
2025-10-16 14:33:58 -04:00
//Are we targetting something?
if (InputTag.MatchesTagExact(FAuraGameplayTags::Get().Input_InputTag_LMB))
{
bTargeting = ThisActor ? true : false;
bAutoRunning = false;
}
}
void AAuraPlayerController::AbilityInputTagReleased(FGameplayTag InputTag)
{
//For Debug of Key Held
2025-10-17 16:43:49 -04:00
//GEngine->AddOnScreenDebugMessage(2, 3.f, FColor::Blue, *InputTag.ToString());
2025-10-16 16:52:48 -04:00
if (!InputTag.MatchesTagExact(FAuraGameplayTags::Get().Input_InputTag_LMB))
{
if (GetASC()) GetASC()->AbilityInputTagReleased(InputTag);
return;
}
2025-10-17 16:43:49 -04:00
if (GetASC()) GetASC()->AbilityInputTagReleased(InputTag);
if (!bTargeting && !bShiftKeyDown)
2025-10-16 16:52:48 -04:00
{
APawn* ControlledPawn = GetPawn();
//Why no Splines?
const FString msg = FString::Printf(TEXT("NavPoints: ActorLoc: %s - CachedLoc: %s"), *ControlledPawn->GetActorLocation().ToString(), *CachedDestination.ToString());
GEngine->AddOnScreenDebugMessage(5,8.f, FColor::Blue,msg);
if (FollowTime <= ShortPressThreshold && ControlledPawn)
{
UNavigationPath* NavPath = UNavigationSystemV1::FindPathToLocationSynchronously(this, ControlledPawn->GetActorLocation(), CachedDestination);
if (NavPath)
{
//DrawDebugLine(GetWorld(), ControlledPawn->GetActorLocation(), CachedDestination, FColor::Green, false, 2.0f, 0, 2.0f);
Spline->ClearSplinePoints();
for (const FVector& PointLoc : NavPath->PathPoints)
{
Spline->AddSplinePoint(PointLoc, ESplineCoordinateSpace::World);
DrawDebugSphere(GetWorld(), PointLoc, 8.f, 8, FColor::Green, false, 5.f);
CachedDestination = NavPath->PathPoints.Last();
}
bAutoRunning = true;
}
}
FollowTime = 0.f;
bTargeting = false;
}
2025-10-16 14:33:58 -04:00
}
void AAuraPlayerController::AbilityInputTagHeld(FGameplayTag InputTag)
{
//For Debug of Key Held
2025-10-16 16:52:48 -04:00
//GEngine->AddOnScreenDebugMessage(3, 3.f, FColor::Green, *InputTag.ToString());
2025-10-16 14:33:58 -04:00
if (!InputTag.MatchesTagExact(FAuraGameplayTags::Get().Input_InputTag_LMB))
{
2025-10-16 16:52:48 -04:00
if (GetASC()) GetASC()->AbilityInputTagHeld(InputTag);
return;
2025-10-16 14:33:58 -04:00
}
2025-10-17 16:43:49 -04:00
if (bTargeting || bShiftKeyDown)
2025-10-16 14:33:58 -04:00
{
2025-10-16 16:52:48 -04:00
if (GetASC()) GetASC()->AbilityInputTagHeld(InputTag);
2025-10-16 14:33:58 -04:00
}
else
{
FollowTime += GetWorld()->GetDeltaSeconds(); //Add delta seconds to FollowTime
FHitResult Hit;
2025-10-16 16:52:48 -04:00
if (CursorHit.bBlockingHit) CachedDestination = CursorHit.ImpactPoint;
2025-10-16 14:33:58 -04:00
if (APawn* ControlledPawn = GetPawn())
{
const FVector WorldDirection = (CachedDestination - ControlledPawn->GetActorLocation()).GetSafeNormal();
ControlledPawn->AddMovementInput(WorldDirection);
}
}
}
2025-10-16 16:52:48 -04:00
2025-10-16 14:33:58 -04:00
UAuraAbilitySystemComponent* AAuraPlayerController::GetASC()
{
if (AuraAbilitySystemComponent == nullptr)
{
AuraAbilitySystemComponent = Cast<UAuraAbilitySystemComponent>(UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetPawn<APawn>()));
}
return AuraAbilitySystemComponent;
}
2025-10-14 22:20:59 -04:00
void AAuraPlayerController::BeginPlay()
{
Super::BeginPlay();
check(AuraContext);
UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer());
if (Subsystem)
{
Subsystem->AddMappingContext(AuraContext,0);
}
bShowMouseCursor = true;
DefaultMouseCursor = EMouseCursor::Default;
FInputModeGameAndUI InputModeData;
InputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
InputModeData.SetHideCursorDuringCapture(false);
SetInputMode(InputModeData);
}
void AAuraPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
2025-10-16 14:33:58 -04:00
UAuraInputComponent* AuraInputComponent = CastChecked<UAuraInputComponent>(InputComponent);
AuraInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AAuraPlayerController::Move);
2025-10-17 16:43:49 -04:00
AuraInputComponent->BindAction(ShiftAction, ETriggerEvent::Started, this, &AAuraPlayerController::ShiftPressed);
AuraInputComponent->BindAction(ShiftAction, ETriggerEvent::Completed, this, &AAuraPlayerController::ShiftReleased);
2025-10-16 16:52:48 -04:00
AuraInputComponent->BindAbilityActions(InputConfig, this, &ThisClass::AbilityInputTagPressed, &ThisClass::AbilityInputTagReleased, &ThisClass::AbilityInputTagHeld);
2025-10-14 22:20:59 -04:00
}
2025-10-17 16:43:49 -04:00
2025-10-14 22:20:59 -04:00
void AAuraPlayerController::Move(const FInputActionValue& InputActionValue)
{
const FVector2D InputAxisVector = InputActionValue.Get<FVector2D>();
const FRotator Rotation = GetControlRotation();
const FRotator YawRotation(0.f, Rotation.Yaw, 0.f);
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
if (APawn* ControlledPawn = GetPawn<APawn>())
{
ControlledPawn->AddMovementInput(ForwardDirection, InputAxisVector.Y);
ControlledPawn->AddMovementInput(RightDirection, InputAxisVector.X);
}
}