Cleaned up code per lesson 108

This commit is contained in:
2025-10-16 16:52:48 -04:00
parent d436c459ec
commit d5b1691a22
41 changed files with 6986 additions and 6959 deletions

View File

@@ -1,10 +1,11 @@
// Copyright Echo Devgroup
#include "AbilitySystemBlueprintLibrary.h"
#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"
@@ -14,7 +15,6 @@
AAuraPlayerController::AAuraPlayerController()
{
bReplicates = true;
Spline = CreateDefaultSubobject<USplineComponent>("Spline");
}
@@ -23,11 +23,30 @@ 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()
{
FHitResult CursorHit;
GetHitResultUnderCursor(ECC_Visibility, false, CursorHit);
if (!CursorHit.bBlockingHit) return;
AActor* HitActor = CursorHit.GetActor();
@@ -35,54 +54,17 @@ void AAuraPlayerController::CursorTrace()
LastActor = ThisActor;
ThisActor = Enemy;
/**
*Line trace from curso. There are several scenarios
* A. LastActor is null && ThisActor is null
* - Do nothing
* B. LastActor is null && ThisActor is valid
* - Highlight ThisActor
* C. LastActor is valid && ThisActor is null
* - UnHighlight LastActor
* D. LastAcrot is valid && ThisActor is valid but not equal
* - UnHighligh LastActor
* - Highlight ThisActor
* E. LastActor is same as ThisActor
* - Do Nothing
*/
if (LastActor == nullptr)
if (LastActor != ThisActor)
{
if (ThisActor != nullptr)
{
//Case B
ThisActor->HighlightActor();
//UE_LOG(LogTemp, Warning, TEXT("Case B"));
}
if (LastActor) LastActor->UnHighlightActor();
if (ThisActor) ThisActor->HighlightActor();
}
else //LastActor is Valid
{
if (ThisActor == nullptr)
{
LastActor->UnHighlightActor();
}
else
{
if (LastActor != ThisActor) // Case D
{
LastActor->UnHighlightActor();
ThisActor->HighlightActor();
}
}
}
}
void AAuraPlayerController::AbilityInputTagPressed(FGameplayTag InputTag)
{
//For Debug of key pressed
//GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, *InputTag.ToString());
GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, *InputTag.ToString());
//Are we targetting something?
if (InputTag.MatchesTagExact(FAuraGameplayTags::Get().Input_InputTag_LMB))
@@ -97,37 +79,65 @@ void AAuraPlayerController::AbilityInputTagReleased(FGameplayTag InputTag)
{
//For Debug of Key Held
//GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, *InputTag.ToString());
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() == nullptr) return;
GetASC()->AbilityInputTagReleased(InputTag);
if (bTargeting)
{
if (GetASC()) GetASC()->AbilityInputTagReleased(InputTag);
}
else
{
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(1, 3.f, FColor::Red, *InputTag.ToString());
//GEngine->AddOnScreenDebugMessage(3, 3.f, FColor::Green, *InputTag.ToString());
if (!InputTag.MatchesTagExact(FAuraGameplayTags::Get().Input_InputTag_LMB))
{
if (GetASC() == nullptr) return;
GetASC()->AbilityInputTagHeld(InputTag);
if (GetASC()) GetASC()->AbilityInputTagHeld(InputTag);
return;
}
if (bTargeting)
{
if (GetASC())
{
GetASC()->AbilityInputTagHeld(InputTag);
}
if (GetASC()) GetASC()->AbilityInputTagHeld(InputTag);
}
else
{
FollowTime += GetWorld()->GetDeltaSeconds(); //Add delta seconds to FollowTime
FHitResult Hit;
if (GetHitResultUnderCursor(ECC_Visibility, false, Hit))
{
CachedDestination = Hit.Location;
}
if (CursorHit.bBlockingHit) CachedDestination = CursorHit.ImpactPoint;
if (APawn* ControlledPawn = GetPawn())
{
@@ -138,6 +148,7 @@ void AAuraPlayerController::AbilityInputTagHeld(FGameplayTag InputTag)
}
UAuraAbilitySystemComponent* AAuraPlayerController::GetASC()
{
if (AuraAbilitySystemComponent == nullptr)
@@ -147,18 +158,6 @@ UAuraAbilitySystemComponent* AAuraPlayerController::GetASC()
return AuraAbilitySystemComponent;
}
/* //cheated and skipped ahead to try and fix error :(
void AAuraPlayerController::HightlightActor(AActor* Actor)
{
}
void AAuraPlayerController::UnHightlightActor(AActor* Actor)
{
}
*/
void AAuraPlayerController::BeginPlay()
{
Super::BeginPlay();
@@ -187,7 +186,7 @@ void AAuraPlayerController::SetupInputComponent()
UAuraInputComponent* AuraInputComponent = CastChecked<UAuraInputComponent>(InputComponent);
AuraInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AAuraPlayerController::Move);
AuraInputComponent->BindAbilityActions(InputConfig, this, &ThisClass::AbilityInputTagPressed, &ThisClass::AbilityInputTagHeld, &ThisClass::AbilityInputTagHeld);
AuraInputComponent->BindAbilityActions(InputConfig, this, &ThisClass::AbilityInputTagPressed, &ThisClass::AbilityInputTagReleased, &ThisClass::AbilityInputTagHeld);
}
void AAuraPlayerController::Move(const FInputActionValue& InputActionValue)