Minor - Implémente le système d'input - V01.05.00

Initialise la gestion des inputs avec le système Enhanced Input.
Ajoute un PlayerController et un PlayerPawn de base.
Définit les actions et mappings par défaut pour le mouvement.
This commit is contained in:
2025-10-15 17:56:07 +02:00
parent f1cbe8c8c3
commit cb7516abb8
7 changed files with 115 additions and 192 deletions

Binary file not shown.

View File

@@ -1,94 +1,42 @@
// Fill out your copyright notice in the Description page of Project Settings. #include "M4_PlayerController.h"
#include "M4_PlayerController.h" #include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
AM4_PlayerController::AM4_PlayerController() AM4_PlayerController::AM4_PlayerController()
{ {
PrimaryActorTick.bCanEverTick = false; static ConstructorHelpers::FObjectFinder<UInputMappingContext> MappingContextRef(TEXT("/Game/CTP/03_Input/IMC_Default.IMC_Default"));
PrimaryActorTick.bStartWithTickEnabled = false; if (MappingContextRef.Succeeded())
{
DefaultMappingContext = MappingContextRef.Object;
}
} }
void AM4_PlayerController::BeginPlay() void AM4_PlayerController::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
if (ULocalPlayer* LocalPlayer = GetLocalPlayer())
if (!GetLocalPlayer())
{ {
PRINT_SCREEN(TEXT("No Local Player found!"), FColor::Red); if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer))
return; {
} if (DefaultMappingContext)
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, TEXT("Mapping Context added successfully"));
if (!ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer())) }
{ else
PRINT_SCREEN(TEXT("No Enhanced Input Local Player Subsystem found!"), FColor::Red); {
return; GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Failed to load Mapping Context"));
} }
}
// load the default mapping ctx else
ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer())->AddMappingContext(DefaultMappingContext.LoadSynchronous(), 0); {
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("No Enhanced Input Subsystem"));
if (!DefaultMappingContext.IsValid()) }
{
PRINT_SCREEN(TEXT("No Default Mapping Context found!"), FColor::Red);
return;
}
PRINT_SCREEN(TEXT("Default Mapping Context loaded"), FColor::Green);
}
void AM4_PlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
if (!InputComponent)
{
PRINT_SCREEN(TEXT("No Input Component found!"), FColor::Red);
return;
}
if (!InputComponent->IsA(UEnhancedInputComponent::StaticClass()))
{
PRINT_SCREEN(TEXT("Input Component is not an Enhanced Input Component!"), FColor::Red);
return;
}
// load the Move Action
UInputAction* MoveActionLoaded = MoveAction.LoadSynchronous();
if (MoveActionLoaded)
{
PRINT_SCREEN(TEXT("Move Action loaded"), FColor::Green);
} }
else else
{ {
PRINT_SCREEN(TEXT("No Move Action found!"), FColor::Red); GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("No Local Player"));
return;
}
// Bind the Move Action
UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent);
if (EnhancedInputComponent)
{
EnhancedInputComponent->BindAction(MoveActionLoaded, ETriggerEvent::Triggered, this, &AM4_PlayerController::Move);
PRINT_SCREEN(TEXT("Move Action bound"), FColor::Green);
} }
} }
void AM4_PlayerController::Move(const FInputActionValue& Value)
{
GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Green, FString::Printf(TEXT("Move Value: %s"), *Value.ToString()));
FVector2D MovementVector = Value.Get<FVector2D>();
if (APawn* ControlledPawn = GetPawn())
{
FVector NewLocation = ControlledPawn->GetActorLocation() + FVector(MovementVector.X, MovementVector.Y, 0.0f) * 10.0f;
ControlledPawn->SetActorLocation(NewLocation);
}
}
void AM4_PlayerController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
}

View File

