Minor - Initialise le contrôleur et le pawn - V01.3.0

Initialise le contrôleur du joueur avec une caméra orthographique et la gestion des inputs, et initialise le pawn du joueur avec un mesh par défaut.

Ceci permet d'avoir un setup de base fonctionnel pour le joueur.
This commit is contained in:
2025-10-15 12:48:33 +02:00
parent 34a6fec411
commit 55a9234871
7 changed files with 147 additions and 42 deletions

View File

@@ -1,8 +1,7 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "M4_PlayerController.h"
#include "EnhancedInputComponent.h"
#include "Camera/CameraComponent.h"
AM4_PlayerController::AM4_PlayerController()
@@ -10,7 +9,11 @@ AM4_PlayerController::AM4_PlayerController()
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->ProjectionMode = ECameraProjectionMode::Orthographic;
CameraComponent->OrthoWidth = 2048.0f;
CameraComponent->SetupAttachment(RootComponent);
CameraComponent->SetAspectRatio(160.0f/192.0f);
// attach the camera to the controller's root component
CameraComponent->SetupAttachment(GetRootComponent());
}
void AM4_PlayerController::BeginPlay()
@@ -21,15 +24,94 @@ void AM4_PlayerController::BeginPlay()
{
CameraComponent->SetRelativeLocation(FVector(0, 0, 1000));
CameraComponent->SetRelativeRotation(FRotator(-90.0f, 0.0f, 0.0f));
// Set the camera as the view target
SetViewTarget(this);
}
if (!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);
}
else
{
PRINT_SCREEN(TEXT("No Move Action found!"), FColor::Red);
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);
PRINT_SCREEN(TEXT("PlayerController Possess"), FColor::Green);
if (InPawn && CameraComponent)
{
CameraComponent->AttachToComponent(InPawn->GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
CameraComponent->SetRelativeLocation(FVector(0, 0, 1000));
CameraComponent->SetRelativeRotation(FRotator(-90.0f, 0.0f, 0.0f));
}
}