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

View File

@@ -1,94 +1,42 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "M4_PlayerController.h"
#include "EnhancedInputComponent.h"
#include "M4_PlayerController.h"
#include "EnhancedInputSubsystems.h"
#include "Camera/CameraComponent.h"
AM4_PlayerController::AM4_PlayerController()
{
PrimaryActorTick.bCanEverTick = false;
PrimaryActorTick.bStartWithTickEnabled = false;
static ConstructorHelpers::FObjectFinder<UInputMappingContext> MappingContextRef(TEXT("/Game/CTP/03_Input/IMC_Default.IMC_Default"));
if (MappingContextRef.Succeeded())
{
DefaultMappingContext = MappingContextRef.Object;
}
}
void AM4_PlayerController::BeginPlay()
{
Super::BeginPlay();
if (!GetLocalPlayer())
if (ULocalPlayer* LocalPlayer = GetLocalPlayer())
{
PRINT_SCREEN(TEXT("No Local Player found!"), FColor::Red);
return;
}
if (!ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
PRINT_SCREEN(TEXT("No Enhanced Input Local Player Subsystem found!"), FColor::Red);
return;
}
// load the default mapping ctx
ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer())->AddMappingContext(DefaultMappingContext.LoadSynchronous(), 0);
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);
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer))
{
if (DefaultMappingContext)
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, TEXT("Mapping Context added successfully"));
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Failed to load Mapping Context"));
}
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("No Enhanced Input Subsystem"));
}
}
else
{
PRINT_SCREEN(TEXT("No Move Action found!"), FColor::Red);
return;
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("No Local Player"));
}
// 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()
{
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bStartWithTickEnabled = true;
PrimaryActorTick.bCanEverTick = 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->SetMobility(EComponentMobility::Movable);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
MeshComponent->SetRelativeScale3D(FVector(1.0f, 1.0f, 1.0f));
MeshComponent->SetupAttachment(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));
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));
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<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
if (DefaultMeshRef.Succeeded())
{
MeshComponent->SetStaticMesh(DefaultMeshRef.Object);
}
static ConstructorHelpers::FObjectFinder<UInputAction> MoveActionRef(TEXT("/Game/CTP/03_Input/IA_Move.IA_Move"));
if (MoveActionRef.Succeeded())
{
MoveAction = MoveActionRef.Object;
}
}
void AM4_PlayerPawn::BeginPlay()
{
Super::BeginPlay();
if (MeshComponent && LoadedMesh)
{
PRINT_SCREEN(TEXT("LoadedMesh"), FColor::Green);
MeshComponent->SetStaticMesh(LoadedMesh);
}
Super::BeginPlay();
SetActorLocation(FVector(0.0f, 0.0f, 10.0f));
}
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>();
}