@@ -1,56 +1,76 @@
// Fill out your copyright notice in the Description page of Project Settings. #include "M4_PlayerPawn.h"
#include "M4_PlayerPawn.h" #include "EnhancedInputComponent.h"
#include "GameFramework/PlayerController.h"
#include "Camera/CameraComponent.h"
AM4_PlayerPawn::AM4_PlayerPawn() AM4_PlayerPawn::AM4_PlayerPawn()
{ {
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bStartWithTickEnabled = true; PrimaryActorTick.bStartWithTickEnabled = true;
AutoPossessPlayer = EAutoReceiveInput::Player0;
SpawnCollisionHandlingMethod = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
SpawnCollisionHandlingMethod = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent")); MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
MeshComponent->SetupAttachment(RootComponent);
MeshComponent->SetMobility(EComponentMobility::Movable);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
MeshComponent->SetRelativeScale3D(FVector(1.0f));
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent")); CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->ProjectionMode = ECameraProjectionMode::Orthographic;
CameraComponent->OrthoWidth = 2048.0f;
CameraComponent->SetAspectRatio(160.0f/192.0f);
CameraComponent->SetupAttachment(RootComponent);
CameraComponent->SetRelativeLocation(FVector(0,0,1000));
CameraComponent->SetRelativeRotation(FRotator(-90.0f, 0.0f, 0.0f));
MeshComponent->SetMobility(EComponentMobility::Movable); static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); if (DefaultMeshRef.Succeeded())
MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName); {
MeshComponent->SetRelativeScale3D(FVector(1.0f, 1.0f, 1.0f)); MeshComponent->SetStaticMesh(DefaultMeshRef.Object);
MeshComponent->SetupAttachment(RootComponent); }
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->ProjectionMode = ECameraProjectionMode::Orthographic;
CameraComponent->OrthoWidth = 2048.0f;
CameraComponent->SetAspectRatio(160.0f/192.0f);
CameraComponent->SetupAttachment(RootComponent);
CameraComponent->SetRelativeLocation(FVector(0,0,1000));
CameraComponent->SetRelativeRotation(FRotator(-90.0f, 0.0f, 0.0f));
LoadedMesh = DefaultMesh.LoadSynchronous();
if (LoadedMesh)
{
PRINT_SCREEN(TEXT("DefaultMesh loaded"), FColor::Green);
}
else
{
PRINT_SCREEN(TEXT("Failed to load DefaultMesh"), FColor::Red);
}
static ConstructorHelpers::FObjectFinder<UInputAction> MoveActionRef(TEXT("/Game/CTP/03_Input/IA_Move.IA_Move"));
if (MoveActionRef.Succeeded())
{
MoveAction = MoveActionRef.Object;
}
} }
void AM4_PlayerPawn::BeginPlay() void AM4_PlayerPawn::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
SetActorLocation(FVector(0.0f, 0.0f, 10.0f));
if (MeshComponent && LoadedMesh)
{
PRINT_SCREEN(TEXT("LoadedMesh"), FColor::Green);
MeshComponent->SetStaticMesh(LoadedMesh);
}
} }
void AM4_PlayerPawn::Tick(float DeltaTime) void AM4_PlayerPawn::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
if (LastMoveValue != 0)
{
FVector Loc = GetActorLocation();
Loc.Y += LastMoveValue * MoveSpeed * DeltaTime;
SetActorLocation(Loc);
}
}
void AM4_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* Input = Cast<UEnhancedInputComponent>(PlayerInputComponent))
{
if (MoveAction)
{
Input->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AM4_PlayerPawn::Move);
}
}
}
void AM4_PlayerPawn::Move(const FInputActionInstance& Instance)
{
LastMoveValue = Instance.GetValue().Get<float>();
} }

View File

@@ -5,4 +5,9 @@
#define C_INPUT_FOLDER TEXT("/Game/CTP/03_Input/") #define C_INPUT_FOLDER TEXT("/Game/CTP/03_Input/")
#define C_MESH_FOLDER TEXT("/Game/CTP/04_Mesh/") #define C_MESH_FOLDER TEXT("/Game/CTP/04_Mesh/")
#define INPUT_CTX_PATH(AssetName) TEXT("/Game/CTP/03_Input/") TEXT(#AssetName) TEXT(".") TEXT(#AssetName)
#define MESH_PATH(AssetName) TEXT("/Game/CTP/04_Mesh/") TEXT(#AssetName) TEXT(".") TEXT(#AssetName)
#define ASSET_PATH(Path, Asset) TEXT(Path) TEXT(#Asset) TEXT(".") TEXT(#Asset)
#define PRINT_SCREEN(Text, Color) if(GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, Color, Text); #define PRINT_SCREEN(Text, Color) if(GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, Color, Text);

