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

Binary file not shown.

Binary file not shown.

View File

@@ -1,8 +1,7 @@
// Fill out your copyright notice in the Description page of Project Settings. // Fill out your copyright notice in the Description page of Project Settings.
#include "M4_PlayerController.h" #include "M4_PlayerController.h"
#include "EnhancedInputComponent.h"
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
AM4_PlayerController::AM4_PlayerController() AM4_PlayerController::AM4_PlayerController()
@@ -10,7 +9,11 @@ AM4_PlayerController::AM4_PlayerController()
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent")); CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->ProjectionMode = ECameraProjectionMode::Orthographic; CameraComponent->ProjectionMode = ECameraProjectionMode::Orthographic;
CameraComponent->OrthoWidth = 2048.0f; 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() void AM4_PlayerController::BeginPlay()
@@ -21,15 +24,94 @@ void AM4_PlayerController::BeginPlay()
{ {
CameraComponent->SetRelativeLocation(FVector(0, 0, 1000)); CameraComponent->SetRelativeLocation(FVector(0, 0, 1000));
CameraComponent->SetRelativeRotation(FRotator(-90.0f, 0.0f, 0.0f)); 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() void AM4_PlayerController::SetupInputComponent()
{ {
Super::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) void AM4_PlayerController::OnPossess(APawn* InPawn)
{ {
Super::OnPossess(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));
}
} }

View File

@@ -1,6 +1,4 @@
// Fill out your copyright notice in the Description page of Project Settings. // Fill out your copyright notice in the Description page of Project Settings.
#include "M4_PlayerPawn.h" #include "M4_PlayerPawn.h"
AM4_PlayerPawn::AM4_PlayerPawn() AM4_PlayerPawn::AM4_PlayerPawn()
@@ -14,46 +12,37 @@ AM4_PlayerPawn::AM4_PlayerPawn()
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent")); MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
MeshComponent->SetMobility(EComponentMobility::Movable);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName); MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
MeshComponent->SetRelativeScale3D(FVector(1.0f, 1.0f, 1.0f)); MeshComponent->SetRelativeScale3D(FVector(1.0f, 1.0f, 1.0f));
MeshComponent->SetupAttachment(RootComponent); MeshComponent->SetupAttachment(RootComponent);
LoadedMesh = DefaultMesh.LoadSynchronous();
if (LoadedMesh)
{
PRINT_SCREEN(TEXT("DefaultMesh loaded"), FColor::Green);
} }
else
{
PRINT_SCREEN(TEXT("Failed to load DefaultMesh"), FColor::Red);
}
}
void AM4_PlayerPawn::BeginPlay() void AM4_PlayerPawn::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
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);
} }
void AM4_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
if (MoveAction)
{
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AM4_PlayerPawn::Move);
}
}
}
void AM4_PlayerPawn::Move(const FInputActionValue& Value)
{
// Récupérer la valeur du mouvement (vecteur 2D pour horizontal/vertical)
FVector2D MovementVector = Value.Get<FVector2D>();
// Appliquer le mouvement
if (Controller)
{
FVector NewLocation = GetActorLocation();
NewLocation.X += MovementVector.X;
NewLocation.Y += MovementVector.Y;
SetActorLocation(NewLocation);
}
}

View File

@@ -0,0 +1,8 @@
#pragma once
#include "Engine/EngineTypes.h"
#define C_FOLDER TEXT("/Game/CTP/")
#define C_INPUT_FOLDER TEXT("/Game/CTP/03_Input/")
#define C_MESH_FOLDER TEXT("Game/CTP/04_Mesh/")
#define PRINT_SCREEN(Text, Color) if(GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, Color, Text);

View File

@@ -1,10 +1,21 @@
// Fill out your copyright notice in the Description page of Project Settings. // 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 "Camera/CameraComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "M4_PlayerController.generated.h" #include "M4_PlayerController.generated.h"
/** /**
@@ -20,6 +31,14 @@ public:
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; virtual void SetupInputComponent() override;
virtual void OnPossess(APawn* InPawn) override; virtual void OnPossess(APawn* InPawn) override;
@@ -28,4 +47,6 @@ private:
UPROPERTY(VisibleAnywhere,BlueprintReadOnly ,Category="Centipede", meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere,BlueprintReadOnly ,Category="Centipede", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UCameraComponent> CameraComponent; TObjectPtr<UCameraComponent> CameraComponent;
void Move(const FInputActionValue& Value);
}; };

View File

@@ -1,11 +1,18 @@
// Fill out your copyright notice in the Description page of Project Settings. // 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 "InputActionValue.h"
#include "InputMappingContext.h"
#include "Components/StaticMeshComponent.h" #include "Components/StaticMeshComponent.h"
#include "EnhancedInputComponent.h" #include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h" #include "EnhancedInputSubsystems.h"
@@ -26,21 +33,19 @@ public :
UStaticMeshComponent* GetMeshComponent() const { return MeshComponent; } UStaticMeshComponent* GetMeshComponent() const { return MeshComponent; }
virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere, Category="centipede")
TObjectPtr<UInputAction> MoveAction;
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
private: private:
UPROPERTY(Category = "Centipede", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) UPROPERTY(EditAnywhere,Category = "Centipede")
TObjectPtr<UStaticMeshComponent> MeshComponent; TObjectPtr<UStaticMeshComponent> MeshComponent;
void Move(const FInputActionValue& Value); UPROPERTY(EditAnywhere, Category = "Centipede")
TSoftObjectPtr<UStaticMesh> DefaultMesh = \
TSoftObjectPtr<UStaticMesh>(FSoftObjectPath(FString(C_MESH_FOLDER) + MESH_DEFAULT));
UStaticMesh* LoadedMesh;
}; };