Minor - Ajoute une caméra orthographique - V01.06.00

Implémente une caméra orthographique pour une vue 2D.

Ajuste le mouvement du joueur pour qu'il soit contraint par les limites du niveau.

Remplace l'ancienne caméra dans le PlayerPawn par une nouvelle dans le PlayerController.
This commit is contained in:
2025-10-16 15:27:44 +02:00
parent cb7516abb8
commit 1797fd1ba6
8 changed files with 35 additions and 21 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -1,6 +1,5 @@
#include "M4_PlayerController.h"
#include "EnhancedInputSubsystems.h"
#include "Camera/CameraComponent.h"
AM4_PlayerController::AM4_PlayerController()
{
@@ -9,12 +8,26 @@ AM4_PlayerController::AM4_PlayerController()
{
DefaultMappingContext = MappingContextRef.Object;
}
}
void AM4_PlayerController::BeginPlay()
{
Super::BeginPlay();
// Spawn a camera actor
FActorSpawnParameters SpawnParameters;
SpawnParameters.Owner = this;
CameraActor = GetWorld()->SpawnActor<ACameraActor>(SpawnParameters);
CameraActor->SetActorLocation(FVector(-500.f, 0.f, 0.f));
CameraActor->GetCameraComponent()->SetProjectionMode(ECameraProjectionMode::Orthographic);
CameraActor->GetCameraComponent()->SetOrthoWidth(1600.f);
CameraActor->GetCameraComponent()->SetWorldLocation(FVector(-500.f, 0.f, 0.f));
CameraActor->GetCameraComponent()->SetAspectRatio(192.0f/160.0f);
SetViewTarget(CameraActor);
if (ULocalPlayer* LocalPlayer = GetLocalPlayer())
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer))

View File

@@ -1,5 +1,6 @@
#include "M4_PlayerPawn.h"
#include "EnhancedInputComponent.h"
#include "M4_Gamemode.h"
#include "GameFramework/PlayerController.h"
#include "Camera/CameraComponent.h"
@@ -17,15 +18,8 @@ AM4_PlayerPawn::AM4_PlayerPawn()
MeshComponent->SetMobility(EComponentMobility::Movable);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
MeshComponent->SetRelativeScale3D(FVector(1.0f));
MeshComponent->SetRelativeScale3D(FVector(1.0f, MeshScale.Y, MeshScale.X));
CameraComponent = CreateDefaultSubobject<UCameraComponent>(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));
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
if (DefaultMeshRef.Succeeded())
@@ -43,19 +37,22 @@ AM4_PlayerPawn::AM4_PlayerPawn()
void AM4_PlayerPawn::BeginPlay()
{
Super::BeginPlay();
SetActorLocation(FVector(0.0f, 0.0f, 10.0f));
SetActorLocation(FVector(0.0f, 0.0f, -400.0f));
}
void AM4_PlayerPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (LastMoveValue != 0)
if (AM4_Gamemode* GM = Cast<AM4_Gamemode>(GetWorld()->GetAuthGameMode()))
{
FVector Loc = GetActorLocation();
Loc.Y += LastMoveValue * MoveSpeed * DeltaTime;
SetActorLocation(Loc);
FVector NewLocation = GetActorLocation() + FVector( 0.0f, LastMoveValue.Y, LastMoveValue.X) * MoveSpeed * DeltaTime;
NewLocation.Z = FMath::Clamp(NewLocation.Z, GM->Bounds.Min.X, GM->Bounds.Max.X);
NewLocation.Y = FMath::Clamp(NewLocation.Y, GM->Bounds.Min.Y, GM->Bounds.Max.Y);
SetActorLocation(NewLocation);
}
}
void AM4_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
@@ -72,5 +69,5 @@ void AM4_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputCompo
void AM4_PlayerPawn::Move(const FInputActionInstance& Instance)
{
LastMoveValue = Instance.GetValue().Get<float>();
LastMoveValue = Instance.GetValue().Get<FVector2D>().GetSafeNormal();
}

View File

@@ -20,5 +20,6 @@ public:
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere)
FBox2D Bounds = FBox2D(FVector2D(-400.0f, -750.0f), FVector2D(-200.0f, 750.0f));
};

View File

@@ -3,6 +3,8 @@
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "InputMappingContext.h"
#include "Camera/CameraComponent.h"
#include "Camera/CameraActor.h"
#include "M4_PlayerController.generated.h"
UCLASS()
@@ -17,4 +19,7 @@ public:
protected:
UPROPERTY()
UInputMappingContext* DefaultMappingContext;
UPROPERTY()
TObjectPtr<ACameraActor> CameraActor;
};

View File

@@ -24,12 +24,10 @@ protected:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MeshComponent;
UPROPERTY(VisibleAnywhere)
UCameraComponent* CameraComponent;
UPROPERTY()
UInputAction* MoveAction;
float MoveSpeed = 500.f;
float LastMoveValue = 0; // Pour stockage du mouvement
FVector2D MeshScale = FVector2D(0.6f, 0.5f);
FVector2D LastMoveValue = FVector2D::ZeroVector;
};