View File

@@ -1,50 +1,20 @@
// Fill out your copyright notice in the Description page of Project Settings. #pragma once
#pragma once
// Include General Macros
#include "M4_CTP_Macros.h"
#define IMC_DEFAULT TEXT("IMC_Default.IMC_Default")
#define IA_MOVE_DEFAULT TEXT("IA_Move.IA_Move")
// Include necessary Unreal Engine headers
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "InputAction.h"
#include "InputMappingContext.h"
#include "GameFramework/PlayerController.h" #include "GameFramework/PlayerController.h"
#include "Camera/CameraComponent.h" #include "InputMappingContext.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "M4_PlayerController.generated.h" #include "M4_PlayerController.generated.h"
/**
*
*/
UCLASS() UCLASS()
class M4_CPP_API AM4_PlayerController : public APlayerController class AM4_PlayerController : public APlayerController
{ {
GENERATED_BODY() GENERATED_BODY()
public: public:
AM4_PlayerController(); AM4_PlayerController();
virtual void BeginPlay() override; virtual void BeginPlay() override;
UPROPERTY(EditAnywhere)
TSoftObjectPtr<UInputMappingContext> DefaultMappingContext = \
TSoftObjectPtr<UInputMappingContext>(FSoftObjectPath(FString(C_INPUT_FOLDER) + IMC_DEFAULT));
UPROPERTY(EditAnywhere)
TSoftObjectPtr<UInputAction> MoveAction = \
TSoftObjectPtr<UInputAction>(FSoftObjectPath(FString(C_INPUT_FOLDER) + IA_MOVE_DEFAULT));
protected: protected:
virtual void SetupInputComponent() override; UPROPERTY()
virtual void OnPossess(APawn* InPawn) override; UInputMappingContext* DefaultMappingContext;
private:
void Move(const FInputActionValue& Value);
}; };

View File

@@ -1,55 +1,35 @@
// Fill out your copyright notice in the Description page of Project Settings. #pragma once
#pragma once
// Include General Macros
#include "M4_CTP_Macros.h"
#define MESH_DEFAULT TEXT("SM_Cube.SM_Cube")
// Include necessary Unreal Engine headers
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Pawn.h" #include "GameFramework/Pawn.h"
#include "InputAction.h" #include "InputAction.h"
#include "InputActionValue.h"
#include "InputMappingContext.h"
#include "Components/StaticMeshComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Camera/CameraComponent.h"
#include "M4_PlayerPawn.generated.h" #include "M4_PlayerPawn.generated.h"
/** class UCameraComponent;
*
*/
UCLASS() UCLASS()
class M4_CPP_API AM4_PlayerPawn : public APawn class AM4_PlayerPawn : public APawn
{ {
GENERATED_BODY() GENERATED_BODY()
public:
public :
AM4_PlayerPawn(); AM4_PlayerPawn();
virtual void BeginPlay() override;
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;
UStaticMeshComponent* GetMeshComponent() const { return MeshComponent; } void Move(const FInputActionInstance& Instance);
protected: protected:
virtual void BeginPlay() override;
private: UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MeshComponent;
UPROPERTY(EditAnywhere,Category = "Centipede")
TObjectPtr<UStaticMeshComponent> MeshComponent;
UPROPERTY(EditAnywhere, Category = "Centipede")
TSoftObjectPtr<UStaticMesh> DefaultMesh = \
TSoftObjectPtr<UStaticMesh>(FSoftObjectPath(FString(C_MESH_FOLDER) + MESH_DEFAULT));
UStaticMesh* LoadedMesh;
UPROPERTY(VisibleAnywhere) UPROPERTY(VisibleAnywhere)
UCameraComponent* CameraComponent; UCameraComponent* CameraComponent;
UPROPERTY()
UInputAction* MoveAction;
float MoveSpeed = 500.f;
float LastMoveValue = 0; // Pour stockage du mouvement
}; };