From a08696819d8fc58054f6ec560b253375f3b96b8d Mon Sep 17 00:00:00 2001 From: baz Date: Fri, 22 Nov 2024 00:19:03 +0000 Subject: [PATCH] Handle inputs using Inputable interface --- Source/vampires/Inputable.cpp | 7 ++++ Source/vampires/Inputable.h | 30 +++++++++++++++++ Source/vampires/PlayerCharacter.cpp | 36 --------------------- Source/vampires/PlayerCharacter.h | 15 --------- Source/vampires/VampireCharacter.cpp | 13 ++++++-- Source/vampires/VampireCharacter.h | 14 +++++--- Source/vampires/VampirePlayerController.cpp | 34 +++++++++++++++++++ Source/vampires/VampirePlayerController.h | 11 +++++++ 8 files changed, 102 insertions(+), 58 deletions(-) create mode 100644 Source/vampires/Inputable.cpp create mode 100644 Source/vampires/Inputable.h diff --git a/Source/vampires/Inputable.cpp b/Source/vampires/Inputable.cpp new file mode 100644 index 0000000..0166497 --- /dev/null +++ b/Source/vampires/Inputable.cpp @@ -0,0 +1,7 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "Inputable.h" + + +// Add default functionality here for any IInputtable functions that are not pure virtual. diff --git a/Source/vampires/Inputable.h b/Source/vampires/Inputable.h new file mode 100644 index 0000000..5cf1ff3 --- /dev/null +++ b/Source/vampires/Inputable.h @@ -0,0 +1,30 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "UObject/Interface.h" +#include "Inputable.generated.h" + +class UInputMappingContext; +// This class does not need to be modified. +UINTERFACE() +class UInputable : public UInterface +{ + GENERATED_BODY() +}; + +/** + * + */ +class VAMPIRES_API IInputable +{ + GENERATED_BODY() + +public: + UFUNCTION(BlueprintCallable, BlueprintNativeEvent) + void Input_Move(FVector2D value); + + UFUNCTION(BlueprintCallable, BlueprintNativeEvent) + UInputMappingContext* Input_GetInputMappingContext(); +}; diff --git a/Source/vampires/PlayerCharacter.cpp b/Source/vampires/PlayerCharacter.cpp index 8a1beea..dbd6891 100644 --- a/Source/vampires/PlayerCharacter.cpp +++ b/Source/vampires/PlayerCharacter.cpp @@ -56,31 +56,6 @@ void APlayerCharacter::BeginPlay() Super::BeginPlay(); } -void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) -{ - Super::SetupPlayerInputComponent(PlayerInputComponent); - - if (AVampirePlayerController* TankPlayerController = Cast(GetController())) - { - if (UEnhancedInputLocalPlayerSubsystem* InputSystem = ULocalPlayer::GetSubsystem< - UEnhancedInputLocalPlayerSubsystem>(TankPlayerController->GetLocalPlayer())) - { - if (!InputMappingContext.IsNull()) - { - InputSystem->AddMappingContext(InputMappingContext.LoadSynchronous(), 0); - } - } - } - - if (UEnhancedInputComponent* Input = Cast(PlayerInputComponent)) - { - if (MovementAction) - { - Input->BindAction(MovementAction, ETriggerEvent::Triggered, this, &APlayerCharacter::MovementCallback); - } - } -} - UEXPComponent* APlayerCharacter::GetEXPComponent() { return EXPComponent; @@ -90,14 +65,3 @@ UGoldComponent* APlayerCharacter::GetGoldComponent() { return GoldComponent; } - -void APlayerCharacter::MovementCallback(const FInputActionInstance& Instance) -{ - PreviousMovementDirection = Instance.GetValue().Get(); - - if (PreviousMovementDirection.Size() != 0.0f) - { - AddMovementInput({0.0f, 1.0f, 0.0f}, PreviousMovementDirection.Y); - AddMovementInput({1.0f, 0.0f, 0.0f}, PreviousMovementDirection.X); - } -} diff --git a/Source/vampires/PlayerCharacter.h b/Source/vampires/PlayerCharacter.h index a9013e0..2b9c028 100644 --- a/Source/vampires/PlayerCharacter.h +++ b/Source/vampires/PlayerCharacter.h @@ -31,12 +31,6 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite) UCameraComponent* CameraComponent = nullptr; - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) - TSoftObjectPtr InputMappingContext; - - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) - UInputAction* MovementAction; - UPROPERTY(EditAnywhere, BlueprintReadWrite) UEXPComponent* EXPComponent; @@ -51,23 +45,14 @@ public: UPROPERTY(EditAnywhere) UWidgetComponent* HealthBarWidgetComponent; -private: - FTimerHandle GarlicTimerHandle; - -public: APlayerCharacter(); protected: virtual void BeginPlay() override; public: - virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override; UEXPComponent* GetEXPComponent(); UGoldComponent* GetGoldComponent(); - -private: - UFUNCTION() - void MovementCallback(const FInputActionInstance& Instance); }; diff --git a/Source/vampires/VampireCharacter.cpp b/Source/vampires/VampireCharacter.cpp index 01815c3..76df70b 100644 --- a/Source/vampires/VampireCharacter.cpp +++ b/Source/vampires/VampireCharacter.cpp @@ -35,11 +35,18 @@ void AVampireCharacter::Tick(float DeltaTime) } -// Called to bind functionality to input -void AVampireCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) +void AVampireCharacter::Input_Move_Implementation(FVector2D value) { - Super::SetupPlayerInputComponent(PlayerInputComponent); + if (value.Size() != 0.0f) + { + AddMovementInput({0.0f, 1.0f, 0.0f}, value.Y); + AddMovementInput({1.0f, 0.0f, 0.0f}, value.X); + } +} +UInputMappingContext* AVampireCharacter::Input_GetInputMappingContext_Implementation() +{ + return InputMappingContext; } UHealthComponent* AVampireCharacter::GetHealthComponent() diff --git a/Source/vampires/VampireCharacter.h b/Source/vampires/VampireCharacter.h index dc764f3..b45bd52 100644 --- a/Source/vampires/VampireCharacter.h +++ b/Source/vampires/VampireCharacter.h @@ -3,14 +3,16 @@ #pragma once #include "CoreMinimal.h" +#include "Inputable.h" #include "GameFramework/Character.h" #include "VampireCharacter.generated.h" +class UInputAction; class UHealthComponent; class UPaperFlipbookComponent; -UCLASS() -class VAMPIRES_API AVampireCharacter : public ACharacter +UCLASS(Abstract) +class VAMPIRES_API AVampireCharacter : public ACharacter, public IInputable { GENERATED_BODY() @@ -22,6 +24,9 @@ protected: UPROPERTY() UHealthComponent* HealthComponent; + UPROPERTY(EditAnywhere, BlueprintReadWrite) + TObjectPtr InputMappingContext; + public: // Sets default values for this character's properties AVampireCharacter(); @@ -34,8 +39,9 @@ public: // Called every frame virtual void Tick(float DeltaTime) override; - // Called to bind functionality to input - virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; + virtual void Input_Move_Implementation(FVector2D value) override; + + virtual UInputMappingContext* Input_GetInputMappingContext_Implementation() override; UHealthComponent* GetHealthComponent(); }; diff --git a/Source/vampires/VampirePlayerController.cpp b/Source/vampires/VampirePlayerController.cpp index 82c34ba..7084239 100644 --- a/Source/vampires/VampirePlayerController.cpp +++ b/Source/vampires/VampirePlayerController.cpp @@ -3,7 +3,10 @@ #include "VampirePlayerController.h" +#include "EnhancedInputComponent.h" +#include "EnhancedInputSubsystems.h" #include "EXPComponent.h" +#include "Inputable.h" #include "VampireGameMode.h" #include "Blueprint/UserWidget.h" #include "Kismet/GameplayStatics.h" @@ -13,6 +16,14 @@ void AVampirePlayerController::OnPossess(APawn* aPawn) { Super::OnPossess(aPawn); + if (UEnhancedInputLocalPlayerSubsystem* subsystem = ULocalPlayer::GetSubsystem(GetLocalPlayer())) + { + if (UKismetSystemLibrary::DoesImplementInterface(aPawn, UInputable::StaticClass())) + { + subsystem->AddMappingContext(IInputable::Execute_Input_GetInputMappingContext(aPawn), 0); + } + } + if (PlayerHUD) { currentPlayerHUD = CreateWidget(this, PlayerHUD.Get()); @@ -47,6 +58,29 @@ void AVampirePlayerController::OnUnPossess() GetWorld()->GetTimerManager().ClearTimer(pawnLifeTimeHandle); } +void AVampirePlayerController::SetupInputComponent() +{ + Super::SetupInputComponent(); + + if (UEnhancedInputComponent* EIP = CastChecked(InputComponent)) + { + EIP->BindAction(MovementAction, ETriggerEvent::Triggered, this, &AVampirePlayerController::Move); + } +} + +void AVampirePlayerController::Move(const FInputActionValue& MovementInput) +{ + FVector2D movement = MovementInput.Get(); + + if (APawn* pawn = GetPawn()) + { + if (UKismetSystemLibrary::DoesImplementInterface(pawn, UInputable::StaticClass())) + { + IInputable::Execute_Input_Move(pawn, movement); + } + } +} + void AVampirePlayerController::UpdatePlayerEXPHUD(int exp, float currentLevelPercent) { if (currentPlayerHUD) diff --git a/Source/vampires/VampirePlayerController.h b/Source/vampires/VampirePlayerController.h index a0fcc41..4e92740 100644 --- a/Source/vampires/VampirePlayerController.h +++ b/Source/vampires/VampirePlayerController.h @@ -6,6 +6,7 @@ #include "GameFramework/PlayerController.h" #include "VampirePlayerController.generated.h" +class UInputAction; class UHUDWidget; /** * @@ -20,6 +21,11 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite) TSubclassOf PlayerHUD = nullptr; + // Inputs + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UInputAction* MovementAction; + + private: TObjectPtr currentPlayerHUD = nullptr; @@ -31,6 +37,11 @@ protected: virtual void OnUnPossess() override; + virtual void SetupInputComponent() override; + + UFUNCTION() + void Move(const FInputActionValue& MovementInput); + UFUNCTION() void UpdatePlayerEXPHUD(int exp, float currentLevelPercent);