Handle inputs using Inputable interface

This commit is contained in:
baz 2024-11-22 00:19:03 +00:00
parent 09d795600c
commit a08696819d
8 changed files with 102 additions and 58 deletions

View File

@ -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.

View File

@ -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();
};

View File

@ -56,31 +56,6 @@ void APlayerCharacter::BeginPlay()
Super::BeginPlay();
}
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (AVampirePlayerController* TankPlayerController = Cast<AVampirePlayerController>(GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* InputSystem = ULocalPlayer::GetSubsystem<
UEnhancedInputLocalPlayerSubsystem>(TankPlayerController->GetLocalPlayer()))
{
if (!InputMappingContext.IsNull())
{
InputSystem->AddMappingContext(InputMappingContext.LoadSynchronous(), 0);
}
}
}
if (UEnhancedInputComponent* Input = Cast<UEnhancedInputComponent>(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<FVector2D>();
if (PreviousMovementDirection.Size() != 0.0f)
{
AddMovementInput({0.0f, 1.0f, 0.0f}, PreviousMovementDirection.Y);
AddMovementInput({1.0f, 0.0f, 0.0f}, PreviousMovementDirection.X);
}
}

View File

@ -31,12 +31,6 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UCameraComponent* CameraComponent = nullptr;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSoftObjectPtr<UInputMappingContext> 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);
};

View File

@ -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()

View File

@ -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<UInputMappingContext> 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();
};

View File

@ -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<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
if (UKismetSystemLibrary::DoesImplementInterface(aPawn, UInputable::StaticClass()))
{
subsystem->AddMappingContext(IInputable::Execute_Input_GetInputMappingContext(aPawn), 0);
}
}
if (PlayerHUD)
{
currentPlayerHUD = CreateWidget<UHUDWidget, AVampirePlayerController*>(this, PlayerHUD.Get());
@ -47,6 +58,29 @@ void AVampirePlayerController::OnUnPossess()
GetWorld()->GetTimerManager().ClearTimer(pawnLifeTimeHandle);
}
void AVampirePlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
if (UEnhancedInputComponent* EIP = CastChecked<UEnhancedInputComponent>(InputComponent))
{
EIP->BindAction(MovementAction, ETriggerEvent::Triggered, this, &AVampirePlayerController::Move);
}
}
void AVampirePlayerController::Move(const FInputActionValue& MovementInput)
{
FVector2D movement = MovementInput.Get<FVector2D>();
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)

View File

@ -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<UHUDWidget> PlayerHUD = nullptr;
// Inputs
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* MovementAction;
private:
TObjectPtr<UHUDWidget> 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);