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:
Binary file not shown.
Binary file not shown.
@@ -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()))
|
||||
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer))
|
||||
{
|
||||
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())
|
||||
if (DefaultMappingContext)
|
||||
{
|
||||
PRINT_SCREEN(TEXT("No Default Mapping Context found!"), FColor::Red);
|
||||
return;
|
||||
}
|
||||
PRINT_SCREEN(TEXT("Default Mapping Context loaded"), FColor::Green);
|
||||
}
|
||||
Subsystem->AddMappingContext(DefaultMappingContext, 0);
|
||||
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, TEXT("Mapping Context added successfully"));
|
||||
|
||||
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;
|
||||
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Failed to load Mapping Context"));
|
||||
}
|
||||
|
||||
// Bind the Move Action
|
||||
UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent);
|
||||
if (EnhancedInputComponent)
|
||||
}
|
||||
else
|
||||
{
|
||||
EnhancedInputComponent->BindAction(MoveActionLoaded, ETriggerEvent::Triggered, this, &AM4_PlayerController::Move);
|
||||
PRINT_SCREEN(TEXT("Move Action bound"), FColor::Green);
|
||||
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("No Enhanced Input Subsystem"));
|
||||
}
|
||||
}
|
||||
|
||||
void AM4_PlayerController::Move(const FInputActionValue& Value)
|
||||
else
|
||||
{
|
||||
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);
|
||||
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("No Local Player"));
|
||||
}
|
||||
}
|
||||
|
||||
void AM4_PlayerController::OnPossess(APawn* InPawn)
|
||||
{
|
||||
Super::OnPossess(InPawn);
|
||||
}
|
||||
@@ -1,22 +1,23 @@
|
||||
// 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;
|
||||
|
||||
AutoPossessPlayer = EAutoReceiveInput::Player0;
|
||||
SpawnCollisionHandlingMethod = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
|
||||
|
||||
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, 1.0f, 1.0f));
|
||||
MeshComponent->SetupAttachment(RootComponent);
|
||||
MeshComponent->SetRelativeScale3D(FVector(1.0f));
|
||||
|
||||
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
|
||||
CameraComponent->ProjectionMode = ECameraProjectionMode::Orthographic;
|
||||
@@ -26,31 +27,50 @@ AM4_PlayerPawn::AM4_PlayerPawn()
|
||||
CameraComponent->SetRelativeLocation(FVector(0,0,1000));
|
||||
CameraComponent->SetRelativeRotation(FRotator(-90.0f, 0.0f, 0.0f));
|
||||
|
||||
LoadedMesh = DefaultMesh.LoadSynchronous();
|
||||
if (LoadedMesh)
|
||||
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
|
||||
if (DefaultMeshRef.Succeeded())
|
||||
{
|
||||
PRINT_SCREEN(TEXT("DefaultMesh loaded"), FColor::Green);
|
||||
}
|
||||
else
|
||||
{
|
||||
PRINT_SCREEN(TEXT("Failed to load DefaultMesh"), FColor::Red);
|
||||
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);
|
||||
}
|
||||
SetActorLocation(FVector(0.0f, 0.0f, 10.0f));
|
||||
}
|
||||
|
||||
void AM4_PlayerPawn::Tick(float 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>();
|
||||
}
|
||||
@@ -5,4 +5,9 @@
|
||||
#define C_INPUT_FOLDER TEXT("/Game/CTP/03_Input/")
|
||||
#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);
|
||||
@@ -1,50 +1,20 @@
|
||||
// Fill out your copyright notice in the Description page of Project Settings.
|
||||
|
||||
#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
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "InputAction.h"
|
||||
#include "InputMappingContext.h"
|
||||
#include "GameFramework/PlayerController.h"
|
||||
#include "Camera/CameraComponent.h"
|
||||
#include "EnhancedInputComponent.h"
|
||||
#include "EnhancedInputSubsystems.h"
|
||||
#include "InputMappingContext.h"
|
||||
#include "M4_PlayerController.generated.h"
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
UCLASS()
|
||||
class M4_CPP_API AM4_PlayerController : public APlayerController
|
||||
class AM4_PlayerController : public APlayerController
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
AM4_PlayerController();
|
||||
|
||||
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:
|
||||
virtual void SetupInputComponent() override;
|
||||
virtual void OnPossess(APawn* InPawn) override;
|
||||
|
||||
private:
|
||||
|
||||
void Move(const FInputActionValue& Value);
|
||||
|
||||
UPROPERTY()
|
||||
UInputMappingContext* DefaultMappingContext;
|
||||
};
|
||||
@@ -1,55 +1,35 @@
|
||||
// Fill out your copyright notice in the Description page of Project Settings.
|
||||
|
||||
#pragma once
|
||||
// Include General Macros
|
||||
#include "M4_CTP_Macros.h"
|
||||
|
||||
#define MESH_DEFAULT TEXT("SM_Cube.SM_Cube")
|
||||
|
||||
// Include necessary Unreal Engine headers
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/Pawn.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"
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class UCameraComponent;
|
||||
|
||||
UCLASS()
|
||||
class M4_CPP_API AM4_PlayerPawn : public APawn
|
||||
class AM4_PlayerPawn : public APawn
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
AM4_PlayerPawn();
|
||||
|
||||
virtual void BeginPlay() override;
|
||||
virtual void Tick(float DeltaTime) override;
|
||||
|
||||
UStaticMeshComponent* GetMeshComponent() const { return MeshComponent; }
|
||||
|
||||
virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;
|
||||
void Move(const FInputActionInstance& Instance);
|
||||
|
||||
protected:
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
private:
|
||||
|
||||
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)
|
||||
UStaticMeshComponent* MeshComponent;
|
||||
|
||||
UPROPERTY(VisibleAnywhere)
|
||||
UCameraComponent* CameraComponent;
|
||||
|
||||
UPROPERTY()
|
||||
UInputAction* MoveAction;
|
||||
|
||||
float MoveSpeed = 500.f;
|
||||
float LastMoveValue = 0; // Pour stockage du mouvement
|
||||
};
|
||||
Reference in New Issue
Block a user