Illusion-UE5/hwanyoung2/Source/hwanyoung2/hwanyoung2Character.cpp

522 lines
14 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "hwanyoung2Character.h"
#include "HYCharacAbilitySystemComponent.h"
#include "HYPlayerCharacAttributeSet.h"
#include "HYCharacGameplayAbility.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
float Ahwanyoung2Character::GetGaugeP() const
{
if (AttributeSetBase.IsValid())
{
return AttributeSetBase->GetGaugeP();
}
return 0.0f;
}
float Ahwanyoung2Character::GetMaxGaugeP() const
{
if (AttributeSetBase.IsValid())
{
return AttributeSetBase->GetMaxGaugeP();
}
return 0.0f;
}
Ahwanyoung2Character::Ahwanyoung2Character()
{
Initialize();
}
Ahwanyoung2Character::Ahwanyoung2Character(const FObjectInitializer& ObjectInitializer):
Super(ObjectInitializer.SetDefaultSubobjectClass<UCharacterMovementComponent>(
ACharacter::CharacterMovementComponentName))
{
Initialize();
// 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;
//setting up the capsule collider object for the character:
GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility,
ECollisionResponse::ECR_Overlap);
bAlwaysRelevant = true;
DeadTag = FGameplayTag::RequestGameplayTag(FName("State.Dead"));
EffectRemoveOnDeathTag = FGameplayTag::RequestGameplayTag(FName("State.RemoveOnDeath"));
}
void Ahwanyoung2Character::PossessedBy(AController* NewController)
{
Super::PossessedBy(NewController);
//In the server:
if (AbilitySystemComponent != NULL) {
AbilitySystemComponent->InitAbilityActorInfo(this, this); // who is the avatar?
}
SetOwner(this);
InitializeAttributes();
AddCharacterAbilities();
}
void Ahwanyoung2Character::OnRep_PlayerState()
{
Super::OnRep_PlayerState();
//The same business with intializing:
AbilitySystemComponent->InitAbilityActorInfo(this, this);
InitializeAttributes();
//Key binding:
if (AbilitySystemComponent != 0 && InputComponent) {
const FGameplayAbilityInputBinds Binds("Confirm", "Cancel", "EAbilityInputID",
static_cast<int32>(EAbilityInputID::Confirm), static_cast<int32>(EAbilityInputID::Cancel));
AbilitySystemComponent->BindAbilityActivationToInputComponent(InputComponent, Binds);
}
}
UAbilitySystemComponent* Ahwanyoung2Character::GetAbilitySystemComponent() const
{
return AbilitySystemComponent.Get();
}
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(DefaultMappingContext, 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 = 500; //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());
if (IController) {
//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
AHYInteractableActor* Interactable = Cast<AHYInteractableActor>(HitResult.GetActor());
if (Interactable) {
IController->CurrentInteractable = Interactable;
return;
}
}
IController->CurrentInteractable = nullptr;
}
}
void Ahwanyoung2Character::Tick(float DeltaTime)
{
}
//////////////////////////////////////////////////////////////////////////
// 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);
//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++)
}
bool Ahwanyoung2Character::IsAlive() const
{
return GetHealth() > 0.0f;
}
int32 Ahwanyoung2Character::GetAbilityLevel(EAbilityInputID AbilityID) const
{
return int32();
}
//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();
// }
//}
void Ahwanyoung2Character::FinishDying()
{
Destroy();
}
float Ahwanyoung2Character::GetPlayerLevel() const
{
if (AttributeSetBase.IsValid())
{
return AttributeSetBase->GetPlayerLevel();
}
return 0.0f;
}
float Ahwanyoung2Character::GetHealth() const
{
if (AttributeSetBase.IsValid())
{
return AttributeSetBase->GetHealth();
}
return 0.0f;
}
float Ahwanyoung2Character::GetMaxHealth() const
{
if (AttributeSetBase.IsValid())
{
return AttributeSetBase->GetMaxHealth();
}
return 0.0f;
}
float Ahwanyoung2Character::GetMana() const
{
if (AttributeSetBase.IsValid())
{
return AttributeSetBase->GetMana();
}
return 0.0f;
}
float Ahwanyoung2Character::GetMaxMana() const
{
if (AttributeSetBase.IsValid())
{
return AttributeSetBase->GetMaxMana();
}
return 0.0f;
}
float Ahwanyoung2Character::GetStamina() const
{
if (AttributeSetBase.IsValid())
{
return AttributeSetBase->GetStamina();
}
return 0.0f;
}
float Ahwanyoung2Character::GetMaxStamina() const
{
if (AttributeSetBase.IsValid())
{
return AttributeSetBase->GetMaxStamina();
}
return 0.0f;
}
void Ahwanyoung2Character::AddCharacterAbilities()
{
if (GetLocalRole() != ROLE_Authority ||
!AbilitySystemComponent.IsValid() ||
AbilitySystemComponent->CharacterAbilitiesGiven)
{
return;
}
//if there are no abilities added to the character:
for (TSubclassOf<UHYCharacGameplayAbility>& StartUpAbility : CharacterAbilities)
{
AbilitySystemComponent->GiveAbility(
FGameplayAbilitySpec(StartUpAbility,
GetAbilityLevel(StartUpAbility.GetDefaultObject()->AbilityID),
static_cast<int32> (StartUpAbility.GetDefaultObject()->AbilityInputID),
this));
}
}
void Ahwanyoung2Character::InitializeAttributes()
{
if (!AbilitySystemComponent.IsValid())
{
return;
}
if (!DefaultAttributes)
{
UE_LOG(LogTemp, Error,
TEXT("%s() Missing DefaultAttributes for %s. Please fill in the character's Blueprint."),
*FString(__FUNCTION__), *GetName());
return;
}
FGameplayEffectContextHandle EffectContext = AbilitySystemComponent->MakeEffectContext();
EffectContext.AddSourceObject(this);
FGameplayEffectSpecHandle NewHandle = AbilitySystemComponent->
MakeOutgoingSpec(DefaultAttributes, GetPlayerLevel(), EffectContext);
if (NewHandle.IsValid())
{
//apply the gameplayeffect here:
FActiveGameplayEffectHandle ActiveGEHandle = AbilitySystemComponent->ApplyGameplayEffectSpecToTarget(
*NewHandle.Data.Get(), AbilitySystemComponent.Get());
}
}
void Ahwanyoung2Character::AddStartupEffects()
{
if (GetLocalRole() != ROLE_Authority ||
!AbilitySystemComponent.IsValid() ||
AbilitySystemComponent->StartupEffectApplied)
{
return;
}
FGameplayEffectContextHandle EffectContext = AbilitySystemComponent->MakeEffectContext();
EffectContext.AddSourceObject(this);
for (TSubclassOf<UGameplayEffect> GameplayEffect : StartupEffects)
{
FGameplayEffectSpecHandle NewHandle = AbilitySystemComponent->MakeOutgoingSpec(GameplayEffect, GetPlayerLevel(), EffectContext);
if (NewHandle.IsValid())
{
FActiveGameplayEffectHandle ActiveGEHandle =
AbilitySystemComponent->ApplyGameplayEffectSpecToTarget(
*NewHandle.Data.Get(), AbilitySystemComponent.Get());
}
}
AbilitySystemComponent->StartupEffectApplied = true;
}
void Ahwanyoung2Character::SetHealth(float Health)
{
if (AttributeSetBase.IsValid())
{
AttributeSetBase->SetHealth(Health);
}
}
void Ahwanyoung2Character::SetMana(float Mana)
{
if (AttributeSetBase.IsValid())
{
AttributeSetBase->SetMana(Mana);
}
}
void Ahwanyoung2Character::SetStamina(float Stamina)
{
if (AttributeSetBase.IsValid())
{
AttributeSetBase->SetStamina(Stamina);
}
}
void Ahwanyoung2Character::SetGaugeP(float GaugeP)
{
if (AttributeSetBase.IsValid())
{
AttributeSetBase->SetGaugeP(GaugeP);
}
}