177 - Client-Side Predicting Ammo

This commit is contained in:
Kingsmedia 2022-05-26 16:37:53 +02:00
parent 07fb4c4e5b
commit 41a200110b
3 changed files with 42 additions and 19 deletions

View File

@ -438,7 +438,7 @@ void UCombatComponent::UpdateAmmoValues()
{ {
Controller->SetHUDCarriedAmmo(CarriedAmmo); Controller->SetHUDCarriedAmmo(CarriedAmmo);
} }
PrimaryWeapon->AddAmmo(-ReloadAmount); PrimaryWeapon->AddAmmo(ReloadAmount);
} }
void UCombatComponent::UpdateShotgunAmmoValues() void UCombatComponent::UpdateShotgunAmmoValues()
@ -455,7 +455,7 @@ void UCombatComponent::UpdateShotgunAmmoValues()
{ {
Controller->SetHUDCarriedAmmo(CarriedAmmo); Controller->SetHUDCarriedAmmo(CarriedAmmo);
} }
PrimaryWeapon->AddAmmo(-1); PrimaryWeapon->AddAmmo(1);
bCanFire = true; bCanFire = true;
if (PrimaryWeapon->IsFull() || CarriedAmmo == 0) if (PrimaryWeapon->IsFull() || CarriedAmmo == 0)
{ {

View File

@ -45,7 +45,6 @@ void AWeapon::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeP
Super::GetLifetimeReplicatedProps(OutLifetimeProps); Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AWeapon, WeaponState); DOREPLIFETIME(AWeapon, WeaponState);
DOREPLIFETIME(AWeapon, Ammo);
} }
void AWeapon::EnableCustomDepth(bool bEnabled) void AWeapon::EnableCustomDepth(bool bEnabled)
@ -131,15 +130,41 @@ void AWeapon::SpendRound()
{ {
Ammo = FMath::Clamp(Ammo - 1, 0, MagCapacity); Ammo = FMath::Clamp(Ammo - 1, 0, MagCapacity);
SetHUDAmmo(); SetHUDAmmo();
if (HasAuthority())
{
ClientUpdateAmmo(Ammo);
}
else
{
++Sequence;
}
} }
void AWeapon::OnRep_Ammo() void AWeapon::ClientUpdateAmmo_Implementation(int32 ServerAmmo)
{ {
if (HasAuthority()) return;
Ammo = ServerAmmo;
--Sequence;
Ammo -= Sequence;
SetHUDAmmo();
}
void AWeapon::AddAmmo(int32 AmmoToAdd)
{
Ammo = FMath::Clamp(Ammo + AmmoToAdd, 0, MagCapacity);
SetHUDAmmo();
ClientAddAmmo(AmmoToAdd);
}
void AWeapon::ClientAddAmmo_Implementation(int32 AmmoToAdd)
{
if (HasAuthority()) return;
Ammo = FMath::Clamp(Ammo + AmmoToAdd, 0, MagCapacity);
OwnerCharacter = OwnerCharacter == nullptr ? Cast<ABlasterCharacter>(GetOwner()) : OwnerCharacter; OwnerCharacter = OwnerCharacter == nullptr ? Cast<ABlasterCharacter>(GetOwner()) : OwnerCharacter;
if (OwnerCharacter && OwnerCharacter->GetCombat() && IsFull()) if (OwnerCharacter && OwnerCharacter->GetCombat() && IsFull())
{ {
OwnerCharacter->GetCombat()->JumpToShotgunEnd(); OwnerCharacter->GetCombat()->JumpToShotgunEnd();
} }
SetHUDAmmo(); SetHUDAmmo();
} }
@ -268,10 +293,7 @@ void AWeapon::Fire(const FVector& HitTarget)
} }
} }
} }
if (HasAuthority()) SpendRound();
{
SpendRound();
}
} }
void AWeapon::Dropped() void AWeapon::Dropped()
@ -288,12 +310,6 @@ void AWeapon::Dropped()
OwnerController = nullptr; OwnerController = nullptr;
} }
void AWeapon::AddAmmo(int32 Amount)
{
Ammo = FMath::Clamp(Ammo - Amount, 0, MagCapacity);
SetHUDAmmo();
}
FVector AWeapon::TraceEndWithScatter(const FVector& HitTarget) FVector AWeapon::TraceEndWithScatter(const FVector& HitTarget)
{ {
const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash"); const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash");

View File

@ -41,7 +41,7 @@ public:
void ShowPickupWidget(bool bShowWidget); void ShowPickupWidget(bool bShowWidget);
virtual void Fire(const FVector& HitTarget); virtual void Fire(const FVector& HitTarget);
void Dropped(); void Dropped();
void AddAmmo(int32 Amount); void AddAmmo(int32 AmmoToAdd);
FVector TraceEndWithScatter(const FVector& HitTarget); FVector TraceEndWithScatter(const FVector& HitTarget);
// Textures for the weapon crosshairs // Textures for the weapon crosshairs
@ -146,17 +146,24 @@ private:
UPROPERTY(EditAnywhere, Category = "Weapon Properties") UPROPERTY(EditAnywhere, Category = "Weapon Properties")
TSubclassOf<class ACasing> CasingClass; TSubclassOf<class ACasing> CasingClass;
UPROPERTY(EditAnywhere, ReplicatedUsing=OnRep_Ammo) UPROPERTY(EditAnywhere)
int32 Ammo; int32 Ammo;
UFUNCTION() UFUNCTION(Client, Reliable) // Server to Client RPC
void OnRep_Ammo(); void ClientUpdateAmmo(int32 ServerAmmo);
UFUNCTION(Client, Reliable) // Server to Client RPC
void ClientAddAmmo(int32 AmmoToAdd);
void SpendRound(); void SpendRound();
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
int32 MagCapacity; int32 MagCapacity;
// The number of unprocessed server requests for ammo
// Incremented in SpendRound(), decremented in ClientUpdateAmmo()
int32 Sequence = 0;
UPROPERTY() UPROPERTY()
class ABlasterCharacter* OwnerCharacter; class ABlasterCharacter* OwnerCharacter;