Advertisement
Guest User

GASCharacter.cpp

a guest
Mar 11th, 2021
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.31 KB | None | 0 0
  1. #include "GASCharacter.h"
  2. #include "HeadMountedDisplayFunctionLibrary.h"
  3. #include "Camera/CameraComponent.h"
  4. #include "Components/CapsuleComponent.h"
  5. #include "Components/InputComponent.h"
  6. #include "GameFramework/CharacterMovementComponent.h"
  7. #include "GameFramework/Controller.h"
  8. #include "GameFramework/SpringArmComponent.h"
  9. #include "GASAbilitySystemComponent.h"
  10. #include "GASAttributeSet.h"
  11. #include "AbilitySystemGlobals.h"
  12. #include "GASGameplayAbility.h"
  13. #include <GameplayEffectTypes.h>
  14.  
  15. //////////////////////////////////////////////////////////////////////////
  16. // AGASCharacter
  17.  
  18. AGASCharacter::AGASCharacter()
  19. {
  20.     // Set size for collision capsule
  21.     GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
  22.  
  23.     // set our turn rates for input
  24.     BaseTurnRate = 45.f;
  25.     BaseLookUpRate = 45.f;
  26.  
  27.     // Don't rotate when the controller rotates. Let that just affect the camera.
  28.     bUseControllerRotationPitch = false;
  29.     bUseControllerRotationYaw = false;
  30.     bUseControllerRotationRoll = false;
  31.  
  32.     // Configure character movement
  33.     GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...  
  34.     GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
  35.     GetCharacterMovement()->JumpZVelocity = 600.f;
  36.     GetCharacterMovement()->AirControl = 0.2f;
  37.  
  38.     // Create a camera boom (pulls in towards the player if there is a collision)
  39.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  40.     CameraBoom->SetupAttachment(RootComponent);
  41.     CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character  
  42.     CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
  43.  
  44.     // Create a follow camera
  45.     FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  46.     FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  47.     FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  48.  
  49.     AbilitySystemComponent = CreateDefaultSubobject<UGASAbilitySystemComponent>("AbilitySystemComp");
  50.     AbilitySystemComponent->SetIsReplicated(true);
  51.     AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Minimal);
  52.  
  53.     Attributes = CreateDefaultSubobject<UGASAttributeSet>("Attributes");
  54.  
  55.     // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
  56.     // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
  57. }
  58.  
  59. class UAbilitySystemComponent* AGASCharacter::GetAbilitySystemComponent() const
  60. {
  61.     return AbilitySystemComponent;
  62. }
  63.  
  64. void AGASCharacter::InitializeAttributes()
  65. {
  66.     if (AbilitySystemComponent && DefaultAttributeEffect)
  67.     {
  68.         FGameplayEffectContextHandle EffectContext = AbilitySystemComponent->MakeEffectContext();
  69.         EffectContext.AddSourceObject(this);
  70.  
  71.         FGameplayEffectSpecHandle SpecHandle = AbilitySystemComponent->MakeOutgoingSpec(DefaultAttributeEffect, 1, EffectContext);
  72.  
  73.         if (SpecHandle.IsValid())
  74.         {
  75.             FActiveGameplayEffectHandle GEHandle = AbilitySystemComponent->ApplyGameplayEffectSpecToSelf(*SpecHandle.Data.Get());
  76.         }
  77.     }
  78. }
  79.  
  80. void AGASCharacter::GiveAbilities()
  81. {
  82.     if (HasAuthority() && AbilitySystemComponent)
  83.     {
  84.         for (TSubclassOf<UGASGameplayAbility>& StartupAbility : DefaultAbilities)
  85.         {
  86.             AbilitySystemComponent->GiveAbility(
  87.                 FGameplayAbilitySpec(StartupAbility, 1, static_cast<int32>(StartupAbility.GetDefaultObject()->AbilityInputID), this));
  88.         }
  89.     }
  90. }
  91.  
  92. void AGASCharacter::PossessedBy(AController* NewController)
  93. {
  94.     Super::PossessedBy(NewController);
  95.  
  96.     //Server GAS init
  97.     AbilitySystemComponent->InitAbilityActorInfo(this, this);
  98.  
  99.     InitializeAttributes();
  100.     GiveAbilities();
  101. }
  102.  
  103. void AGASCharacter::OnRep_PlayerState()
  104. {
  105.     Super::OnRep_PlayerState();
  106.  
  107.     AbilitySystemComponent->InitAbilityActorInfo(this, this);
  108.     InitializeAttributes();
  109.  
  110. }
  111.  
  112. //////////////////////////////////////////////////////////////////////////
  113. // Input
  114.  
  115. void AGASCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
  116. {
  117.     // Set up gameplay key bindings
  118.     check(PlayerInputComponent);
  119.     PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  120.     PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  121.  
  122.     PlayerInputComponent->BindAxis("MoveForward", this, &AGASCharacter::MoveForward);
  123.     PlayerInputComponent->BindAxis("MoveRight", this, &AGASCharacter::MoveRight);
  124.  
  125.     // We have 2 versions of the rotation bindings to handle different kinds of devices differently
  126.     // "turn" handles devices that provide an absolute delta, such as a mouse.
  127.     // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  128.     PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  129.     PlayerInputComponent->BindAxis("TurnRate", this, &AGASCharacter::TurnAtRate);
  130.     PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  131.     PlayerInputComponent->BindAxis("LookUpRate", this, &AGASCharacter::LookUpAtRate);
  132.  
  133.     // handle touch devices
  134.     PlayerInputComponent->BindTouch(IE_Pressed, this, &AGASCharacter::TouchStarted);
  135.     PlayerInputComponent->BindTouch(IE_Released, this, &AGASCharacter::TouchStopped);
  136.  
  137.     // VR headset functionality
  138.     PlayerInputComponent->BindAction("ResetVR", IE_Pressed, this, &AGASCharacter::OnResetVR);
  139.  
  140.     if (AbilitySystemComponent && InputComponent)
  141.     {
  142.         const FGameplayAbilityInputBinds Binds("Confirm", "Cancel", "EGASAbilityInputID", static_cast<int32>(EGASAbilityInputID::Confirm), static_cast<int32>(EGASAbilityInputID::Cancel));
  143.         AbilitySystemComponent->BindAbilityActivationToInputComponent(InputComponent, Binds);
  144.     }
  145. }
  146.  
  147.  
  148. void AGASCharacter::OnResetVR()
  149. {
  150.     UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition();
  151. }
  152.  
  153. void AGASCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
  154. {
  155.         Jump();
  156. }
  157.  
  158. void AGASCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
  159. {
  160.         StopJumping();
  161. }
  162.  
  163. void AGASCharacter::TurnAtRate(float Rate)
  164. {
  165.     // calculate delta for this frame from the rate information
  166.     AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  167. }
  168.  
  169. void AGASCharacter::LookUpAtRate(float Rate)
  170. {
  171.     // calculate delta for this frame from the rate information
  172.     AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  173. }
  174.  
  175. void AGASCharacter::MoveForward(float Value)
  176. {
  177.     if ((Controller != NULL) && (Value != 0.0f))
  178.     {
  179.         // find out which way is forward
  180.         const FRotator Rotation = Controller->GetControlRotation();
  181.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  182.  
  183.         // get forward vector
  184.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  185.         AddMovementInput(Direction, Value);
  186.     }
  187. }
  188.  
  189. void AGASCharacter::MoveRight(float Value)
  190. {
  191.     if ( (Controller != NULL) && (Value != 0.0f) )
  192.     {
  193.         // find out which way is right
  194.         const FRotator Rotation = Controller->GetControlRotation();
  195.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  196.    
  197.         // get right vector
  198.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  199.         // add movement in that direction
  200.         AddMovementInput(Direction, Value);
  201.     }
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement