207 lines
6.4 KiB
C++
207 lines
6.4 KiB
C++
// Copyright Echo Devgroup
|
|
|
|
#include "Player/AuraPlayerController.h"
|
|
#include "AbilitySystemBlueprintLibrary.h"
|
|
#include "AuraGameplayTags.h"
|
|
#include "EnhancedInputSubsystems.h"
|
|
#include "NavigationPath.h"
|
|
#include "NavigationSystem.h"
|
|
#include "AbilitySystem/AuraAbilitySystemComponent.h"
|
|
#include "AbilitySystem/AuraAbilitySystemLibrary.h"
|
|
#include "Components/SplineComponent.h"
|
|
#include "Input/AuraInputComponent.h"
|
|
#include "Interact/EnemyInterface.h"
|
|
|
|
AAuraPlayerController::AAuraPlayerController()
|
|
{
|
|
bReplicates = true;
|
|
Spline = CreateDefaultSubobject<USplineComponent>("Spline");
|
|
}
|
|
|
|
void AAuraPlayerController::PlayerTick(float DeltaTime)
|
|
{
|
|
Super::PlayerTick(DeltaTime);
|
|
|
|
CursorTrace();
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
void AAuraPlayerController::CursorTrace()
|
|
{
|
|
GetHitResultUnderCursor(ECC_Visibility, false, CursorHit);
|
|
if (!CursorHit.bBlockingHit) return;
|
|
AActor* HitActor = CursorHit.GetActor();
|
|
IEnemyInterface* Enemy = Cast<IEnemyInterface>(HitActor);
|
|
|
|
LastActor = ThisActor;
|
|
ThisActor = Enemy;
|
|
|
|
if (LastActor != ThisActor)
|
|
{
|
|
if (LastActor) LastActor->UnHighlightActor();
|
|
if (ThisActor) ThisActor->HighlightActor();
|
|
}
|
|
}
|
|
|
|
void AAuraPlayerController::AbilityInputTagPressed(FGameplayTag InputTag)
|
|
{
|
|
//For Debug of key pressed
|
|
//GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, *InputTag.ToString());
|
|
|
|
//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
|
|
//GEngine->AddOnScreenDebugMessage(2, 3.f, FColor::Blue, *InputTag.ToString());
|
|
if (!InputTag.MatchesTagExact(FAuraGameplayTags::Get().Input_InputTag_LMB))
|
|
{
|
|
if (GetASC()) GetASC()->AbilityInputTagReleased(InputTag);
|
|
return;
|
|
}
|
|
if (GetASC()) GetASC()->AbilityInputTagReleased(InputTag);
|
|
|
|
if (!bTargeting && !bShiftKeyDown)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
void AAuraPlayerController::AbilityInputTagHeld(FGameplayTag InputTag)
|
|
{
|
|
//For Debug of Key Held
|
|
//GEngine->AddOnScreenDebugMessage(3, 3.f, FColor::Green, *InputTag.ToString());
|
|
|
|
if (!InputTag.MatchesTagExact(FAuraGameplayTags::Get().Input_InputTag_LMB))
|
|
{
|
|
|
|
if (GetASC()) GetASC()->AbilityInputTagHeld(InputTag);
|
|
return;
|
|
}
|
|
if (bTargeting || bShiftKeyDown)
|
|
{
|
|
if (GetASC()) GetASC()->AbilityInputTagHeld(InputTag);
|
|
}
|
|
else
|
|
{
|
|
FollowTime += GetWorld()->GetDeltaSeconds(); //Add delta seconds to FollowTime
|
|
FHitResult Hit;
|
|
if (CursorHit.bBlockingHit) CachedDestination = CursorHit.ImpactPoint;
|
|
|
|
if (APawn* ControlledPawn = GetPawn())
|
|
{
|
|
const FVector WorldDirection = (CachedDestination - ControlledPawn->GetActorLocation()).GetSafeNormal();
|
|
ControlledPawn->AddMovementInput(WorldDirection);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
UAuraAbilitySystemComponent* AAuraPlayerController::GetASC()
|
|
{
|
|
if (AuraAbilitySystemComponent == nullptr)
|
|
{
|
|
AuraAbilitySystemComponent = Cast<UAuraAbilitySystemComponent>(UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetPawn<APawn>()));
|
|
}
|
|
return AuraAbilitySystemComponent;
|
|
}
|
|
|
|
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();
|
|
|
|
UAuraInputComponent* AuraInputComponent = CastChecked<UAuraInputComponent>(InputComponent);
|
|
AuraInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AAuraPlayerController::Move);
|
|
AuraInputComponent->BindAction(ShiftAction, ETriggerEvent::Started, this, &AAuraPlayerController::ShiftPressed);
|
|
AuraInputComponent->BindAction(ShiftAction, ETriggerEvent::Completed, this, &AAuraPlayerController::ShiftReleased);
|
|
AuraInputComponent->BindAbilityActions(InputConfig, this, &ThisClass::AbilityInputTagPressed, &ThisClass::AbilityInputTagReleased, &ThisClass::AbilityInputTagHeld);
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
}
|
|
}
|