Compare commits

...

2 Commits

Author SHA1 Message Date
baz eae0dbb802 Update Player blueprints 2024-11-22 00:19:22 +00:00
baz a08696819d Handle inputs using Inputable interface 2024-11-22 00:19:03 +00:00
10 changed files with 106 additions and 62 deletions

BIN
Content/Player/BP_PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Player/BP_PlayerController.uasset (Stored with Git LFS)

Binary file not shown.

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(); 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() UEXPComponent* APlayerCharacter::GetEXPComponent()
{ {
return EXPComponent; return EXPComponent;
@ -90,14 +65,3 @@ UGoldComponent* APlayerCharacter::GetGoldComponent()
{ {
return GoldComponent; 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) UPROPERTY(EditAnywhere, BlueprintReadWrite)
UCameraComponent* CameraComponent = nullptr; UCameraComponent* CameraComponent = nullptr;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSoftObjectPtr<UInputMappingContext> InputMappingContext;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* MovementAction;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
UEXPComponent* EXPComponent; UEXPComponent* EXPComponent;
@ -51,23 +45,14 @@ public:
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
UWidgetComponent* HealthBarWidgetComponent; UWidgetComponent* HealthBarWidgetComponent;
private:
FTimerHandle GarlicTimerHandle;
public:
APlayerCharacter(); APlayerCharacter();
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
public: public:
virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;
UEXPComponent* GetEXPComponent(); UEXPComponent* GetEXPComponent();
UGoldComponent* GetGoldComponent(); 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::Input_Move_Implementation(FVector2D value)
void AVampireCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{ {
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() UHealthComponent* AVampireCharacter::GetHealthComponent()

View File

@ -3,14 +3,16 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "Inputable.h"
#include "GameFramework/Character.h" #include "GameFramework/Character.h"
#include "VampireCharacter.generated.h" #include "VampireCharacter.generated.h"
class UInputAction;
class UHealthComponent; class UHealthComponent;
class UPaperFlipbookComponent; class UPaperFlipbookComponent;
UCLASS() UCLASS(Abstract)
class VAMPIRES_API AVampireCharacter : public ACharacter class VAMPIRES_API AVampireCharacter : public ACharacter, public IInputable
{ {
GENERATED_BODY() GENERATED_BODY()
@ -22,6 +24,9 @@ protected:
UPROPERTY() UPROPERTY()
UHealthComponent* HealthComponent; UHealthComponent* HealthComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UInputMappingContext> InputMappingContext;
public: public:
// Sets default values for this character's properties // Sets default values for this character's properties
AVampireCharacter(); AVampireCharacter();
@ -34,8 +39,9 @@ public:
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input virtual void Input_Move_Implementation(FVector2D value) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
virtual UInputMappingContext* Input_GetInputMappingContext_Implementation() override;
UHealthComponent* GetHealthComponent(); UHealthComponent* GetHealthComponent();
}; };

View File

@ -3,7 +3,10 @@
#include "VampirePlayerController.h" #include "VampirePlayerController.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EXPComponent.h" #include "EXPComponent.h"
#include "Inputable.h"
#include "VampireGameMode.h" #include "VampireGameMode.h"
#include "Blueprint/UserWidget.h" #include "Blueprint/UserWidget.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
@ -13,6 +16,14 @@ void AVampirePlayerController::OnPossess(APawn* aPawn)
{ {
Super::OnPossess(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) if (PlayerHUD)
{ {
currentPlayerHUD = CreateWidget<UHUDWidget, AVampirePlayerController*>(this, PlayerHUD.Get()); currentPlayerHUD = CreateWidget<UHUDWidget, AVampirePlayerController*>(this, PlayerHUD.Get());
@ -47,6 +58,29 @@ void AVampirePlayerController::OnUnPossess()
GetWorld()->GetTimerManager().ClearTimer(pawnLifeTimeHandle); 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) void AVampirePlayerController::UpdatePlayerEXPHUD(int exp, float currentLevelPercent)
{ {
if (currentPlayerHUD) if (currentPlayerHUD)

View File

@ -6,6 +6,7 @@
#include "GameFramework/PlayerController.h" #include "GameFramework/PlayerController.h"
#include "VampirePlayerController.generated.h" #include "VampirePlayerController.generated.h"
class UInputAction;
class UHUDWidget; class UHUDWidget;
/** /**
* *
@ -20,6 +21,11 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<UHUDWidget> PlayerHUD = nullptr; TSubclassOf<UHUDWidget> PlayerHUD = nullptr;
// Inputs
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* MovementAction;
private: private:
TObjectPtr<UHUDWidget> currentPlayerHUD = nullptr; TObjectPtr<UHUDWidget> currentPlayerHUD = nullptr;
@ -31,6 +37,11 @@ protected:
virtual void OnUnPossess() override; virtual void OnUnPossess() override;
virtual void SetupInputComponent() override;
UFUNCTION()
void Move(const FInputActionValue& MovementInput);
UFUNCTION() UFUNCTION()
void UpdatePlayerEXPHUD(int exp, float currentLevelPercent); void UpdatePlayerEXPHUD(int exp, float currentLevelPercent);