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; 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(); 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() void ABlasterCharacter::PlayHitReactMontage()
{ {
if (Combat == nullptr || Combat->PrimaryWeapon == nullptr) return; if (Combat == nullptr || Combat->PrimaryWeapon == nullptr) return;
@ -524,7 +533,18 @@ void ABlasterCharacter::EquipButtonPressed()
if (bDisableGameplay) return; if (bDisableGameplay) return;
if (Combat) 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 OnRep_ReplicatedMovement() override;
virtual void Destroyed() override; virtual void Destroyed() override;
void RotateInPlace(float DeltaTime); void RotateInPlace(float DeltaTime);
// Play Montages
void PlayFireMontage(bool bAiming); void PlayFireMontage(bool bAiming);
void PlayReloadMontage(); void PlayReloadMontage();
void PlayEliminatedMontage(); void PlayEliminatedMontage();
void PlayThrowGrenadeMontage(); void PlayThrowGrenadeMontage();
void PlaySwapMontage();
void Eliminated(); void Eliminated();
@ -48,7 +51,8 @@ public:
void SpawnDefaultWeapon(); void SpawnDefaultWeapon();
UPROPERTY() UPROPERTY()
TMap<FName, class UBoxComponent*> HitCollisionBoxes; TMap<FName, class UBoxComponent*> HitCollisionBoxes;
bool bFinishedSwapping = false;
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
@ -191,6 +195,9 @@ private:
UPROPERTY(EditAnywhere, Category = Combat) UPROPERTY(EditAnywhere, Category = Combat)
class UAnimMontage* ThrowGrenadeMontage; class UAnimMontage* ThrowGrenadeMontage;
UPROPERTY(EditAnywhere, Category = Combat)
class UAnimMontage* SwapMontage;
void HideCameraIfCharacterClose(); void HideCameraIfCharacterClose();
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)

View File

@ -241,20 +241,13 @@ void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
void UCombatComponent::SwapWeapons() 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;
PrimaryWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(PrimaryWeapon);
PrimaryWeapon->SetHUDAmmo();
UpdateCarriedAmmo();
PlayEquipWeaponSound(PrimaryWeapon);
SecondaryWeapon->SetWeaponState(EWeaponState::EWS_EquippedSecondary); Character->PlaySwapMontage();
AttachActorToBackpack(SecondaryWeapon); CombatState = ECombatState::ECS_SwappingWeapons;
Character->bFinishedSwapping = false;
if (SecondaryWeapon) SecondaryWeapon->EnableCustomDepth(false);
} }
bool UCombatComponent::ShouldSwapWeapons() 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() void UCombatComponent::DropWeapons()
{ {
if (PrimaryWeapon) PrimaryWeapon->Dropped(); if (PrimaryWeapon) PrimaryWeapon->Dropped();
@ -535,6 +554,12 @@ void UCombatComponent::OnRep_CombatState()
ShowAttachedGrenade(true); ShowAttachedGrenade(true);
} }
break; 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 == nullptr) return false;
if (PrimaryWeapon->IsEmpty()) return false; if (PrimaryWeapon->IsEmpty()) return false;
if (bLocallyReloading) return false;
if (!bCanFire) return false; if (!bCanFire) return false;
if (CombatState == ECombatState::ECS_Reloading && PrimaryWeapon->GetWeaponType() == EWeaponType::EWT_Shotgun) return true; if (CombatState == ECombatState::ECS_Reloading && PrimaryWeapon->GetWeaponType() == EWeaponType::EWT_Shotgun) return true;
if (bLocallyReloading) return false;
return CombatState == ECombatState::ECS_Unoccupied; return CombatState == ECombatState::ECS_Unoccupied;
} }

View File

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

View File

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

View File

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