diff --git a/Source/Blaster/Components/CombatComponent.cpp b/Source/Blaster/Components/CombatComponent.cpp index 545cb07..5f4e2ca 100644 --- a/Source/Blaster/Components/CombatComponent.cpp +++ b/Source/Blaster/Components/CombatComponent.cpp @@ -343,7 +343,8 @@ void UCombatComponent::FinishedReloading() if (Character == nullptr) return; if (Character->HasAuthority()) { - CombatState = ECombatState::ECS_Unoccupied;; + CombatState = ECombatState::ECS_Unoccupied; + UpdateAmmoValues(); } if (bFireButtonPressed) { @@ -351,10 +352,30 @@ void UCombatComponent::FinishedReloading() } } +void UCombatComponent::UpdateAmmoValues() +{ + if (Character == nullptr || EquippedWeapon == nullptr) return; + const int32 ReloadAmount = AmountToReload(); + if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType())) + { + CarriedAmmoMap[EquippedWeapon->GetWeaponType()] -= ReloadAmount; + CarriedAmmo = CarriedAmmoMap[EquippedWeapon->GetWeaponType()]; + } + Controller = Controller == nullptr ? Cast(Character->Controller) : Controller; + if (Controller) + { + Controller->SetHUDCarriedAmmo(CarriedAmmo); + } + EquippedWeapon->AddAmmo(-ReloadAmount); +} + void UCombatComponent::ServerReload_Implementation() { - if (Character == nullptr) return; + if (Character == nullptr || EquippedWeapon == nullptr) return; + // return if weapon mag is at max capacity + if (EquippedWeapon->GetAmmo() == EquippedWeapon->GetMagCapacity()) return; + CombatState = ECombatState::ECS_Reloading; HandleReload(); } @@ -380,6 +401,22 @@ void UCombatComponent::HandleReload() Character->PlayReloadMontage(); } +int32 UCombatComponent::AmountToReload() +{ + if (EquippedWeapon == nullptr) return 0; + + const int32 RoomInMag = EquippedWeapon->GetMagCapacity() - EquippedWeapon->GetAmmo(); + + if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType())) + { + const int32 AmountCarried = CarriedAmmoMap[EquippedWeapon->GetWeaponType()]; + const int32 Least = FMath::Min(RoomInMag, AmountCarried); + return FMath::Clamp(RoomInMag, 0, Least); + } + + return 0; +} + void UCombatComponent::OnRep_EquippedWeapon() { if (EquippedWeapon && Character) diff --git a/Source/Blaster/Components/CombatComponent.h b/Source/Blaster/Components/CombatComponent.h index c51aad0..3095fcd 100644 --- a/Source/Blaster/Components/CombatComponent.h +++ b/Source/Blaster/Components/CombatComponent.h @@ -27,7 +27,8 @@ public: void Reload(); UFUNCTION(BlueprintCallable) void FinishedReloading(); - + void UpdateAmmoValues(); + protected: virtual void BeginPlay() override; void SetAiming(bool bIsAiming); @@ -55,6 +56,7 @@ protected: void ServerReload(); void HandleReload(); + int32 AmountToReload(); private: UPROPERTY() class ABlasterCharacter* Character; diff --git a/Source/Blaster/Weapon/Weapon.cpp b/Source/Blaster/Weapon/Weapon.cpp index 2a8b863..3550513 100644 --- a/Source/Blaster/Weapon/Weapon.cpp +++ b/Source/Blaster/Weapon/Weapon.cpp @@ -212,3 +212,9 @@ void AWeapon::Dropped() OwnerCharacter = nullptr; OwnerController = nullptr; } + +void AWeapon::AddAmmo(int32 Amount) +{ + Ammo = FMath::Clamp(Ammo - Amount, 0, MagCapacity); + SetHUDAmmo(); +} diff --git a/Source/Blaster/Weapon/Weapon.h b/Source/Blaster/Weapon/Weapon.h index c6d2fbb..de06f80 100644 --- a/Source/Blaster/Weapon/Weapon.h +++ b/Source/Blaster/Weapon/Weapon.h @@ -30,6 +30,7 @@ public: void ShowPickupWidget(bool bShowWidget); virtual void Fire(const FVector& HitTarget); void Dropped(); + void AddAmmo(int32 Amount); // Textures for the weapon crosshairs @@ -133,5 +134,7 @@ public: FORCEINLINE float GetZoomedFOV() const { return ZoomedFOV; }; FORCEINLINE float GetZoomInterpSpeed() const { return ZoomInterpSpeed; }; FORCEINLINE EWeaponType GetWeaponType() const { return WeaponType; } + FORCEINLINE int32 GetAmmo() const { return Ammo; } + FORCEINLINE int32 GetMagCapacity() const { return MagCapacity; } bool IsEmpty(); };