144 - Shotgun Reload

This commit is contained in:
Kingsmedia 2022-05-21 01:33:02 +02:00
parent d4afa9f956
commit 6fdfeef596
12 changed files with 84 additions and 4 deletions

Binary file not shown.

View File

@ -196,4 +196,5 @@ public:
ECombatState GetCombatState() const; ECombatState GetCombatState() const;
FORCEINLINE UCombatComponent* GetCombat() const { return Combat; } FORCEINLINE UCombatComponent* GetCombat() const { return Combat; }
FORCEINLINE bool GetDisableGameplay() const { return bDisableGameplay; } FORCEINLINE bool GetDisableGameplay() const { return bDisableGameplay; }
FORCEINLINE UAnimMontage* GetReloadMontage() const { return ReloadMontage; }
}; };

View File

@ -74,6 +74,14 @@ void UCombatComponent::FireButtonPressed(bool bPressed)
} }
} }
void UCombatComponent::ShotgunShellReload()
{
if (Character && Character->HasAuthority())
{
UpdateShotgunAmmoValues();
}
}
void UCombatComponent::Fire() void UCombatComponent::Fire()
{ {
if (CanFire()) if (CanFire())
@ -121,6 +129,13 @@ 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)
{
Character->PlayFireMontage(bAiming);
EquippedWeapon->Fire(TraceHitTarget);
CombatState = ECombatState::ECS_Unoccupied;
return;
}
if (Character && CombatState == ECombatState::ECS_Unoccupied) if (Character && CombatState == ECombatState::ECS_Unoccupied)
{ {
Character->PlayFireMontage(bAiming); Character->PlayFireMontage(bAiming);
@ -188,7 +203,7 @@ void UCombatComponent::ServerReload_Implementation()
// return if weapon mag is at max capacity // return if weapon mag is at max capacity
if (EquippedWeapon->GetAmmo() == EquippedWeapon->GetMagCapacity()) return; if (EquippedWeapon->GetAmmo() == EquippedWeapon->GetMagCapacity()) return;
CombatState = ECombatState::ECS_Reloading; CombatState = ECombatState::ECS_Reloading;
HandleReload(); HandleReload();
} }
@ -224,6 +239,38 @@ void UCombatComponent::UpdateAmmoValues()
EquippedWeapon->AddAmmo(-ReloadAmount); EquippedWeapon->AddAmmo(-ReloadAmount);
} }
void UCombatComponent::UpdateShotgunAmmoValues()
{
if (Character == nullptr || EquippedWeapon == nullptr) return;
if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType()))
{
CarriedAmmoMap[EquippedWeapon->GetWeaponType()] -= 1;
CarriedAmmo = CarriedAmmoMap[EquippedWeapon->GetWeaponType()];
}
Controller = Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
if (Controller)
{
Controller->SetHUDCarriedAmmo(CarriedAmmo);
}
EquippedWeapon->AddAmmo(-1);
bCanFire = true;
if (EquippedWeapon->IsFull() || CarriedAmmo == 0)
{
JumpToShotgunEnd();
}
}
void UCombatComponent::JumpToShotgunEnd()
{
// Jump to ShotgunEnd section
UAnimInstance* AnimInstance = Character->GetMesh()->GetAnimInstance();
if (AnimInstance && Character->GetReloadMontage())
{
AnimInstance->Montage_JumpToSection(FName("ShotgunEnd"));
}
}
void UCombatComponent::OnRep_CombatState() void UCombatComponent::OnRep_CombatState()
{ {
switch (CombatState) switch (CombatState)
@ -425,7 +472,7 @@ void UCombatComponent::SetAiming(bool bIsAiming)
ServerSetAiming(bIsAiming); ServerSetAiming(bIsAiming);
Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed; Character->GetCharacterMovement()->MaxWalkSpeed = bIsAiming ? AimWalkSpeed : BaseWalkSpeed;
if (Character->IsLocallyControlled() && EquippedWeapon->GetWeaponType() == EWeaponType::EWT_SniperRifle) if (Character->IsLocallyControlled() && EquippedWeapon->GetWeaponType() == EWeaponType::EWT_SniperRifle)
{ {
Character->ShowSniperScopeWidget(bIsAiming); Character->ShowSniperScopeWidget(bIsAiming);
@ -444,7 +491,10 @@ void UCombatComponent::ServerSetAiming_Implementation(bool bIsAiming)
bool UCombatComponent::CanFire() bool UCombatComponent::CanFire()
{ {
if (EquippedWeapon == nullptr) return false; if (EquippedWeapon == nullptr) return false;
return !EquippedWeapon->IsEmpty() && bCanFire && CombatState == ECombatState::ECS_Unoccupied; if (EquippedWeapon->IsEmpty()) return false;
if (!bCanFire) return false;
if (CombatState == ECombatState::ECS_Reloading && EquippedWeapon->GetWeaponType() == EWeaponType::EWT_Shotgun) return true;
return CombatState == ECombatState::ECS_Unoccupied;
} }
void UCombatComponent::OnRep_CarriedAmmo() void UCombatComponent::OnRep_CarriedAmmo()
@ -454,6 +504,15 @@ void UCombatComponent::OnRep_CarriedAmmo()
{ {
Controller->SetHUDCarriedAmmo(CarriedAmmo); Controller->SetHUDCarriedAmmo(CarriedAmmo);
} }
bool bJumpToShotgunEnd = CombatState == ECombatState::ECS_Reloading &&
EquippedWeapon != nullptr &&
EquippedWeapon->GetWeaponType() == EWeaponType::EWT_Shotgun &&
CarriedAmmo == 0;
if (bJumpToShotgunEnd)
{
JumpToShotgunEnd();
}
} }
void UCombatComponent::InitializeCarriedAmmo() void UCombatComponent::InitializeCarriedAmmo()

View File

@ -25,9 +25,14 @@ public:
void Reload(); void Reload();
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void FinishedReloading(); void FinishedReloading();
void UpdateAmmoValues();
void FireButtonPressed(bool bPressed); void FireButtonPressed(bool bPressed);
UFUNCTION(BlueprintCallable)
void ShotgunShellReload();
void JumpToShotgunEnd();
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
void SetAiming(bool bIsAiming); void SetAiming(bool bIsAiming);
@ -146,4 +151,7 @@ private:
UFUNCTION() UFUNCTION()
void OnRep_CombatState(); void OnRep_CombatState();
void UpdateAmmoValues();
void UpdateShotgunAmmoValues();
}; };

View File

@ -5,6 +5,7 @@
#include "Casing.h" #include "Casing.h"
#include "Blaster/Character/BlasterCharacter.h" #include "Blaster/Character/BlasterCharacter.h"
#include "Blaster/Components/CombatComponent.h"
#include "Blaster/PlayerController/BlasterPlayerController.h" #include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Components/SphereComponent.h" #include "Components/SphereComponent.h"
#include "Components/WidgetComponent.h" #include "Components/WidgetComponent.h"
@ -119,6 +120,11 @@ void AWeapon::SpendRound()
void AWeapon::OnRep_Ammo() void AWeapon::OnRep_Ammo()
{ {
OwnerCharacter = OwnerCharacter == nullptr ? Cast<ABlasterCharacter>(GetOwner()) : OwnerCharacter;
if (OwnerCharacter && OwnerCharacter->GetCombat() && IsFull())
{
OwnerCharacter->GetCombat()->JumpToShotgunEnd();
}
SetHUDAmmo(); SetHUDAmmo();
} }
@ -160,6 +166,11 @@ bool AWeapon::IsEmpty()
return Ammo <= 0; return Ammo <= 0;
} }
bool AWeapon::IsFull()
{
return Ammo == MagCapacity;
}
void AWeapon::OnRep_WeaponState() void AWeapon::OnRep_WeaponState()
{ {
switch (WeaponState) switch (WeaponState)

View File

@ -140,4 +140,5 @@ public:
FORCEINLINE int32 GetAmmo() const { return Ammo; } FORCEINLINE int32 GetAmmo() const { return Ammo; }
FORCEINLINE int32 GetMagCapacity() const { return MagCapacity; } FORCEINLINE int32 GetMagCapacity() const { return MagCapacity; }
bool IsEmpty(); bool IsEmpty();
bool IsFull();
}; };