116 - Updating Ammo

This commit is contained in:
Kingsmedia 2022-05-10 00:00:06 +02:00
parent 6792b354c7
commit d7539845c6
4 changed files with 51 additions and 3 deletions

View File

@ -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<ABlasterPlayerController>(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)

View File

@ -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;

View File

@ -212,3 +212,9 @@ void AWeapon::Dropped()
OwnerCharacter = nullptr;
OwnerController = nullptr;
}
void AWeapon::AddAmmo(int32 Amount)
{
Ammo = FMath::Clamp(Ammo - Amount, 0, MagCapacity);
SetHUDAmmo();
}

View File

@ -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();
};