Advertisement
Dieton

SlashCharacter.cpp

Nov 13th, 2023 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.31 KB | Gaming | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "Characters/SlashCharacter.h"
  4.  
  5. #include "Components/InputComponent.h"
  6. #include "EnhancedInputSubsystems.h"
  7. #include "EnhancedInputComponent.h"
  8. #include "Slash/DebugMacros.h"
  9. #include "GameFramework/SpringArmComponent.h"
  10. #include "Camera/CameraComponent.h"
  11. #include "GameFramework/CharacterMovementComponent.h"
  12. #include "GroomComponent.h"
  13. #include "Items/Item.h"
  14. #include "Items/Weapons/Weapon.h"
  15. #include "Animation/AnimMontage.h"
  16. #include "Components/BoxComponent.h"
  17.  
  18. // Sets default values
  19. ASlashCharacter::ASlashCharacter()
  20. {
  21.     // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  22.     PrimaryActorTick.bCanEverTick = true;
  23.  
  24.     bUseControllerRotationPitch = false;
  25.     bUseControllerRotationYaw = false;
  26.     bUseControllerRotationRoll = false;
  27.  
  28.     GetCharacterMovement()->bOrientRotationToMovement = true;
  29.     GetCharacterMovement()->RotationRate = FRotator(0.f,400.f,0.f);
  30.  
  31.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  32.     CameraBoom->SetupAttachment(GetRootComponent());
  33.     CameraBoom->TargetArmLength = 300;
  34.     CameraBoom->bUsePawnControlRotation = true;
  35.  
  36.     ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
  37.     ViewCamera->SetupAttachment(CameraBoom);
  38.  
  39.     Hair = CreateDefaultSubobject<UGroomComponent>(TEXT("Hair"));
  40.     Hair->SetupAttachment(GetMesh());
  41.     Hair->AttachmentName = FString("head");
  42.  
  43.     Eyebrows = CreateDefaultSubobject<UGroomComponent>(TEXT("Eyebrows"));
  44.     Eyebrows->SetupAttachment(GetMesh());
  45.     Eyebrows->AttachmentName = FString("head");
  46. }
  47.  
  48. // Called when the game starts or when spawned
  49. void ASlashCharacter::BeginPlay()
  50. {
  51.     Super::BeginPlay();
  52.    
  53.     if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
  54.     {
  55.         if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
  56.         {
  57.             Subsystem->AddMappingContext(SlashMappingContext, 0);
  58.         }
  59.     }
  60. }
  61.  
  62. void ASlashCharacter::SetWeaponCollisionEnable(ECollisionEnabled::Type CollisionEnabled)
  63. {
  64.     if (EquippedWeapon && EquippedWeapon->GetWeaponBox())
  65.     {
  66.         EquippedWeapon->GetWeaponBox()->SetCollisionEnabled(CollisionEnabled);
  67.         EquippedWeapon->IgnoreActors.Empty();
  68.     }
  69. }
  70.  
  71. void ASlashCharacter::Move(const FInputActionValue& Value)
  72. {
  73.     //if (ActionState != EActionState::EAS_Unoccupied&) return;
  74.     if (ActionState != EActionState::EAS_Unoccupied) return;
  75.  
  76.     if (Controller && (Value.Get<FVector2D>() != FVector2D(0.f,0.f)))
  77.     {
  78.         const FVector2D MovementVector = Value.Get<FVector2D>();
  79.  
  80.         const FRotator Rotation = Controller->GetControlRotation();
  81.         const FRotator YawRotation(0.f, Rotation.Yaw, 0.f);
  82.  
  83.         const FVector FowardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  84.         AddMovementInput(FowardDirection, MovementVector.Y);
  85.         const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  86.         AddMovementInput(RightDirection, MovementVector.X);
  87.     }
  88.  
  89.     /*
  90.     FVector Forward = GetActorForwardVector();
  91.     AddMovementInput(Forward, MovementVector.Y);
  92.     FVector Right = GetActorRightVector();
  93.     AddMovementInput(Right, MovementVector.X);
  94.     */
  95. }
  96.  
  97. void ASlashCharacter::Look(const FInputActionValue& Value)
  98. {
  99.     const FVector2D LookAxisVector = Value.Get<FVector2D>();
  100.  
  101.     AddControllerPitchInput(LookAxisVector.Y);
  102.     AddControllerYawInput(LookAxisVector.X);
  103. }
  104.  
  105. void ASlashCharacter::Jump()
  106. {
  107.     Super::Jump();
  108. }
  109.  
  110. void ASlashCharacter::Equip()
  111. {
  112.     //UE_LOG(LogTemp, Warning, TEXT("Equip!"));
  113.     AWeapon* OverlappingWeapon = Cast<AWeapon>(OverlapingItem);
  114.     if (OverlappingWeapon)
  115.     {
  116.         OverlappingWeapon->Equip(GetMesh(), FName("RightHandSocket"));
  117.         CharacterState = ECharacterState::ECS_EquippedOneHandedWeapon;
  118.         OverlapingItem = nullptr;
  119.         EquippedWeapon = OverlappingWeapon;
  120.     }
  121.     else
  122.     {
  123.         if (CanDisarm())
  124.         {
  125.             PlayeEquipMontage(FName("Unequip"));
  126.             CharacterState = ECharacterState::ECS_Unequipped;
  127.             ActionState = EActionState::EAS_EquippingWeapon;
  128.         }
  129.         else if (CanArm())
  130.         {
  131.             PlayeEquipMontage(FName("Equip"));
  132.             CharacterState = ECharacterState::ECS_EquippedOneHandedWeapon;
  133.             ActionState = EActionState::EAS_EquippingWeapon;
  134.         }
  135.     }
  136. }
  137.  
  138. void ASlashCharacter::Attack()
  139. {
  140.     if (CanAttack())
  141.     {
  142.         PlayAttackMontage();
  143.         ActionState = EActionState::EAS_Attacking;
  144.     }
  145. }
  146.  
  147. bool ASlashCharacter::CanAttack()
  148. {
  149.     return
  150.         ActionState == EActionState::EAS_Unoccupied &&
  151.         CharacterState != ECharacterState::ECS_Unequipped;
  152. }
  153.  
  154.  
  155. bool ASlashCharacter::CanDisarm()
  156. {
  157.     return
  158.         ActionState == EActionState::EAS_Unoccupied &&
  159.         CharacterState != ECharacterState::ECS_Unequipped;
  160. }
  161.  
  162. bool ASlashCharacter::CanArm()
  163. {
  164.     return ActionState == EActionState::EAS_Unoccupied &&
  165.         CharacterState == ECharacterState::ECS_Unequipped &&
  166.         EquippedWeapon;
  167. }
  168.  
  169. void ASlashCharacter::Disarm()
  170. {
  171.     if (EquippedWeapon)
  172.     {
  173.         EquippedWeapon->AttachMeshToSocket(GetMesh(), FName("SpineSocket"));
  174.     }
  175. }
  176.  
  177. void ASlashCharacter::Arm()
  178. {
  179.     if (EquippedWeapon)
  180.     {
  181.         EquippedWeapon->AttachMeshToSocket(GetMesh(), FName("RightHandSocket"));
  182.     }
  183. }
  184.  
  185. void ASlashCharacter::FinishEquipping()
  186. {
  187.     ActionState = EActionState::EAS_Unoccupied;
  188. }
  189.  
  190. void ASlashCharacter::PlayAttackMontage()
  191. {
  192.     //UE_LOG(LogTemp, Warning, TEXT("Attack!"));
  193.     UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
  194.     if (AnimInstance && AttackMontage)
  195.     {
  196.         AnimInstance->Montage_Play(AttackMontage);
  197.         const int32 Selection = FMath::RandRange(0, 1);
  198.         FName SlectionName = FName();
  199.         switch (Selection)
  200.         {
  201.         case 0:
  202.             SlectionName = FName("Attack1");
  203.             break;
  204.         case 1:
  205.             SlectionName = FName("Attack2");
  206.             break;
  207.         default:
  208.             break;
  209.         }
  210.         AnimInstance->Montage_JumpToSection(SlectionName, AttackMontage);
  211.     }
  212. }
  213.  
  214. void ASlashCharacter::PlayeEquipMontage(const FName& SectionName)
  215. {
  216.  
  217.     UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
  218.     if (AnimInstance && EquipMontage)
  219.     {
  220.         AnimInstance->Montage_Play(EquipMontage);
  221.         AnimInstance->Montage_JumpToSection(SectionName, EquipMontage);
  222.     }
  223. }
  224.  
  225. void ASlashCharacter::AttackEnd()
  226. {
  227.     ActionState = EActionState::EAS_Unoccupied;
  228. }
  229.  
  230.  
  231.  
  232. void ASlashCharacter::Dodge()
  233. {
  234.     UE_LOG(LogTemp, Warning, TEXT("Dodge!"));
  235. }
  236.  
  237. // Called every frame
  238. void ASlashCharacter::Tick(float DeltaTime)
  239. {
  240.     Super::Tick(DeltaTime);
  241. }
  242.  
  243. // Called to bind functionality to input
  244. void ASlashCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  245. {
  246.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  247.  
  248.     if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
  249.     {
  250.         EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Move);
  251.         EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Look);
  252.         EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Jump);
  253.         EnhancedInputComponent->BindAction(EquipAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Equip);
  254.         EnhancedInputComponent->BindAction(AttackAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Attack);
  255.         EnhancedInputComponent->BindAction(DodgeAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Dodge);
  256.     }
  257. }
  258.  
  259.  
  260.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement