blaster/Source/Blaster/Character/BlasterAnimInstance.cpp

88 lines
3.8 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterAnimInstance.h"
#include "BlasterCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "Blaster/Weapon/Weapon.h"
#include "Blaster/Types/CombatState.h"
void UBlasterAnimInstance::NativeInitializeAnimation()
{
Super::NativeInitializeAnimation();
BlasterCharacter = Cast<ABlasterCharacter>(TryGetPawnOwner());
}
void UBlasterAnimInstance::NativeUpdateAnimation(float DeltaTime)
{
Super::NativeUpdateAnimation(DeltaTime);
if (BlasterCharacter == nullptr)
{
BlasterCharacter = Cast<ABlasterCharacter>(TryGetPawnOwner());
}
if (BlasterCharacter == nullptr) return;
FVector Velocity = BlasterCharacter->GetVelocity();
Velocity.Z = 0.f;
Speed = Velocity.Size();
bIsInAir = BlasterCharacter->GetCharacterMovement()->IsFalling();
bIsAccelerating = BlasterCharacter->GetCharacterMovement()->GetCurrentAcceleration().Size() > 0.f ? true : false;
bWeaponEquipped = BlasterCharacter->IsWeaponEquipped();
EquippedWeapon = BlasterCharacter->GetEquippedWeapon();
bIsCrouched = BlasterCharacter->bIsCrouched;
bAiming = BlasterCharacter->IsAiming();
TurningInPlace = BlasterCharacter->GetTurningInPlace();
bRotateRootBone = BlasterCharacter->ShouldRotateRootBone();
bEliminated = BlasterCharacter->IsEliminated();
// Offset Yaw for Strafing
FRotator AimRotation = BlasterCharacter->GetBaseAimRotation();
FRotator MovementRotation = UKismetMathLibrary::MakeRotFromX(BlasterCharacter->GetVelocity());
FRotator DeltaRot = UKismetMathLibrary::NormalizedDeltaRotator(MovementRotation, AimRotation);
DeltaRotation = FMath::RInterpTo(DeltaRotation, DeltaRot, DeltaTime, 6.f);
YawOffset = DeltaRotation.Yaw;
CharacterRotationLastFrame = CharacterRotation;
CharacterRotation = BlasterCharacter->GetActorRotation();
const FRotator Delta = UKismetMathLibrary::NormalizedDeltaRotator(CharacterRotation, CharacterRotationLastFrame);
const float Target = Delta.Yaw / DeltaTime;
const float Interp = FMath::FInterpTo(Lean, Target, DeltaTime, 6.f);
Lean = FMath::Clamp(Interp, -90.f, 90.f);
AO_Yaw = BlasterCharacter->GetAO_Yaw();
AO_Pitch = BlasterCharacter->GetAO_Pitch();
if (bWeaponEquipped && EquippedWeapon && EquippedWeapon->GetWeaponMesh() && BlasterCharacter->GetMesh())
{
LeftHandTransform = EquippedWeapon->GetWeaponMesh()->GetSocketTransform(FName("LeftHandSocket"), ERelativeTransformSpace::RTS_World);
FVector OutPosition;
FRotator OutRotation;
BlasterCharacter->GetMesh()->TransformToBoneSpace(FName("hand_r"), LeftHandTransform.GetLocation(), FRotator::ZeroRotator, OutPosition, OutRotation);
LeftHandTransform.SetLocation(OutPosition);
LeftHandTransform.SetRotation(FQuat(OutRotation));
if (BlasterCharacter->IsLocallyControlled())
{
bLocallyControlled = true;
FTransform RightHandTransform = EquippedWeapon->GetWeaponMesh()->GetSocketTransform(FName("Hand_R"), ERelativeTransformSpace::RTS_World);
FRotator LookAtRotation = UKismetMathLibrary::FindLookAtRotation(RightHandTransform.GetLocation(), RightHandTransform.GetLocation() + (RightHandTransform.GetLocation() - BlasterCharacter->GetHitTarget()));
RightHandRotation = FMath::RInterpTo(RightHandRotation, LookAtRotation, DeltaTime, 30.f);
}
}
bUseFABRIK = BlasterCharacter->GetCombatState() == ECombatState::ECS_Unoccupied;
bool bFABRIKOverride = BlasterCharacter->IsLocallyControlled() &&
BlasterCharacter->GetCombatState() != ECombatState::ECS_ThrowingGrenade &&
BlasterCharacter->bFinishedSwapping;
if (bFABRIKOverride)
{
bUseFABRIK = !BlasterCharacter->IsLocallyReloading();
}
bUseAimOffsets = BlasterCharacter->GetCombatState() == ECombatState::ECS_Unoccupied && !BlasterCharacter->GetDisableGameplay();
bTransformRightHand = BlasterCharacter->GetCombatState() == ECombatState::ECS_Unoccupied && !BlasterCharacter->GetDisableGameplay();
}