From 9ee8f0dfac6df90483128ab2a3164a4577a4f2ba Mon Sep 17 00:00:00 2001 From: in0finite Date: Sun, 20 Dec 2020 22:27:29 +0100 Subject: [PATCH] implement reloading time - all weapons have it set to 0, except rockets --- Assets/Prefabs/GameManager.prefab | 1 + Assets/Scripts/Behaviours/Weapon.cs | 5 ++++ Assets/Scripts/Behaviours/WeaponHolder.cs | 26 ++++++++++++++++--- .../Behaviours/Weapons/WeaponsManager.cs | 1 + 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Assets/Prefabs/GameManager.prefab b/Assets/Prefabs/GameManager.prefab index 07801a6c..ae065bad 100644 --- a/Assets/Prefabs/GameManager.prefab +++ b/Assets/Prefabs/GameManager.prefab @@ -128,6 +128,7 @@ MonoBehaviour: drawLineFromGun: 0 projectilePrefab: {fileID: 8038540346792760480, guid: 858b1226127ef9f4e8d16c9610e691b6, type: 3} + projectileReloadTime: 2.5 --- !u!114 &114529712737653322 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Behaviours/Weapon.cs b/Assets/Scripts/Behaviours/Weapon.cs index 1cba51e9..ae8feef5 100644 --- a/Assets/Scripts/Behaviours/Weapon.cs +++ b/Assets/Scripts/Behaviours/Weapon.cs @@ -369,7 +369,10 @@ namespace SanAndreasUnity.Behaviours this.FiresProjectile = s_weaponsUsingProjectile.Contains(this.Data.modelId1); if (this.FiresProjectile) + { this.ProjectilePrefab = WeaponsSettings.projectilePrefab; + this.ReloadTime = WeaponsSettings.projectileReloadTime; + } } @@ -524,6 +527,8 @@ namespace SanAndreasUnity.Behaviours public GameObject ProjectilePrefab { get; set; } + public float ReloadTime { get; set; } = 0; + // TODO: this function should be removed, and new one should be created: OnAnimsUpdated public virtual void UpdateAnimWhileHolding () diff --git a/Assets/Scripts/Behaviours/WeaponHolder.cs b/Assets/Scripts/Behaviours/WeaponHolder.cs index c7a7d1d5..966f9ab8 100644 --- a/Assets/Scripts/Behaviours/WeaponHolder.cs +++ b/Assets/Scripts/Behaviours/WeaponHolder.cs @@ -110,6 +110,9 @@ namespace SanAndreasUnity.Behaviours { public bool rotatePlayerInDirectionOfAiming = true; + private float m_timeSinceStartedReloading = 0; + + void Awake () { @@ -161,17 +164,31 @@ namespace SanAndreasUnity.Behaviours { } // reload weapon ammo clip + + bool isReloadingNow = false; + if (NetStatus.IsServer) { if (CurrentWeapon != null) { - if (CurrentWeapon.AmmoClipSize > 0 && CurrentWeapon.AmmoInClip <= 0) { - int amountToRefill = Mathf.Min (CurrentWeapon.AmmoClipSize, CurrentWeapon.AmmoOutsideOfClip); - CurrentWeapon.AmmoInClip = amountToRefill; - CurrentWeapon.AmmoOutsideOfClip -= amountToRefill; + if (CurrentWeapon.AmmoClipSize > 0 && CurrentWeapon.AmmoInClip <= 0 && CurrentWeapon.AmmoOutsideOfClip > 0) + { + isReloadingNow = true; + m_timeSinceStartedReloading += Time.deltaTime; + if (m_timeSinceStartedReloading >= CurrentWeapon.ReloadTime) + { + m_timeSinceStartedReloading = 0; + + int amountToRefill = Mathf.Min(CurrentWeapon.AmmoClipSize, CurrentWeapon.AmmoOutsideOfClip); + CurrentWeapon.AmmoInClip = amountToRefill; + CurrentWeapon.AmmoOutsideOfClip -= amountToRefill; + } } } } + if (!isReloadingNow) + m_timeSinceStartedReloading = 0; + } void LateUpdate_jdghrjgjr() @@ -304,6 +321,7 @@ namespace SanAndreasUnity.Behaviours { this.currentWeaponSlot = slotIndex; m_frameWhenSwitchedWeapon = Time.frameCount; + m_timeSinceStartedReloading = 0; if (NetStatus.IsServer) m_ped.StopFiring (); diff --git a/Assets/Scripts/Behaviours/Weapons/WeaponsManager.cs b/Assets/Scripts/Behaviours/Weapons/WeaponsManager.cs index fd5063f1..fc7954d8 100644 --- a/Assets/Scripts/Behaviours/Weapons/WeaponsManager.cs +++ b/Assets/Scripts/Behaviours/Weapons/WeaponsManager.cs @@ -51,6 +51,7 @@ namespace SanAndreasUnity.Behaviours.Weapons public bool drawLineFromGun = false; public GameObject projectilePrefab; + public float projectileReloadTime = 3f; public static WeaponsManager Instance { get; private set; }