diff --git a/Content/CTP/03_Input/IA_Move.uasset b/Content/CTP/03_Input/IA_Move.uasset index 418a3d8..ce1bed8 100644 Binary files a/Content/CTP/03_Input/IA_Move.uasset and b/Content/CTP/03_Input/IA_Move.uasset differ diff --git a/Content/CTP/03_Input/IMC_Default.uasset b/Content/CTP/03_Input/IMC_Default.uasset index 2187f41..adc6db4 100644 Binary files a/Content/CTP/03_Input/IMC_Default.uasset and b/Content/CTP/03_Input/IMC_Default.uasset differ diff --git a/Source/M4_CPP/private/M4_PlayerController.cpp b/Source/M4_CPP/private/M4_PlayerController.cpp index 60b7de5..e7004b6 100644 --- a/Source/M4_CPP/private/M4_PlayerController.cpp +++ b/Source/M4_CPP/private/M4_PlayerController.cpp @@ -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 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(GetLocalPlayer())) - { - PRINT_SCREEN(TEXT("No Enhanced Input Local Player Subsystem found!"), FColor::Red); - return; - } - - // load the default mapping ctx - ULocalPlayer::GetSubsystem(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(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(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(); - - 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); } \ No newline at end of file diff --git a/Source/M4_CPP/private/M4_PlayerPawn.cpp b/Source/M4_CPP/private/M4_PlayerPawn.cpp index f3722ff..d98f178 100644 --- a/Source/M4_CPP/private/M4_PlayerPawn.cpp +++ b/Source/M4_CPP/private/M4_PlayerPawn.cpp @@ -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(TEXT("RootComponent")); - RootComponent = CreateDefaultSubobject(TEXT("RootComponent")); - - MeshComponent = CreateDefaultSubobject(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(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(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(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 DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube")); + if (DefaultMeshRef.Succeeded()) + { + MeshComponent->SetStaticMesh(DefaultMeshRef.Object); + } + + static ConstructorHelpers::FObjectFinder 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(PlayerInputComponent)) + { + if (MoveAction) + { + Input->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AM4_PlayerPawn::Move); + } + } +} + +void AM4_PlayerPawn::Move(const FInputActionInstance& Instance) +{ + LastMoveValue = Instance.GetValue().Get(); } \ No newline at end of file diff --git a/Source/M4_CPP/public/M4_CTP_Macros.h b/Source/M4_CPP/public/M4_CTP_Macros.h index 68a03f6..157a41a 100644 --- a/Source/M4_CPP/public/M4_CTP_Macros.h +++ b/Source/M4_CPP/public/M4_CTP_Macros.h @@ -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); \ No newline at end of file diff --git a/Source/M4_CPP/public/M4_PlayerController.h b/Source/M4_CPP/public/M4_PlayerController.h index 3063c4e..0ecedd8 100644 --- a/Source/M4_CPP/public/M4_PlayerController.h +++ b/Source/M4_CPP/public/M4_PlayerController.h @@ -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 DefaultMappingContext = \ - TSoftObjectPtr(FSoftObjectPath(FString(C_INPUT_FOLDER) + IMC_DEFAULT)); - - UPROPERTY(EditAnywhere) - TSoftObjectPtr MoveAction = \ - TSoftObjectPtr(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; +}; \ No newline at end of file diff --git a/Source/M4_CPP/public/M4_PlayerPawn.h b/Source/M4_CPP/public/M4_PlayerPawn.h index b2b34a9..cbb898d 100644 --- a/Source/M4_CPP/public/M4_PlayerPawn.h +++ b/Source/M4_CPP/public/M4_PlayerPawn.h @@ -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 : +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 MeshComponent; - - UPROPERTY(EditAnywhere, Category = "Centipede") - TSoftObjectPtr DefaultMesh = \ - TSoftObjectPtr(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 +}; \ No newline at end of file