269 lines
8.5 KiB
C++
269 lines
8.5 KiB
C++
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "hwanyoung2Character.h"
|
|
#include "HYPlayerCharacController.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 "HYInteractableActor.h"
|
|
#include "InventoryItem.h"
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// Ahwanyoung2Character
|
|
Ahwanyoung2Character::Ahwanyoung2Character()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
|
|
|
|
void Ahwanyoung2Character::PossessedBy(AController* NewController)
|
|
{
|
|
Super::PossessedBy(NewController);
|
|
|
|
}
|
|
|
|
float Ahwanyoung2Character::GetStartingCameraBoomArmLength()
|
|
{
|
|
return 0.0f;
|
|
}
|
|
|
|
FVector Ahwanyoung2Character::GetStartingCameraBoomLocation()
|
|
{
|
|
return FVector();
|
|
}
|
|
|
|
void Ahwanyoung2Character::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(InputMapping, 0);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
void Ahwanyoung2Character::CheckForInteractables()
|
|
{
|
|
// Creates a LineTrace (similar to RayCast) to check for a hit
|
|
FHitResult HitResult;
|
|
|
|
// The range of area the system should check for interactables
|
|
int32 Range = 800; //this can be changed
|
|
|
|
//The start of the trace is the transform of the follow camera
|
|
FVector StartTrace = FollowCamera->GetComponentLocation();
|
|
|
|
//And the end of the trace is the 500 units ahead of the start trace
|
|
FVector EndTrace = (FollowCamera->GetForwardVector() * Range) + StartTrace;
|
|
|
|
//Keeps track of parameters passed into collision function, assuming that
|
|
//there are multiple collided actors that is passed through the collision function
|
|
FCollisionQueryParams QueryParams;
|
|
QueryParams.AddIgnoredActor(this); //we are ignoring this, which is the character
|
|
|
|
//Similarly to how GetComponent<>() function works, we get the controller
|
|
//object attached to the character and cast it as player character controller class
|
|
AHYPlayerCharacController* IController = Cast<AHYPlayerCharacController>(GetController());
|
|
|
|
|
|
//Checking if something is hit by the line cast within the range
|
|
if (GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace,
|
|
ECC_Visibility, QueryParams)) {
|
|
|
|
//Cast the actor to AInteractable
|
|
if (AHYInteractableActor* Interactable = Cast<AHYInteractableActor>(HitResult.GetActor())) {
|
|
IController->CurrentInteractable = Interactable;
|
|
return;
|
|
}
|
|
} else {
|
|
IController->CurrentInteractable = nullptr;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
void Ahwanyoung2Character::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
CheckForInteractables();
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// Input
|
|
|
|
void Ahwanyoung2Character::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
|
|
{
|
|
// Set up action bindings
|
|
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)) {
|
|
|
|
//Jumping
|
|
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
|
|
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
|
|
|
|
//Moving
|
|
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &Ahwanyoung2Character::Move);
|
|
|
|
//Walking
|
|
EnhancedInputComponent->BindAction(WalkAction, ETriggerEvent::Triggered, this, &Ahwanyoung2Character::Move);
|
|
|
|
//Looking
|
|
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &Ahwanyoung2Character::Look);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Ahwanyoung2Character::Move(const FInputActionValue& Value)
|
|
{
|
|
// input is a Vector2D
|
|
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 Ahwanyoung2Character::Look(const FInputActionValue& Value)
|
|
{
|
|
// input is a Vector2D
|
|
FVector2D LookAxisVector = Value.Get<FVector2D>();
|
|
|
|
if (Controller != nullptr)
|
|
{
|
|
// add yaw and pitch input to controller
|
|
AddControllerYawInput(LookAxisVector.X);
|
|
AddControllerPitchInput(LookAxisVector.Y);
|
|
}
|
|
}
|
|
|
|
void Ahwanyoung2Character::Initialize()
|
|
{
|
|
// 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 = 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
|
|
|
|
// 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 Ahwanyoung2Character::RemoveCharacterAbilities()
|
|
//{
|
|
// //if the object doesn't have the authority, ability system component is not valid
|
|
// //or character abilities are not given in the ability system component,
|
|
// //we don't do anything and just return
|
|
// if (GetLocalRole() != ROLE_Authority ||
|
|
// !AbilitySystemComponent.IsValid() ||
|
|
// !AbilitySystemComponent->CharacterAbilitiesGiven)
|
|
// {
|
|
// return;
|
|
// }
|
|
//
|
|
// TArray<FGameplayAbilitySpecHandle> AbilitiesToRemove;
|
|
// for (const FGameplayAbilitySpec& Spec : AbilitySystemComponent->GetActivatableAbilities())
|
|
// {
|
|
// if (Spec.SourceObject == this && CharacterAbilities.Contains(Spec.Ability->GetClass()))
|
|
// {
|
|
// AbilitiesToRemove.Add(Spec.Handle); //spec.handle is an instance of the ability
|
|
// }
|
|
// }
|
|
//
|
|
// for (int32 i = 0; i < AbilitiesToRemove.Num(); i++)
|
|
// {
|
|
// AbilitySystemComponent->ClearAbility(AbilitiesToRemove[i]);
|
|
// }
|
|
//
|
|
// AbilitySystemComponent->CharacterAbilitiesGiven = false;
|
|
//}
|
|
|
|
//void Ahwanyoung2Character::Die()
|
|
//{
|
|
// RemoveCharacterAbilities();
|
|
//
|
|
// GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
|
|
// GetCharacterMovement()->GravityScale = 0;
|
|
// GetCharacterMovement()->Velocity = FVector(0); //disabling character movement when death
|
|
//
|
|
// OnCharacterDied.Broadcast(this);
|
|
//
|
|
// if (AbilitySystemComponent.IsValid())
|
|
// {
|
|
// AbilitySystemComponent->CancelAbilities();
|
|
//
|
|
// FGameplayTagContainer EffectsTagsToRemove;
|
|
// EffectsTagsToRemove.AddTag(EffectRemoveOnDeathTag);
|
|
// int32 NumEffectsRemoved = AbilitySystemComponent->RemoveActiveEffectsWithTags(EffectsTagsToRemove);
|
|
// AbilitySystemComponent->AddLooseGameplayTag(DeadTag);
|
|
// }
|
|
//
|
|
// //playing death anim:
|
|
// if (DeathMontage)
|
|
// {
|
|
// PlayAnimMontage(DeathMontage);
|
|
// }
|
|
// else
|
|
// {
|
|
// FinishDying();
|
|
// }
|
|
//}
|