2023-10-10 01:37:22 +02:00
// Copyright Epic Games, Inc. All Rights Reserved.
# include "hwanyoung2Character.h"
2023-11-28 01:22:22 +01:00
# include "HYPlayerCharacController.h"
2023-10-10 01:37:22 +02:00
# 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"
2023-12-26 15:33:12 +01:00
# include "HYInteractableActor.h"
2023-11-28 01:22:22 +01:00
# include "InventoryItem.h"
2024-08-22 16:24:14 +02:00
# include "HYAutomaticPickUp.h"
2023-11-28 01:22:22 +01:00
2023-10-10 01:37:22 +02:00
//////////////////////////////////////////////////////////////////////////
// Ahwanyoung2Character
Ahwanyoung2Character : : Ahwanyoung2Character ( )
{
2024-01-04 15:03:39 +01:00
Initialize ( ) ;
2023-10-10 01:37:22 +02:00
}
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 ( ) ) )
{
2024-06-08 16:26:38 +02:00
Subsystem - > AddMappingContext ( InputMapping , 0 ) ;
2023-10-10 01:37:22 +02:00
}
}
2024-06-08 16:26:38 +02:00
2023-10-10 01:37:22 +02:00
}
2024-11-18 16:06:48 +01:00
//void Ahwanyoung2Character::SetGenericTeamId(const FGenericTeamId& NewTeamID)
//{
//}
//
//FGenericTeamId Ahwanyoung2Character::GetGenericTeamId() const
//{
// return FGenericTeamId();
//}
2023-11-28 01:22:22 +01:00
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
2024-06-17 16:38:43 +02:00
int32 Range = 800 ; //this can be changed
2023-11-28 01:22:22 +01:00
//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 ( ) ) ;
2024-06-17 16:38:43 +02:00
//Checking if something is hit by the line cast within the range
if ( GetWorld ( ) - > LineTraceSingleByChannel ( HitResult , StartTrace , EndTrace ,
ECC_Visibility , QueryParams ) ) {
2023-11-28 01:22:22 +01:00
2024-06-17 16:38:43 +02:00
//Cast the actor to AInteractable
if ( AHYInteractableActor * Interactable = Cast < AHYInteractableActor > ( HitResult . GetActor ( ) ) ) {
IController - > CurrentInteractable = Interactable ;
return ;
2023-11-28 01:22:22 +01:00
}
2024-06-17 16:38:43 +02:00
} else {
2023-11-28 01:22:22 +01:00
IController - > CurrentInteractable = nullptr ;
}
2024-06-17 16:38:43 +02:00
2023-11-28 01:22:22 +01:00
}
2024-08-22 16:24:14 +02:00
void Ahwanyoung2Character : : CollectAutoPickups ( )
{
// Stores all the overlapping actors in an array
TArray < AActor * > CollectedActors ;
CollectionSphere - > GetOverlappingActors ( CollectedActors ) ;
AHYPlayerCharacController * IController = Cast < AHYPlayerCharacController > ( GetController ( ) ) ;
for ( int32 indCollect = 0 ; indCollect < CollectedActors . Num ( ) ; + + indCollect ) {
AHYAutomaticPickUp * const Pickup = Cast < AHYAutomaticPickUp > ( CollectedActors [ indCollect ] ) ;
if ( Pickup & & ! Pickup - > IsPendingKill ( ) ) {
Pickup - > Collect ( IController ) ;
}
}
}
2023-11-28 01:22:22 +01:00
void Ahwanyoung2Character : : Tick ( float DeltaTime )
{
2024-06-12 15:48:50 +02:00
Super : : Tick ( DeltaTime ) ;
CheckForInteractables ( ) ;
2024-08-22 16:24:14 +02:00
CollectAutoPickups ( ) ;
2023-11-28 01:22:22 +01:00
}
2023-10-10 01:37:22 +02:00
//////////////////////////////////////////////////////////////////////////
// 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 ) ;
2024-06-10 11:41:00 +02:00
//Walking
EnhancedInputComponent - > BindAction ( WalkAction , ETriggerEvent : : Triggered , this , & Ahwanyoung2Character : : Move ) ;
2023-10-10 01:37:22 +02:00
//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 ) ;
}
}
2024-01-04 15:03:39 +01:00
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++)
2024-08-22 16:24:14 +02:00
CollectionSphere = CreateDefaultSubobject < USphereComponent > ( TEXT ( " CollectionSphere " ) ) ;
CollectionSphere - > SetupAttachment ( RootComponent ) ;
CollectionSphere - > SetSphereRadius ( 200.f ) ;
2024-01-04 15:03:39 +01:00
}
2023-10-10 01:37:22 +02:00
2024-06-01 02:29:00 +02:00
2023-10-10 01:37:22 +02:00
2023-11-28 01:22:22 +01:00
//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();
// }
//}