147 - Weapon Attachment while Throwing Grenades

This commit is contained in:
Kingsmedia 2022-05-21 15:40:49 +02:00
parent ecf950ba75
commit c5189e22ab
9 changed files with 79 additions and 40 deletions

View File

@ -137,7 +137,7 @@ void ABlasterCharacter::MulticastEliminated_Implementation()
GetActorLocation() GetActorLocation()
); );
} }
if (IsLocallyControlled() && GetEquippedWeapon() && GetEquippedWeapon()->GetWeaponType() == EWeaponType::EWT_SniperRifle && IsAiming()) if (IsLocallyControlled() && GetEquippedWeapon() && GetEquippedWeapon()->IsSniper() && IsAiming())
{ {
ShowSniperScopeWidget(false); ShowSniperScopeWidget(false);
} }

View File

@ -115,10 +115,7 @@ void UCombatComponent::FireTimerFinished()
{ {
Fire(); Fire();
} }
if (EquippedWeapon->IsEmpty()) ReloadEmptyWeapon();
{
Reload();
}
} }
void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize& TraceHitTarget) void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize& TraceHitTarget)
@ -129,7 +126,7 @@ void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize& Trac
void UCombatComponent::MulticastFire_Implementation(const FVector_NetQuantize& TraceHitTarget) void UCombatComponent::MulticastFire_Implementation(const FVector_NetQuantize& TraceHitTarget)
{ {
if (EquippedWeapon == nullptr) return; if (EquippedWeapon == nullptr) return;
if (Character && CombatState == ECombatState::ECS_Reloading && EquippedWeapon->GetWeaponType() == EWeaponType::EWT_Shotgun) if (Character && CombatState == ECombatState::ECS_Reloading && EquippedWeapon->IsShotgun())
{ {
Character->PlayFireMontage(bAiming); Character->PlayFireMontage(bAiming);
EquippedWeapon->Fire(TraceHitTarget); EquippedWeapon->Fire(TraceHitTarget);
@ -147,20 +144,54 @@ void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
{ {
if (Character == nullptr || WeaponToEquip == nullptr) return; if (Character == nullptr || WeaponToEquip == nullptr) return;
if (CombatState != ECombatState::ECS_Unoccupied) return; if (CombatState != ECombatState::ECS_Unoccupied) return;
DropEquippedWeapon();
EquippedWeapon = WeaponToEquip;
EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
AttachActorToRightHand(EquippedWeapon);
EquippedWeapon->SetOwner(Character);
EquippedWeapon->SetHUDAmmo();
UpdateCarriedAmmo();
PlayEquipWeaponSound();
ReloadEmptyWeapon();
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
}
void UCombatComponent::DropEquippedWeapon()
{
if (EquippedWeapon) if (EquippedWeapon)
{ {
EquippedWeapon->Dropped(); EquippedWeapon->Dropped();
} }
EquippedWeapon = WeaponToEquip; }
EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
void UCombatComponent::AttachActorToRightHand(AActor* ActorToAttach)
{
if (Character == nullptr || Character->GetMesh() == nullptr|| ActorToAttach == nullptr) return;
const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(FName("RightHandSocket")); const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(FName("RightHandSocket"));
if (HandSocket) if (HandSocket)
{ {
HandSocket->AttachActor(EquippedWeapon, Character->GetMesh()); HandSocket->AttachActor(ActorToAttach, Character->GetMesh());
} }
EquippedWeapon->SetOwner(Character); }
EquippedWeapon->SetHUDAmmo();
void UCombatComponent::AttachActorToLeftHand(AActor* ActorToAttach)
{
if (Character == nullptr || Character->GetMesh() == nullptr || ActorToAttach == nullptr || EquippedWeapon == nullptr) return;
bool bUsePistolSocket = EquippedWeapon->IsPistol() || EquippedWeapon->IsSMG();
FName SocketName = bUsePistolSocket ? FName("LeftHandPistolSocket") : FName("LeftHandSocket");
const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(SocketName);
if (HandSocket)
{
HandSocket->AttachActor(ActorToAttach, Character->GetMesh());
}
}
void UCombatComponent::UpdateCarriedAmmo()
{
if (EquippedWeapon == nullptr) return;
if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType())) if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType()))
{ {
CarriedAmmo = CarriedAmmoMap[EquippedWeapon->GetWeaponType()]; CarriedAmmo = CarriedAmmoMap[EquippedWeapon->GetWeaponType()];
@ -171,8 +202,11 @@ void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
{ {
Controller->SetHUDCarriedAmmo(CarriedAmmo); Controller->SetHUDCarriedAmmo(CarriedAmmo);
} }
}
if (EquippedWeapon->EquipSound) void UCombatComponent::PlayEquipWeaponSound()
{
if (Character && EquippedWeapon && EquippedWeapon->EquipSound)
{ {
UGameplayStatics::PlaySoundAtLocation( UGameplayStatics::PlaySoundAtLocation(
this, this,
@ -180,14 +214,14 @@ void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
Character->GetActorLocation() Character->GetActorLocation()
); );
} }
}
if (EquippedWeapon->IsEmpty()) void UCombatComponent::ReloadEmptyWeapon()
{
if (EquippedWeapon && EquippedWeapon->IsEmpty())
{ {
Reload(); Reload();
} }
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
} }
void UCombatComponent::Reload() void UCombatComponent::Reload()
@ -275,6 +309,7 @@ void UCombatComponent::JumpToShotgunEnd()
void UCombatComponent::ThrowGrenadeFinished() void UCombatComponent::ThrowGrenadeFinished()
{ {
CombatState = ECombatState::ECS_Unoccupied; CombatState = ECombatState::ECS_Unoccupied;
AttachActorToRightHand(EquippedWeapon);
} }
void UCombatComponent::OnRep_CombatState() void UCombatComponent::OnRep_CombatState()
@ -294,6 +329,7 @@ void UCombatComponent::OnRep_CombatState()
if (Character && !Character->IsLocallyControlled()) if (Character && !Character->IsLocallyControlled())
{ {
Character->PlayThrowGrenadeMontage(); Character->PlayThrowGrenadeMontage();
AttachActorToLeftHand(EquippedWeapon);
} }
break; break;
} }
@ -321,14 +357,12 @@ int32 UCombatComponent::AmountToReload()
void UCombatComponent::ThrowGrenade() void UCombatComponent::ThrowGrenade()
{ {
if (CombatState != ECombatState::ECS_Unoccupied) return; if (CombatState != ECombatState::ECS_Unoccupied) return;
CombatState = ECombatState::ECS_ThrowingGrenade; CombatState = ECombatState::ECS_ThrowingGrenade;
if (Character) if (Character)
{ {
Character->PlayThrowGrenadeMontage(); Character->PlayThrowGrenadeMontage();
AttachActorToLeftHand(EquippedWeapon);
} }
if (Character && !Character->HasAuthority()) if (Character && !Character->HasAuthority())
{ {
ServerThrowGrenade(); ServerThrowGrenade();
@ -338,10 +372,10 @@ void UCombatComponent::ThrowGrenade()
void UCombatComponent::ServerThrowGrenade_Implementation() void UCombatComponent::ServerThrowGrenade_Implementation()
{ {
CombatState = ECombatState::ECS_ThrowingGrenade; CombatState = ECombatState::ECS_ThrowingGrenade;
if (Character) if (Character)
{ {
Character->PlayThrowGrenadeMontage(); Character->PlayThrowGrenadeMontage();
AttachActorToLeftHand(EquippedWeapon);
} }
} }
@ -350,22 +384,10 @@ void UCombatComponent::OnRep_EquippedWeapon()
if (EquippedWeapon && Character) if (EquippedWeapon && Character)
{ {
EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped); EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped);
const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(FName("RightHandSocket")); AttachActorToRightHand(EquippedWeapon);
if (HandSocket)
{
HandSocket->AttachActor(EquippedWeapon, Character->GetMesh());
}
Character->GetCharacterMovement()->bOrientRotationToMovement = false; Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true; Character->bUseControllerRotationYaw = true;
PlayEquipWeaponSound();
if (EquippedWeapon->EquipSound)
{
UGameplayStatics::PlaySoundAtLocation(
this,
EquippedWeapon->EquipSound,
Character->GetActorLocation()
);
}
} }
} }
@ -509,10 +531,11 @@ void UCombatComponent::SetAiming(bool bIsAiming)
if (Character == nullptr || EquippedWeapon == nullptr) return; if (Character == nullptr || EquippedWeapon == nullptr) return;
bAiming = bIsAiming; bAiming = bIsAiming;
ServerSetAiming(bIsAiming); ServerSetAiming(bIsAiming);
if (Character)
Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed; {
Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed;
if (Character->IsLocallyControlled() && EquippedWeapon->GetWeaponType() == EWeaponType::EWT_SniperRifle) }
if (Character->IsLocallyControlled() && EquippedWeapon->IsSniper())
{ {
Character->ShowSniperScopeWidget(bIsAiming); Character->ShowSniperScopeWidget(bIsAiming);
} }

