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:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,5 @@
|
|||||||
#include "M4_PlayerController.h"
|
#include "M4_PlayerController.h"
|
||||||
#include "EnhancedInputSubsystems.h"
|
#include "EnhancedInputSubsystems.h"
|
||||||
#include "Camera/CameraComponent.h"
|
|
||||||
|
|
||||||
AM4_PlayerController::AM4_PlayerController()
|
AM4_PlayerController::AM4_PlayerController()
|
||||||
{
|
{
|
||||||
@@ -9,12 +8,26 @@ AM4_PlayerController::AM4_PlayerController()
|
|||||||
{
|
{
|
||||||
DefaultMappingContext = MappingContextRef.Object;
|
DefaultMappingContext = MappingContextRef.Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AM4_PlayerController::BeginPlay()
|
void AM4_PlayerController::BeginPlay()
|
||||||
{
|
{
|
||||||
Super::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 (ULocalPlayer* LocalPlayer = GetLocalPlayer())
|
||||||
{
|
{
|
||||||
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer))
|
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer))
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "M4_PlayerPawn.h"
|
#include "M4_PlayerPawn.h"
|
||||||
#include "EnhancedInputComponent.h"
|
#include "EnhancedInputComponent.h"
|
||||||
|
#include "M4_Gamemode.h"
|
||||||
#include "GameFramework/PlayerController.h"
|
#include "GameFramework/PlayerController.h"
|
||||||
#include "Camera/CameraComponent.h"
|
#include "Camera/CameraComponent.h"
|
||||||
|
|
||||||
@@ -17,15 +18,8 @@ AM4_PlayerPawn::AM4_PlayerPawn()
|
|||||||
MeshComponent->SetMobility(EComponentMobility::Movable);
|
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));
|
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"));
|
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
|
||||||
if (DefaultMeshRef.Succeeded())
|
if (DefaultMeshRef.Succeeded())
|
||||||
@@ -43,19 +37,22 @@ AM4_PlayerPawn::AM4_PlayerPawn()
|
|||||||
void AM4_PlayerPawn::BeginPlay()
|
void AM4_PlayerPawn::BeginPlay()
|
||||||
{
|
{
|
||||||
Super::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)
|
void AM4_PlayerPawn::Tick(float DeltaTime)
|
||||||
{
|
{
|
||||||
Super::Tick(DeltaTime);
|
Super::Tick(DeltaTime);
|
||||||
|
|
||||||
if (LastMoveValue != 0)
|
if (AM4_Gamemode* GM = Cast<AM4_Gamemode>(GetWorld()->GetAuthGameMode()))
|
||||||
{
|
{
|
||||||
FVector Loc = GetActorLocation();
|
FVector NewLocation = GetActorLocation() + FVector( 0.0f, LastMoveValue.Y, LastMoveValue.X) * MoveSpeed * DeltaTime;
|
||||||
Loc.Y += LastMoveValue * MoveSpeed * DeltaTime;
|
NewLocation.Z = FMath::Clamp(NewLocation.Z, GM->Bounds.Min.X, GM->Bounds.Max.X);
|
||||||
SetActorLocation(Loc);
|
NewLocation.Y = FMath::Clamp(NewLocation.Y, GM->Bounds.Min.Y, GM->Bounds.Max.Y);
|
||||||
|
SetActorLocation(NewLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AM4_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
void AM4_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
||||||
@@ -72,5 +69,5 @@ void AM4_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputCompo
|
|||||||
|
|
||||||
void AM4_PlayerPawn::Move(const FInputActionInstance& Instance)
|
void AM4_PlayerPawn::Move(const FInputActionInstance& Instance)
|
||||||
{
|
{
|
||||||
LastMoveValue = Instance.GetValue().Get<float>();
|
LastMoveValue = Instance.GetValue().Get<FVector2D>().GetSafeNormal();
|
||||||
}
|
}
|
||||||
@@ -20,5 +20,6 @@ public:
|
|||||||
|
|
||||||
virtual void BeginPlay() override;
|
virtual void BeginPlay() override;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere)
|
||||||
|
FBox2D Bounds = FBox2D(FVector2D(-400.0f, -750.0f), FVector2D(-200.0f, 750.0f));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
#include "CoreMinimal.h"
|
#include "CoreMinimal.h"
|
||||||
#include "GameFramework/PlayerController.h"
|
#include "GameFramework/PlayerController.h"
|
||||||
#include "InputMappingContext.h"
|
#include "InputMappingContext.h"
|
||||||
|
#include "Camera/CameraComponent.h"
|
||||||
|
#include "Camera/CameraActor.h"
|
||||||
#include "M4_PlayerController.generated.h"
|
#include "M4_PlayerController.generated.h"
|
||||||
|
|
||||||
UCLASS()
|
UCLASS()
|
||||||
@@ -17,4 +19,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
UPROPERTY()
|
UPROPERTY()
|
||||||
UInputMappingContext* DefaultMappingContext;
|
UInputMappingContext* DefaultMappingContext;
|
||||||
|
|
||||||
|
UPROPERTY()
|
||||||
|
TObjectPtr<ACameraActor> CameraActor;
|
||||||
};
|
};
|
||||||
@@ -24,12 +24,10 @@ protected:
|
|||||||
UPROPERTY(VisibleAnywhere)
|
UPROPERTY(VisibleAnywhere)
|
||||||
UStaticMeshComponent* MeshComponent;
|
UStaticMeshComponent* MeshComponent;
|
||||||
|
|
||||||
UPROPERTY(VisibleAnywhere)
|
|
||||||
UCameraComponent* CameraComponent;
|
|
||||||
|
|
||||||
UPROPERTY()
|
UPROPERTY()
|
||||||
UInputAction* MoveAction;
|
UInputAction* MoveAction;
|
||||||
|
|
||||||
float MoveSpeed = 500.f;
|
float MoveSpeed = 500.f;
|
||||||
float LastMoveValue = 0; // Pour stockage du mouvement
|
FVector2D MeshScale = FVector2D(0.6f, 0.5f);
|
||||||
|
FVector2D LastMoveValue = FVector2D::ZeroVector;
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user