201 - Swap Weapon Animation

This commit is contained in:
Kingsmedia 2022-05-28 12:16:32 +02:00
parent 9e64e70607
commit d6ce6fc25f
13 changed files with 80 additions and 18 deletions

Binary file not shown.

Binary file not shown.

View File

@ -74,7 +74,11 @@ void UBlasterAnimInstance::NativeUpdateAnimation(float DeltaTime)
}
bUseFABRIK = BlasterCharacter->GetCombatState() == ECombatState::ECS_Unoccupied;
if (BlasterCharacter->IsLocallyControlled() && BlasterCharacter->GetCombatState() != ECombatState::ECS_ThrowingGrenade)
bool bFABRIKOverride = BlasterCharacter->IsLocallyControlled() &&
BlasterCharacter->GetCombatState() != ECombatState::ECS_ThrowingGrenade &&
BlasterCharacter->bFinishedSwapping;
if (bFABRIKOverride)
{
bUseFABRIK = !BlasterCharacter->IsLocallyReloading();
}

View File

@ -434,6 +434,15 @@ void ABlasterCharacter::PlayThrowGrenadeMontage()
}
}
void ABlasterCharacter::PlaySwapMontage()
{
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && SwapMontage)
{
AnimInstance->Montage_Play(SwapMontage);
}
}
void ABlasterCharacter::PlayHitReactMontage()
{
if (Combat == nullptr || Combat->PrimaryWeapon == nullptr) return;
@ -524,7 +533,18 @@ void ABlasterCharacter::EquipButtonPressed()
if (bDisableGameplay) return;
if (Combat)
{
ServerEquipButtonPressed();
if (Combat->CombatState == ECombatState::ECS_Unoccupied) ServerEquipButtonPressed();
bool bSwap = Combat->ShouldSwapWeapons() &&
!HasAuthority() &&
Combat->CombatState == ECombatState::ECS_Unoccupied &&
OverlappingWeapon == nullptr;
if (bSwap)
{
PlaySwapMontage();
Combat->CombatState = ECombatState::ECS_SwappingWeapons;
bFinishedSwapping = false;
}
}
}

View File

@ -25,10 +25,13 @@ public:
virtual void OnRep_ReplicatedMovement() override;
virtual void Destroyed() override;
void RotateInPlace(float DeltaTime);
// Play Montages
void PlayFireMontage(bool bAiming);
void PlayReloadMontage();
void PlayEliminatedMontage();
void PlayThrowGrenadeMontage();
void PlaySwapMontage();
void Eliminated();
@ -49,6 +52,7 @@ public:
UPROPERTY()
TMap<FName, class UBoxComponent*> HitCollisionBoxes;
bool bFinishedSwapping = false;
protected:
virtual void BeginPlay() override;
@ -191,6 +195,9 @@ private:
UPROPERTY(EditAnywhere, Category = Combat)
class UAnimMontage* ThrowGrenadeMontage;
UPROPERTY(EditAnywhere, Category = Combat)
class UAnimMontage* SwapMontage;
void HideCameraIfCharacterClose();
UPROPERTY(EditAnywhere)

View File

@ -241,20 +241,13 @@ void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
void UCombatComponent::SwapWeapons()
{
if (CombatState != ECombatState::ECS_Unoccupied) return;
if (CombatState != ECombatState::ECS_Unoccupied || Character == nullptr || !Character->HasAuthority()) return;
AWeapon* TempWeapon = PrimaryWeapon;
PrimaryWeapon = SecondaryWeapon;
SecondaryWeapon = TempWeapon;
Character->PlaySwapMontage();
CombatState = ECombatState::ECS_SwappingWeapons;
Character->bFinishedSwapping = false;
PrimaryWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(PrimaryWeapon);
PrimaryWeapon->SetHUDAmmo();
UpdateCarriedAmmo();
PlayEquipWeaponSound(PrimaryWeapon);
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_EquippedSecondary);
AttachActorToBackpack(SecondaryWeapon);
if (SecondaryWeapon) SecondaryWeapon->EnableCustomDepth(false);
}
bool UCombatComponent::ShouldSwapWeapons()
@ -421,6 +414,32 @@ void UCombatComponent::FinishedReloading()
}
}
void UCombatComponent::FinishedSwap()
{
if (Character && Character->HasAuthority())
{
CombatState = ECombatState::ECS_Unoccupied;
}
if (Character) Character->bFinishedSwapping = true;
if (SecondaryWeapon) SecondaryWeapon->EnableCustomDepth(true);
}
void UCombatComponent::FinishedSwapAttachWeapons()
{
AWeapon* TempWeapon = PrimaryWeapon;
PrimaryWeapon = SecondaryWeapon;
SecondaryWeapon = TempWeapon;
PrimaryWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(PrimaryWeapon);
PrimaryWeapon->SetHUDAmmo();
UpdateCarriedAmmo();
PlayEquipWeaponSound(PrimaryWeapon);
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_EquippedSecondary);
AttachActorToBackpack(SecondaryWeapon);
}
void UCombatComponent::DropWeapons()
{
if (PrimaryWeapon) PrimaryWeapon->Dropped();
@ -535,6 +554,12 @@ void UCombatComponent::OnRep_CombatState()
ShowAttachedGrenade(true);
}
break;
case ECombatState::ECS_SwappingWeapons:
if (Character && !Character->IsLocallyControlled())
{
Character->PlaySwapMontage();
}
break;
}
}
@ -790,9 +815,9 @@ bool UCombatComponent::CanFire()
{
if (PrimaryWeapon == nullptr) return false;
if (PrimaryWeapon->IsEmpty()) return false;
if (bLocallyReloading) return false;
if (!bCanFire) return false;
if (CombatState == ECombatState::ECS_Reloading && PrimaryWeapon->GetWeaponType() == EWeaponType::EWT_Shotgun) return true;
if (bLocallyReloading) return false;
return CombatState == ECombatState::ECS_Unoccupied;
}

View File

@ -29,6 +29,12 @@ public:
UFUNCTION(BlueprintCallable)
void FinishedReloading();
UFUNCTION(BlueprintCallable)
void FinishedSwap();
UFUNCTION(BlueprintCallable)
void FinishedSwapAttachWeapons();
void FireButtonPressed(bool bPressed);
UFUNCTION(BlueprintCallable)

View File

@ -6,6 +6,7 @@ enum class ECombatState : uint8
ECS_Unoccupied UMETA(DisplayName = "Unoccupied"),
ECS_Reloading UMETA(DisplayName = "Reloading"),
ECS_ThrowingGrenade UMETA(DisplayName = "Throwing Grenade"),
ECS_SwappingWeapons UMETA(DisplayName = "Swapping Weapons"),
ECS_MAX UMETA(DisplayName = "DefaultMAX")
};

View File

@ -240,7 +240,6 @@ void AWeapon::OnEquippedSecondary()
WeaponMesh->SetEnableGravity(true);
WeaponMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
}
EnableCustomDepth(true);
if (WeaponMesh)
{
WeaponMesh->SetCustomDepthStencilValue(CUSTOM_DEPTH_TAN);