Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "Characters/SlashCharacter.h"
- #include "Components/InputComponent.h"
- #include "EnhancedInputSubsystems.h"
- #include "EnhancedInputComponent.h"
- #include "Slash/DebugMacros.h"
- #include "GameFramework/SpringArmComponent.h"
- #include "Camera/CameraComponent.h"
- #include "GameFramework/CharacterMovementComponent.h"
- #include "GroomComponent.h"
- #include "Items/Item.h"
- #include "Items/Weapons/Weapon.h"
- #include "Animation/AnimMontage.h"
- #include "Components/BoxComponent.h"
- // Sets default values
- ASlashCharacter::ASlashCharacter()
- {
- // 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;
- bUseControllerRotationPitch = false;
- bUseControllerRotationYaw = false;
- bUseControllerRotationRoll = false;
- GetCharacterMovement()->bOrientRotationToMovement = true;
- GetCharacterMovement()->RotationRate = FRotator(0.f,400.f,0.f);
- CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
- CameraBoom->SetupAttachment(GetRootComponent());
- CameraBoom->TargetArmLength = 300;
- CameraBoom->bUsePawnControlRotation = true;
- ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
- ViewCamera->SetupAttachment(CameraBoom);
- Hair = CreateDefaultSubobject<UGroomComponent>(TEXT("Hair"));
- Hair->SetupAttachment(GetMesh());
- Hair->AttachmentName = FString("head");
- Eyebrows = CreateDefaultSubobject<UGroomComponent>(TEXT("Eyebrows"));
- Eyebrows->SetupAttachment(GetMesh());
- Eyebrows->AttachmentName = FString("head");
- }
- // Called when the game starts or when spawned
- void ASlashCharacter::BeginPlay()
- {
- Super::BeginPlay();
- if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
- {
- if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
- {
- Subsystem->AddMappingContext(SlashMappingContext, 0);
- }
- }
- }
- void ASlashCharacter::SetWeaponCollisionEnable(ECollisionEnabled::Type CollisionEnabled)
- {
- if (EquippedWeapon && EquippedWeapon->GetWeaponBox())
- {
- EquippedWeapon->GetWeaponBox()->SetCollisionEnabled(CollisionEnabled);
- EquippedWeapon->IgnoreActors.Empty();
- }
- }
- void ASlashCharacter::Move(const FInputActionValue& Value)
- {
- //if (ActionState != EActionState::EAS_Unoccupied&) return;
- if (ActionState != EActionState::EAS_Unoccupied) return;
- if (Controller && (Value.Get<FVector2D>() != FVector2D(0.f,0.f)))
- {
- const FVector2D MovementVector = Value.Get<FVector2D>();
- const FRotator Rotation = Controller->GetControlRotation();
- const FRotator YawRotation(0.f, Rotation.Yaw, 0.f);
- const FVector FowardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
- AddMovementInput(FowardDirection, MovementVector.Y);
- const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
- AddMovementInput(RightDirection, MovementVector.X);
- }
- /*
- FVector Forward = GetActorForwardVector();
- AddMovementInput(Forward, MovementVector.Y);
- FVector Right = GetActorRightVector();
- AddMovementInput(Right, MovementVector.X);
- */
- }
- void ASlashCharacter::Look(const FInputActionValue& Value)
- {
- const FVector2D LookAxisVector = Value.Get<FVector2D>();
- AddControllerPitchInput(LookAxisVector.Y);
- AddControllerYawInput(LookAxisVector.X);
- }
- void ASlashCharacter::Jump()
- {
- Super::Jump();
- }
- void ASlashCharacter::Equip()
- {
- //UE_LOG(LogTemp, Warning, TEXT("Equip!"));
- AWeapon* OverlappingWeapon = Cast<AWeapon>(OverlapingItem);
- if (OverlappingWeapon)
- {
- OverlappingWeapon->Equip(GetMesh(), FName("RightHandSocket"));
- CharacterState = ECharacterState::ECS_EquippedOneHandedWeapon;
- OverlapingItem = nullptr;
- EquippedWeapon = OverlappingWeapon;
- }
- else
- {
- if (CanDisarm())
- {
- PlayeEquipMontage(FName("Unequip"));
- CharacterState = ECharacterState::ECS_Unequipped;
- ActionState = EActionState::EAS_EquippingWeapon;
- }
- else if (CanArm())
- {
- PlayeEquipMontage(FName("Equip"));
- CharacterState = ECharacterState::ECS_EquippedOneHandedWeapon;
- ActionState = EActionState::EAS_EquippingWeapon;
- }
- }
- }
- void ASlashCharacter::Attack()
- {
- if (CanAttack())
- {
- PlayAttackMontage();
- ActionState = EActionState::EAS_Attacking;
- }
- }
- bool ASlashCharacter::CanAttack()
- {
- return
- ActionState == EActionState::EAS_Unoccupied &&
- CharacterState != ECharacterState::ECS_Unequipped;
- }
- bool ASlashCharacter::CanDisarm()
- {
- return
- ActionState == EActionState::EAS_Unoccupied &&
- CharacterState != ECharacterState::ECS_Unequipped;
- }
- bool ASlashCharacter::CanArm()
- {
- return ActionState == EActionState::EAS_Unoccupied &&
- CharacterState == ECharacterState::ECS_Unequipped &&
- EquippedWeapon;
- }
- void ASlashCharacter::Disarm()
- {
- if (EquippedWeapon)
- {
- EquippedWeapon->AttachMeshToSocket(GetMesh(), FName("SpineSocket"));
- }
- }
- void ASlashCharacter::Arm()
- {
- if (EquippedWeapon)
- {
- EquippedWeapon->AttachMeshToSocket(GetMesh(), FName("RightHandSocket"));
- }
- }
- void ASlashCharacter::FinishEquipping()
- {
- ActionState = EActionState::EAS_Unoccupied;
- }
- void ASlashCharacter::PlayAttackMontage()
- {
- //UE_LOG(LogTemp, Warning, TEXT("Attack!"));
- UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
- if (AnimInstance && AttackMontage)
- {
- AnimInstance->Montage_Play(AttackMontage);
- const int32 Selection = FMath::RandRange(0, 1);
- FName SlectionName = FName();
- switch (Selection)
- {
- case 0:
- SlectionName = FName("Attack1");
- break;
- case 1:
- SlectionName = FName("Attack2");
- break;
- default:
- break;
- }
- AnimInstance->Montage_JumpToSection(SlectionName, AttackMontage);
- }
- }
- void ASlashCharacter::PlayeEquipMontage(const FName& SectionName)
- {
- UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
- if (AnimInstance && EquipMontage)
- {
- AnimInstance->Montage_Play(EquipMontage);
- AnimInstance->Montage_JumpToSection(SectionName, EquipMontage);
- }
- }
- void ASlashCharacter::AttackEnd()
- {
- ActionState = EActionState::EAS_Unoccupied;
- }
- void ASlashCharacter::Dodge()
- {
- UE_LOG(LogTemp, Warning, TEXT("Dodge!"));
- }
- // Called every frame
- void ASlashCharacter::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- }
- // Called to bind functionality to input
- void ASlashCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
- {
- EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Move);
- EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Look);
- EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Jump);
- EnhancedInputComponent->BindAction(EquipAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Equip);
- EnhancedInputComponent->BindAction(AttackAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Attack);
- EnhancedInputComponent->BindAction(DodgeAction, ETriggerEvent::Triggered, this, &ASlashCharacter::Dodge);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement