Initialise le contrôleur du joueur avec une caméra orthographique et la gestion des inputs, et initialise le pawn du joueur avec un mesh par défaut. Ceci permet d'avoir un setup de base fonctionnel pour le joueur.
117 lines
3.3 KiB
C++
117 lines
3.3 KiB
C++
// 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()
|
|
{
|
|
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
|
|
CameraComponent->ProjectionMode = ECameraProjectionMode::Orthographic;
|
|
CameraComponent->OrthoWidth = 2048.0f;
|
|
CameraComponent->SetAspectRatio(160.0f/192.0f);
|
|
|
|
// attach the camera to the controller's root component
|
|
CameraComponent->SetupAttachment(GetRootComponent());
|
|
|
|
}
|
|
|
|
void AM4_PlayerController::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
if (CameraComponent)
|
|
{
|
|
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<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
|
|
{
|
|
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())
|
|
{
|
|
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<UEnhancedInputComponent>(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<FVector2D>();
|
|
|
|
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));
|
|
}
|
|
} |