in the middle of implementing the controller

This commit is contained in:
Ji Yoon Rhee 2023-11-27 19:22:22 -05:00
parent d91c59aa5c
commit 36bcb08c75
4 changed files with 142 additions and 75 deletions

View File

@ -2,6 +2,8 @@
#pragma once #pragma once
#include "Interactable.h"
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/PlayerController.h" #include "GameFramework/PlayerController.h"
#include "HYPlayerCharacController.generated.h" #include "HYPlayerCharacController.generated.h"

View File

@ -4,6 +4,7 @@
#include "HYCharacAbilitySystemComponent.h" #include "HYCharacAbilitySystemComponent.h"
#include "HYPlayerCharacAttributeSet.h" #include "HYPlayerCharacAttributeSet.h"
#include "HYCharacGameplayAbility.h" #include "HYCharacGameplayAbility.h"
#include "HYPlayerCharacController.h"
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h" #include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h" #include "Components/InputComponent.h"
@ -12,6 +13,9 @@
#include "GameFramework/SpringArmComponent.h" #include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputComponent.h" #include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h" #include "EnhancedInputSubsystems.h"
#include "Interactable.h"
#include "InventoryItem.h"
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -153,6 +157,52 @@ void Ahwanyoung2Character::BeginPlay()
} }
} }
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
AInteractable* Interactable = Cast<AInteractable>(HitResult.GetActor());
if (Interactable) {
IController->CurrentInteractable = Interactable;
return;
}
}
IController->CurrentInteractable = nullptr;
}
}
void Ahwanyoung2Character::Tick(float DeltaTime)
{
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// Input // Input
@ -221,65 +271,65 @@ int32 Ahwanyoung2Character::GetAbilityLevel(EAbilityInputID AbilityID) const
return int32(); return int32();
} }
void Ahwanyoung2Character::RemoveCharacterAbilities() //void Ahwanyoung2Character::RemoveCharacterAbilities()
{ //{
//if the object doesn't have the authority, ability system component is not valid // //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, // //or character abilities are not given in the ability system component,
//we don't do anything and just return // //we don't do anything and just return
if (GetLocalRole() != ROLE_Authority || // if (GetLocalRole() != ROLE_Authority ||
!AbilitySystemComponent.IsValid() || // !AbilitySystemComponent.IsValid() ||
!AbilitySystemComponent->CharacterAbilitiesGiven) // !AbilitySystemComponent->CharacterAbilitiesGiven)
{ // {
return; // 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;
//}
TArray<FGameplayAbilitySpecHandle> AbilitiesToRemove; //void Ahwanyoung2Character::Die()
for (const FGameplayAbilitySpec& Spec : AbilitySystemComponent->GetActivatableAbilities()) //{
{ // RemoveCharacterAbilities();
if (Spec.SourceObject == this && CharacterAbilities.Contains(Spec.Ability->GetClass())) //
{ // GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
AbilitiesToRemove.Add(Spec.Handle); //spec.handle is an instance of the ability // GetCharacterMovement()->GravityScale = 0;
} // GetCharacterMovement()->Velocity = FVector(0); //disabling character movement when death
} //
// OnCharacterDied.Broadcast(this);
for (int32 i = 0; i < AbilitiesToRemove.Num(); i++) //
{ // if (AbilitySystemComponent.IsValid())
AbilitySystemComponent->ClearAbility(AbilitiesToRemove[i]); // {
} // AbilitySystemComponent->CancelAbilities();
//
AbilitySystemComponent->CharacterAbilitiesGiven = false; // FGameplayTagContainer EffectsTagsToRemove;
} // EffectsTagsToRemove.AddTag(EffectRemoveOnDeathTag);
// int32 NumEffectsRemoved = AbilitySystemComponent->RemoveActiveEffectsWithTags(EffectsTagsToRemove);
void Ahwanyoung2Character::Die() // AbilitySystemComponent->AddLooseGameplayTag(DeadTag);
{ // }
RemoveCharacterAbilities(); //
// //playing death anim:
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision); // if (DeathMontage)
GetCharacterMovement()->GravityScale = 0; // {
GetCharacterMovement()->Velocity = FVector(0); //disabling character movement when death // PlayAnimMontage(DeathMontage);
// }
OnCharacterDied.Broadcast(this); // else
// {
if (AbilitySystemComponent.IsValid()) // FinishDying();
{ // }
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() void Ahwanyoung2Character::FinishDying()
{ {
@ -461,3 +511,4 @@ void Ahwanyoung2Character::SetGaugeP(float GaugeP)
AttributeSetBase->SetGaugeP(GaugeP); AttributeSetBase->SetGaugeP(GaugeP);
} }
} }

View File

@ -28,6 +28,11 @@ class Ahwanyoung2Character : public ACharacter, public IAbilitySystemInterface
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera; class UCameraComponent* FollowCamera;
/** Collection sphere */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USphereComponent* CollectionSphere;
#pragma region Controller
/** MappingContext */ /** MappingContext */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true")) UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputMappingContext* DefaultMappingContext; class UInputMappingContext* DefaultMappingContext;
@ -44,6 +49,8 @@ class Ahwanyoung2Character : public ACharacter, public IAbilitySystemInterface
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true")) UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* LookAction; class UInputAction* LookAction;
#pragma endregion move this to PlayerCharacController.h
public: public:
@ -56,10 +63,10 @@ public:
UFUNCTION(BlueprintCallable, Category = "Hwanyoung|Character") UFUNCTION(BlueprintCallable, Category = "Hwanyoung|Character")
virtual int32 GetAbilityLevel(EAbilityInputID AbilityID) const; virtual int32 GetAbilityLevel(EAbilityInputID AbilityID) const;
//this function is called only in the server: ////this function is called only in the server:
virtual void RemoveCharacterAbilities(); //virtual void RemoveCharacterAbilities();
//this is also called only in the server: ////this is also called only in the server:
virtual void Die(); //virtual void Die();
UFUNCTION(BlueprintCallable, Category = "Hwanyoung|Character") UFUNCTION(BlueprintCallable, Category = "Hwanyoung|Character")
virtual void FinishDying(); virtual void FinishDying();
@ -102,6 +109,13 @@ public:
UFUNCTION(BlueprintCallable, Category = "HY_Character|Camera") UFUNCTION(BlueprintCallable, Category = "HY_Character|Camera")
FVector GetStartingCameraBoomLocation(); FVector GetStartingCameraBoomLocation();
/** Returns CameraBoom subobject **/
class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
/** Returns FollowCamera subobject **/
class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
virtual void Tick(float DeltaTime) override;
protected: protected:
//pointers to the attribute set and ability system component //pointers to the attribute set and ability system component
@ -139,19 +153,19 @@ protected:
FGameplayTag EffectRemoveOnDeathTag; FGameplayTag EffectRemoveOnDeathTag;
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Hwanyoung|Character") UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Hwanyoung|Character")
FText CharacterName; FText CharacterName;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Hwanyoung|Animation") UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Hwanyoung|Animation")
UAnimMontage* DeathMontage; UAnimMontage* DeathMontage;
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Hwanyoung|Abilties") UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Hwanyoung|Abilties")
TSubclassOf<class UGameplayEffect> DefaultAttributes; TSubclassOf<class UGameplayEffect> DefaultAttributes;
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Hwanyoung|Abilities") UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Hwanyoung|Abilities")
TArray<TSubclassOf<class UGameplayEffect>> StartupEffects; TArray<TSubclassOf<class UGameplayEffect>> StartupEffects;
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Hwanyoung|Abilities") UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Hwanyoung|Abilities")
TArray<TSubclassOf<class UHYCharacGameplayAbility>> CharacterAbilities; TArray<TSubclassOf<class UHYCharacGameplayAbility>> CharacterAbilities;
virtual void AddCharacterAbilities(); virtual void AddCharacterAbilities();
virtual void InitializeAttributes(); virtual void InitializeAttributes();
@ -173,6 +187,9 @@ protected:
/** Called for looking input */ /** Called for looking input */
void Look(const FInputActionValue& Value); void Look(const FInputActionValue& Value);
/** Called for checking for the closest Interactable in sight and in range*/
void CheckForInteractables();
protected: protected:
// APawn interface // APawn interface
@ -181,11 +198,8 @@ protected:
// To add mapping context // To add mapping context
virtual void BeginPlay(); virtual void BeginPlay();
public: // Checks for the closest Interactable in sight and in range
/** Returns CameraBoom subobject **/ void CheckForInteractables();
class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
/** Returns FollowCamera subobject **/
class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
}; };