PlayerController cleanup

This commit is contained in:
2024-05-15 15:37:00 +02:00
parent 08c5fc0e37
commit 527af292a5
2 changed files with 6 additions and 69 deletions

View File

@@ -17,9 +17,6 @@ DEFINE_LOG_CATEGORY(LogTemplateCharacter);
ATerraformingAnubisPlayerController::ATerraformingAnubisPlayerController()
{
bShowMouseCursor = true;
DefaultMouseCursor = EMouseCursor::Default;
CachedDestination = FVector::ZeroVector;
FollowTime = 0.f;
}
void ATerraformingAnubisPlayerController::BeginPlay()
@@ -43,10 +40,7 @@ void ATerraformingAnubisPlayerController::SetupInputComponent()
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(InputComponent))
{
// Setup mouse input events
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Started, this, &ATerraformingAnubisPlayerController::OnInputStarted);
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Triggered, this, &ATerraformingAnubisPlayerController::OnSetDestinationTriggered);
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Completed, this, &ATerraformingAnubisPlayerController::OnSetDestinationReleased);
EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Canceled, this, &ATerraformingAnubisPlayerController::OnSetDestinationReleased);
EnhancedInputComponent->BindAction(ClickAction, ETriggerEvent::Started, this, &ATerraformingAnubisPlayerController::OnClick);
}
else
{
@@ -54,46 +48,7 @@ void ATerraformingAnubisPlayerController::SetupInputComponent()
}
}
void ATerraformingAnubisPlayerController::OnInputStarted()
void ATerraformingAnubisPlayerController::OnClick()
{
StopMovement();
}
// Triggered every frame when the input is held down
void ATerraformingAnubisPlayerController::OnSetDestinationTriggered()
{
// We flag that the input is being pressed
FollowTime += GetWorld()->GetDeltaSeconds();
// We look for the location in the world where the player has pressed the input
FHitResult Hit;
bool bHitSuccessful = false;
bHitSuccessful = GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility, true, Hit);
// If we hit a surface, cache the location
if (bHitSuccessful)
{
CachedDestination = Hit.Location;
}
// Move towards mouse pointer or touch
APawn* ControlledPawn = GetPawn();
if (ControlledPawn != nullptr)
{
FVector WorldDirection = (CachedDestination - ControlledPawn->GetActorLocation()).GetSafeNormal();
ControlledPawn->AddMovementInput(WorldDirection, 1.0, false);
}
}
void ATerraformingAnubisPlayerController::OnSetDestinationReleased()
{
// If it was a short press
if (FollowTime <= ShortPressThreshold)
{
// We move there and spawn some particles
UAIBlueprintHelperLibrary::SimpleMoveToLocation(this, CachedDestination);
UNiagaraFunctionLibrary::SpawnSystemAtLocation(this, FXCursor, CachedDestination, FRotator::ZeroRotator, FVector(1.f, 1.f, 1.f), true, true, ENCPoolMethod::None, true);
}
FollowTime = 0.f;
//
}

View File

@@ -22,40 +22,22 @@ class ATerraformingAnubisPlayerController : public APlayerController
public:
ATerraformingAnubisPlayerController();
/** Time Threshold to know if it was a short press */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
float ShortPressThreshold;
/** FX Class that we will spawn when clicking */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
UNiagaraSystem* FXCursor;
/** MappingContext */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
UInputMappingContext* DefaultMappingContext;
/** Jump Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
UInputAction* SetDestinationClickAction;
UInputAction* ClickAction;
protected:
/** True if the controlled character should navigate to the mouse cursor. */
uint32 bMoveToMouseCursor : 1;
virtual void SetupInputComponent() override;
// To add mapping context
virtual void BeginPlay();
/** Input handlers for SetDestination action. */
void OnInputStarted();
void OnSetDestinationTriggered();
void OnSetDestinationReleased();
private:
FVector CachedDestination;
float FollowTime; // For how long it has been pressed
void OnClick();
};