View File

@ -67,6 +67,13 @@ protected:
UFUNCTION(Server, Reliable) UFUNCTION(Server, Reliable)
void ServerThrowGrenade(); void ServerThrowGrenade();
void DropEquippedWeapon();
void AttachActorToRightHand(AActor* ActorToAttach);
void AttachActorToLeftHand(AActor* ActorToAttach);
void UpdateCarriedAmmo();
void PlayEquipWeaponSound();
void ReloadEmptyWeapon();
private: private:
UPROPERTY() UPROPERTY()
class ABlasterCharacter* Character; class ABlasterCharacter* Character;

View File

@ -151,7 +151,7 @@ void AWeapon::SetWeaponState(EWeaponState State)
WeaponMesh->SetSimulatePhysics(false); WeaponMesh->SetSimulatePhysics(false);
WeaponMesh->SetEnableGravity(false); WeaponMesh->SetEnableGravity(false);
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision); WeaponMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
if (WeaponType == EWeaponType::EWT_SubmachineGun) if (IsSMG())
{ {
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); WeaponMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
WeaponMesh->SetEnableGravity(true); WeaponMesh->SetEnableGravity(true);
@ -196,7 +196,7 @@ void AWeapon::OnRep_WeaponState()
WeaponMesh->SetSimulatePhysics(false); WeaponMesh->SetSimulatePhysics(false);
WeaponMesh->SetEnableGravity(false); WeaponMesh->SetEnableGravity(false);
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision); WeaponMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
if (WeaponType == EWeaponType::EWT_SubmachineGun) if (IsSMG())
{ {
WeaponMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); WeaponMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
WeaponMesh->SetEnableGravity(true); WeaponMesh->SetEnableGravity(true);

View File

@ -144,4 +144,13 @@ public:
FORCEINLINE int32 GetMagCapacity() const { return MagCapacity; } FORCEINLINE int32 GetMagCapacity() const { return MagCapacity; }
bool IsEmpty(); bool IsEmpty();
bool IsFull(); bool IsFull();
// Convenience methods for WeaponType
FORCEINLINE bool IsPistol() const { return WeaponType == EWeaponType::EWT_Pistol; }
FORCEINLINE bool IsSMG() const { return WeaponType == EWeaponType::EWT_SubmachineGun; }
FORCEINLINE bool IsAR() const { return WeaponType == EWeaponType::EWT_AssaultRifle; }
FORCEINLINE bool IsShotgun() const { return WeaponType == EWeaponType::EWT_Shotgun; }
FORCEINLINE bool IsGrenadeLauncher() const { return WeaponType == EWeaponType::EWT_GrenadeLauncher; }
FORCEINLINE bool IsRocketLauncher() const { return WeaponType == EWeaponType::EWT_RocketLauncher; }
FORCEINLINE bool IsSniper() const { return WeaponType == EWeaponType::EWT_SniperRifle; }
}; };