Resolve #6: Migrate LumiCharacter to C++ #7

Merged
lance1416 merged 8 commits from 6-migrate-lumi-character-to-cpp into main 2024-09-10 20:23:45 +08:00
8 changed files with 368 additions and 17 deletions
Showing only changes of commit 09a4118e37 - Show all commits
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -3,17 +3,67 @@
#include "LumiCharacter.h"
#include "Engine/LocalPlayer.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/Controller.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "InputActionValue.h"
#include "GameFramework/CharacterMovementComponent.h"
DEFINE_LOG_CATEGORY(LogLumiCharacter);
// Sets default values
ALumiCharacter::ALumiCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
// PrimaryActorTick.bCanEverTick = true;
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(35.0f, 90.0f);
// Allow double jumping
JumpMaxCount = 2;
// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 400.0f; // The camera follows at this distance behind the character
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
// Create a follow camera
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
// Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
}
ELumiStance ALumiCharacter::GetCurrentStance() const
{
return CurrentStance;
}
// Called when the game starts or when spawned
void ALumiCharacter::BeginPlay()
{
Super::BeginPlay();
// Add Input Mapping Context
if (const APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<
UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
/*
@@ -24,8 +74,153 @@ void ALumiCharacter::Tick(float DeltaTime)
}
*/
bool ALumiCharacter::IsLastJump()
{
return JumpCurrentCountPreJump == JumpMaxCount - 1;
}
// Called to bind functionality to input
void ALumiCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
{
// Grabbing
EnhancedInputComponent->BindAction(GrabAction,
ETriggerEvent::Triggered,
this,
&ALumiCharacter::OnGrabActionTriggered);
// Jumping
EnhancedInputComponent->BindAction(JumpAction,
ETriggerEvent::Started,
this,
&ALumiCharacter::OnJumpActionStarted);
EnhancedInputComponent->BindAction(JumpAction,
ETriggerEvent::Ongoing,
this,
&ALumiCharacter::OnJumpActionOngoing);
EnhancedInputComponent->BindAction(JumpAction,
ETriggerEvent::Completed,
this,
&ALumiCharacter::OnJumpActionCompleted);
// Moving
EnhancedInputComponent->BindAction(MoveAction,
ETriggerEvent::Triggered,
this,
&ALumiCharacter::OnMoveActionTriggered);
// Looking
EnhancedInputComponent->BindAction(LookAction,
ETriggerEvent::Triggered,
this,
&ALumiCharacter::OnLookActionTriggered);
}
else
{
UE_LOG(
LogLumiCharacter,
Error,
TEXT(
"'%s' Failed to find an Enhanced Input component! "
"This template is built to use the Enhanced Input system. "
"If you intend to use the legacy system, then you will need to update this C++ file."
),
*GetNameSafe(this)
);
}
}
void ALumiCharacter::OnGrabActionTriggered(const FInputActionValue& Value)
{
UE_LOG(LogLumiCharacter, Log, TEXT("Grab Triggered"));
}
void ALumiCharacter::OnJumpActionStarted(const FInputActionValue& Value)
{
// Broadcast that the jump has started
OnJumpStarted.Broadcast();
UE_LOG(LogLumiCharacter, Log, TEXT("Jump Started"));
Jump();
}
void ALumiCharacter::OnJumpActionOngoing(const FInputActionValue& Value)
{
}
void ALumiCharacter::OnJumpActionCompleted(const FInputActionValue& Value)
{
StopJumping();
}
void ALumiCharacter::OnMoveActionTriggered(const FInputActionValue& Value)
{
// input is a Vector2D
const FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
// get right vector
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void ALumiCharacter::OnLookActionTriggered(const FInputActionValue& Value)
{
// input is a Vector2D
const FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// add yaw and pitch input to controller
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
void ALumiCharacter::OnDefaultStance_Implementation()
{
UE_LOG(LogLumiCharacter, Log, TEXT("OnDefaultStance_Implementation()"));
}
void ALumiCharacter::OnMagicStance_Implementation()
{
UE_LOG(LogLumiCharacter, Log, TEXT("OnMagicStance_Implementation()"));
}
ELumiStance ALumiCharacter::SetCurrentStance(const ELumiStance NewStance)
{
const ELumiStance OldStance = CurrentStance;
UCharacterMovementComponent* Movement = GetCharacterMovement();
if (NewStance == ELumiStance::Default)
{
Movement->bOrientRotationToMovement = true;
Movement->bUseControllerDesiredRotation = false;
OnDefaultStance();
}
else if (NewStance == ELumiStance::Magic)
{
Movement->bOrientRotationToMovement = false;
Movement->bUseControllerDesiredRotation = true;
OnMagicStance();
}
CurrentStance = NewStance;
return OldStance;
}
@@ -3,26 +3,168 @@
#pragma once
#include "CoreMinimal.h"
#include "LumiStance.h"
#include "GameFramework/Character.h"
#include "Logging/LogMacros.h"
#include "LumiCharacter.generated.h"
class USpringArmComponent;
class UCameraComponent;
class UInputMappingContext;
class UInputAction;
struct FInputActionValue;
DECLARE_LOG_CATEGORY_EXTERN(LogLumiCharacter, Log, All);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FJumpDelegate);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FStanceChangedDelegate,
ELumiStance, OldStance,
ELumiStance, NewStance);
static const FString PlayerStanceEnumPath = TEXT("/Game/Player/E_PlayerStance");
UCLASS()
class WIZARDINGCENTRAL_API ALumiCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
ALumiCharacter();
// Private UPROPERTIE'S --------------------------------------------------------------------------------------------
/** Camera boom positioning the camera behind the character */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera, meta = (AllowPrivateAccess = "true"))
USpringArmComponent* CameraBoom;
/** Follow camera */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Camera, meta = (AllowPrivateAccess = "true"))
UCameraComponent* FollowCamera;
/** MappingContext */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputMappingContext* DefaultMappingContext;
/** Grab Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* GrabAction;
/** Jump Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* JumpAction;
/** Move Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* MoveAction;
/** Look Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* LookAction;
/** The current stance of the character */
UPROPERTY(BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
ELumiStance CurrentStance = ELumiStance::Default;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// UFUNCTION's -----------------------------------------------------------------------------------------------------
/**
* Set the stance of the character
*
* @param NewStance The new stance to set
* @return The stance before the new stance was set
*/
UFUNCTION(BlueprintCallable, Category = "Stance")
ELumiStance SetCurrentStance(ELumiStance NewStance);
/**
* Event called when the character switches to the default stance
*/
UFUNCTION(BlueprintNativeEvent, Category = "Stance")
void OnDefaultStance();
/**
* Event called when the character switches to the magic stance
*/
UFUNCTION(BlueprintNativeEvent, Category = "Stance")
void OnMagicStance();
public:
UFUNCTION(BlueprintPure, Category = "LumiCharacter")
bool IsLastJump();
public:
// Public Virtual Functions ----------------------------------------------------------------------------------------
// Called every frame
// virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
/**
* Called to bind functionality to input
* @param PlayerInputComponent The input component to bind to
*/
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
protected:
// Protected Virtual Functions -------------------------------------------------------------------------------------
/**
* Called when the game starts or when spawned
*/
virtual void BeginPlay() override;
public:
// Public Functions ------------------------------------------------------------------------------------------------
/**
* Sets default values for this character's properties
*/
ALumiCharacter();
/**
* Getter for @property CurrentStance
*
* @return The current stance of the character
*/
ELumiStance GetCurrentStance() const;
private:
// Private Functions -----------------------------------------------------------------------------------------------
// Grab
/**
* Callback for when the grab action is triggered
* @param Value The value of the input action
*/
void OnGrabActionTriggered(const FInputActionValue& Value);
// Jump
/**
* Callback for when the jump action is started
* @param Value The value of the input action
*/
void OnJumpActionStarted(const FInputActionValue& Value);
/**
* Callback for when the jump action is ongoing
* @param Value The value of the input action
*/
void OnJumpActionOngoing(const FInputActionValue& Value);
/**
* Callback for when the jump action is completed
* @param Value The value of the input action
*/
void OnJumpActionCompleted(const FInputActionValue& Value);
// Move
/**
* Callback for when the move action is triggered
* @param Value The value of the input action
*/
void OnMoveActionTriggered(const FInputActionValue& Value);
// Look
/**
* Callback for when the look action is triggered
* @param Value The value of the input action
*/
void OnLookActionTriggered(const FInputActionValue& Value);
public:
// Delegates -------------------------------------------------------------------------------------------------------
UPROPERTY(BlueprintAssignable, Category = "Input")
FJumpDelegate OnJumpStarted;
UPROPERTY(BlueprintAssignable, Category = "Stance")
FStanceChangedDelegate OnStanceChanged;
};
@@ -0,0 +1,14 @@
// Copyright Team Lumi. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "LumiStance.generated.h"
UENUM(BlueprintType)
enum class ELumiStance : uint8
{
Default UMETA(DisplayName = "Default Stance"),
Magic UMETA(DisplayName = "Magic Stance"),
};