summaryrefslogtreecommitdiff
path: root/GameCode/GunAmmo.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2023-10-27 11:05:14 +0800
committerchai <215380520@qq.com>2023-10-27 11:05:14 +0800
commit766cdff5ffa72b65d7f106658d1603f47739b2ba (patch)
tree34d7799a94dfa9be182825577583c0fa6dc935f7 /GameCode/GunAmmo.cs
+ init
Diffstat (limited to 'GameCode/GunAmmo.cs')
-rw-r--r--GameCode/GunAmmo.cs212
1 files changed, 212 insertions, 0 deletions
diff --git a/GameCode/GunAmmo.cs b/GameCode/GunAmmo.cs
new file mode 100644
index 0000000..102b4d4
--- /dev/null
+++ b/GameCode/GunAmmo.cs
@@ -0,0 +1,212 @@
+using Sonigon;
+using UnityEngine;
+using UnityEngine.UI;
+
+public class GunAmmo : MonoBehaviour
+{
+ [Header("Sounds")]
+ public SoundEvent soundReloadInProgressLoop;
+
+ public SoundEvent soundReloadComplete;
+
+ private SoundParameterIntensity soundReloadInProgressIntensity = new SoundParameterIntensity(0f, UpdateMode.Continuous);
+
+ private bool soundReloadInProgressPlaying;
+
+ private float soundReloadTime;
+
+ [Header("Settings")]
+ public int maxAmmo = 3;
+
+ private int lastMaxAmmo;
+
+ private int currentAmmo;
+
+ public float reloadTime = 1.5f;
+
+ public float reloadTimeMultiplier = 1f;
+
+ public float reloadTimeAdd;
+
+ private Gun gun;
+
+ public CurveAnimation reloadAnim;
+
+ private Image reloadRing;
+
+ public Image cooldownRing;
+
+ private float reloadCounter;
+
+ private float freeReloadCounter;
+
+ public Populate populate;
+
+ public float ammoReg;
+
+ private float currentRegCounter;
+
+ private void Start()
+ {
+ reloadRing = reloadAnim.GetComponent<Image>();
+ lastMaxAmmo = maxAmmo;
+ currentAmmo = maxAmmo;
+ gun = GetComponentInParent<Gun>();
+ ReDrawTotalBullets();
+ }
+
+ private void OnDisable()
+ {
+ SoundStopReloadInProgress();
+ }
+
+ private void OnDestroy()
+ {
+ SoundStopReloadInProgress();
+ }
+
+ private void SoundStopReloadInProgress()
+ {
+ if (soundReloadInProgressPlaying)
+ {
+ soundReloadInProgressPlaying = false;
+ SoundManager.Instance.Stop(soundReloadInProgressLoop, base.transform);
+ }
+ }
+
+ private float ReloadTime()
+ {
+ return (reloadTime + reloadTimeAdd) * reloadTimeMultiplier;
+ }
+
+ private void Update()
+ {
+ if (!gun.isReloading)
+ {
+ SoundStopReloadInProgress();
+ }
+ if (gun.isReloading)
+ {
+ reloadCounter -= TimeHandler.deltaTime;
+ if (!soundReloadInProgressPlaying)
+ {
+ soundReloadInProgressPlaying = true;
+ soundReloadTime = ReloadTime();
+ soundReloadInProgressIntensity.intensity = 0f;
+ SoundManager.Instance.Play(soundReloadInProgressLoop, base.transform, soundReloadInProgressIntensity);
+ }
+ if (soundReloadInProgressPlaying && soundReloadTime > 0f)
+ {
+ soundReloadInProgressIntensity.intensity = 1f - reloadCounter / soundReloadTime;
+ }
+ if (reloadCounter < 0f)
+ {
+ ReloadAmmo();
+ }
+ }
+ else if (currentAmmo != maxAmmo)
+ {
+ freeReloadCounter += TimeHandler.deltaTime;
+ if (freeReloadCounter > ReloadTime() && gun.player.data.stats.automaticReload)
+ {
+ currentAmmo = maxAmmo;
+ SetActiveBullets();
+ }
+ currentRegCounter += ammoReg * TimeHandler.deltaTime * (float)maxAmmo;
+ if (currentRegCounter > 1f)
+ {
+ currentAmmo++;
+ currentRegCounter = 0f;
+ SetActiveBullets();
+ }
+ }
+ if (currentAmmo <= 0)
+ {
+ if (reloadAnim.currentState != 0)
+ {
+ reloadAnim.PlayIn();
+ }
+ }
+ else if (reloadAnim.currentState != CurveAnimationUse.Out)
+ {
+ reloadAnim.PlayOut();
+ }
+ reloadRing.fillAmount = (ReloadTime() - reloadCounter) / ReloadTime();
+ if (gun.attackSpeed > 0.4f)
+ {
+ cooldownRing.fillAmount = gun.ReadyAmount();
+ if (gun.ReadyAmount() >= 1f)
+ {
+ cooldownRing.fillAmount = 0f;
+ }
+ }
+ else
+ {
+ cooldownRing.fillAmount = 0f;
+ }
+ if (maxAmmo != lastMaxAmmo)
+ {
+ ReDrawTotalBullets();
+ }
+ lastMaxAmmo = maxAmmo;
+ }
+
+ public void ReloadAmmo(bool playSound = true)
+ {
+ gun.player.data.stats.OnReload(maxAmmo - currentAmmo);
+ gun.isReloading = false;
+ currentAmmo = maxAmmo;
+ SoundStopReloadInProgress();
+ if (playSound)
+ {
+ SoundManager.Instance.Play(soundReloadComplete, base.transform);
+ }
+ SetActiveBullets();
+ }
+
+ public void Shoot(GameObject projectile)
+ {
+ currentAmmo--;
+ freeReloadCounter = 0f;
+ SetActiveBullets();
+ if (currentAmmo <= 0)
+ {
+ reloadCounter = ReloadTime();
+ gun.isReloading = true;
+ gun.player.data.stats.OnOutOfAmmp(maxAmmo);
+ }
+ }
+
+ public void ReDrawTotalBullets()
+ {
+ currentAmmo = maxAmmo;
+ for (int num = populate.transform.childCount - 1; num >= 0; num--)
+ {
+ if (populate.transform.GetChild(num).gameObject.activeSelf)
+ {
+ Object.Destroy(populate.transform.GetChild(num).gameObject);
+ }
+ }
+ populate.times = maxAmmo;
+ populate.DoPopulate();
+ SetActiveBullets(forceTurnOn: true);
+ }
+
+ private void SetActiveBullets(bool forceTurnOn = false)
+ {
+ for (int i = 1; i < populate.transform.childCount; i++)
+ {
+ if (i <= currentAmmo || forceTurnOn)
+ {
+ if (populate.transform.GetChild(i).GetComponent<CurveAnimation>().currentState != CurveAnimationUse.In || forceTurnOn)
+ {
+ populate.transform.GetChild(i).GetComponent<CurveAnimation>().PlayIn();
+ }
+ }
+ else if (populate.transform.GetChild(i).GetComponent<CurveAnimation>().currentState != CurveAnimationUse.Out)
+ {
+ populate.transform.GetChild(i).GetComponent<CurveAnimation>().PlayOut();
+ }
+ }
+ }
+}