108 lines
4.3 KiB
C++
108 lines
4.3 KiB
C++
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "GSCharacter.h"
|
|
#include "Camera/CameraComponent.h"
|
|
#include "Components/CapsuleComponent.h"
|
|
#include "Components/InputComponent.h"
|
|
#include "GameFramework/CharacterMovementComponent.h"
|
|
#include "GameFramework/Controller.h"
|
|
#include "GameFramework/SpringArmComponent.h"
|
|
#include "EnhancedInputComponent.h"
|
|
#include "EnhancedInputSubsystems.h"
|
|
#include "GSCharacterMovementComponent.h"
|
|
#include "Kismet/KismetMathLibrary.h"
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// AGravityStompCharacter
|
|
|
|
AGSCharacter::AGSCharacter(const FObjectInitializer& ObjectInitializer)
|
|
: Super(ObjectInitializer.SetDefaultSubobjectClass<UGSCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
|
|
{
|
|
// Set size for collision capsule
|
|
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
|
|
|
|
// Don't rotate when the controller rotates. Let that just affect the camera.
|
|
bUseControllerRotationPitch = false;
|
|
bUseControllerRotationYaw = false;
|
|
bUseControllerRotationRoll = false;
|
|
|
|
// Configure character movement
|
|
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
|
|
GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate
|
|
|
|
// Note: For faster iteration times these variables, and many more, can be tweaked in the Character Blueprint
|
|
// instead of recompiling to adjust them
|
|
GetCharacterMovement()->JumpZVelocity = 700.f;
|
|
GetCharacterMovement()->AirControl = 0.35f;
|
|
GetCharacterMovement()->MaxWalkSpeed = 500.f;
|
|
GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
|
|
GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;
|
|
|
|
// 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 = false; // Rotate the arm based on the controller
|
|
|
|
CameraBoom->SetWorldRotation(FVector::ForwardVector.Rotation());
|
|
|
|
// 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
|
|
|
|
// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
|
|
// are set in the derived blueprint asset named ThirdPersonCharacter (to avoid direct content references in C++)
|
|
}
|
|
|
|
|
|
void AGSCharacter::BeginPlay()
|
|
{
|
|
// Call the base class
|
|
Super::BeginPlay();
|
|
|
|
//Add Input Mapping Context
|
|
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
|
|
{
|
|
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
|
|
{
|
|
Subsystem->AddMappingContext(DefaultMappingContext, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// Input
|
|
|
|
void AGSCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
|
|
{
|
|
// Set up action bindings
|
|
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
|
|
{
|
|
//Moving
|
|
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AGSCharacter::Move);
|
|
EnhancedInputComponent->BindAction(ChangeGravityAction, ETriggerEvent::Triggered, this, &AGSCharacter::ChangeGravityDirection);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void AGSCharacter::Move(const FInputActionValue& Value)
|
|
{
|
|
// input is a Vector2D
|
|
FVector2D MovementVector = Value.Get<FVector2D>();
|
|
|
|
// add movement
|
|
AddMovementInput(FVector::UpVector, MovementVector.Y);
|
|
AddMovementInput(FVector::RightVector, MovementVector.X);
|
|
}
|
|
|
|
void AGSCharacter::ChangeGravityDirection(const FInputActionValue& Value)
|
|
{
|
|
FVector2D GravityDirection = Value.Get<FVector2D>();
|
|
FVector NewCharacterUpDirection(0.0f, -GravityDirection.X, -GravityDirection.Y);
|
|
|
|
GetCharacterMovement<UGSCharacterMovementComponent>()->SetCharacterUpDirection(NewCharacterUpDirection);
|
|
}
|