diff --git a/Content/CTP/01_Level/L_Default.umap b/Content/CTP/01_Level/L_Default.umap index e564e01..b92d96f 100644 Binary files a/Content/CTP/01_Level/L_Default.umap and b/Content/CTP/01_Level/L_Default.umap differ diff --git a/Content/CTP/04_Mesh/SM_Cube.uasset b/Content/CTP/04_Mesh/SM_Cube.uasset new file mode 100644 index 0000000..3306ed9 Binary files /dev/null and b/Content/CTP/04_Mesh/SM_Cube.uasset differ diff --git a/Source/M4_CPP/private/M4_PlayerController.cpp b/Source/M4_CPP/private/M4_PlayerController.cpp index 3574487..ec25868 100644 --- a/Source/M4_CPP/private/M4_PlayerController.cpp +++ b/Source/M4_CPP/private/M4_PlayerController.cpp @@ -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(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(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); + } + else + { + PRINT_SCREEN(TEXT("No Move Action found!"), FColor::Red); + return; + } + + // 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); + + 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)); + } } \ 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 a612cfc..6cc2111 100644 --- a/Source/M4_CPP/private/M4_PlayerPawn.cpp +++ b/Source/M4_CPP/private/M4_PlayerPawn.cpp @@ -1,6 +1,4 @@ // Fill out your copyright notice in the Description page of Project Settings. - - #include "M4_PlayerPawn.h" AM4_PlayerPawn::AM4_PlayerPawn() @@ -14,46 +12,37 @@ AM4_PlayerPawn::AM4_PlayerPawn() 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); -} + + 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() { Super::BeginPlay(); - + + if (MeshComponent && LoadedMesh) + { + PRINT_SCREEN(TEXT("LoadedMesh"), FColor::Green); + MeshComponent->SetStaticMesh(LoadedMesh); + } } + void AM4_PlayerPawn::Tick(float DeltaTime) { Super::Tick(DeltaTime); -} - -void AM4_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) -{ - Super::SetupPlayerInputComponent(PlayerInputComponent); - - if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked(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(); - - // Appliquer le mouvement - if (Controller) - { - FVector NewLocation = GetActorLocation(); - NewLocation.X += MovementVector.X; - NewLocation.Y += MovementVector.Y; - SetActorLocation(NewLocation); - } } \ 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 new file mode 100644 index 0000000..9760632 --- /dev/null +++ b/Source/M4_CPP/public/M4_CTP_Macros.h @@ -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); \ 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 134320f..37aaa6c 100644 --- a/Source/M4_CPP/public/M4_PlayerController.h +++ b/Source/M4_CPP/public/M4_PlayerController.h @@ -1,10 +1,21 @@ // 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 #include "CoreMinimal.h" +#include "InputAction.h" +#include "InputMappingContext.h" #include "GameFramework/PlayerController.h" #include "Camera/CameraComponent.h" +#include "EnhancedInputComponent.h" +#include "EnhancedInputSubsystems.h" #include "M4_PlayerController.generated.h" /** @@ -20,6 +31,14 @@ public: 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; @@ -27,5 +46,7 @@ protected: private: UPROPERTY(VisibleAnywhere,BlueprintReadOnly ,Category="Centipede", meta = (AllowPrivateAccess = "true")) TObjectPtr CameraComponent; + + void Move(const FInputActionValue& Value); }; diff --git a/Source/M4_CPP/public/M4_PlayerPawn.h b/Source/M4_CPP/public/M4_PlayerPawn.h index 58e4c70..679ec92 100644 --- a/Source/M4_CPP/public/M4_PlayerPawn.h +++ b/Source/M4_CPP/public/M4_PlayerPawn.h @@ -1,11 +1,18 @@ // 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 #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" @@ -26,21 +33,19 @@ public : UStaticMeshComponent* GetMeshComponent() const { return MeshComponent; } - virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override; - - UPROPERTY(EditAnywhere, Category="centipede") - TObjectPtr MoveAction; protected: virtual void BeginPlay() override; private: - UPROPERTY(Category = "Centipede", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) + UPROPERTY(EditAnywhere,Category = "Centipede") TObjectPtr MeshComponent; - void Move(const FInputActionValue& Value); + UPROPERTY(EditAnywhere, Category = "Centipede") + TSoftObjectPtr DefaultMesh = \ + TSoftObjectPtr(FSoftObjectPath(FString(C_MESH_FOLDER) + MESH_DEFAULT)); - + UStaticMesh* LoadedMesh; };