using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using Sirenix.OdinInspector; using TMPro; using KairoEngine.Core; using KairoEngine.UI; namespace KairoEngine.Inventory { // Todo: Refactor Update method to remove CharacterSystem references using the EventSystem for changing values public class CurrentAmmoUi : MonoBehaviour { private ItemContainer playerInventory; private ItemRef currentItem; public GameObject canvasParent; public ItemSlotUi itemSlotUi; public TextMeshProUGUI textAmmo; public TextMeshProUGUI textAmmoType; private string currentScene; void OnEnable() { EventManager.broadcast.StartListening("PlayerShowAmmo", ShowAmmoUI); GenericEvents.StartListening("PlayerHideAmmo", HideAmmoUI); UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded; } void OnDisable() { EventManager.broadcast.StopListening("PlayerShowAmmo", ShowAmmoUI); GenericEvents.StopListening("PlayerHideAmmo", HideAmmoUI); UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded; } public void Start() { canvasParent.SetActive(false); } private void ShowAmmoUI(ItemContainer itemContainer, ItemRef itemRef) { this.playerInventory = itemContainer; this.currentItem = itemRef; if(itemRef.item.category != ItemType.firearm) { canvasParent.SetActive(false); return; } this.canvasParent.SetActive(true); canvasParent.SetActive(true); } private void HideAmmoUI() { canvasParent.SetActive(false); } private void UpdateAmmo() { int totalAmmo; ItemFirearmRef firearmRef = currentItem as ItemFirearmRef; ItemRef ammoRef = playerInventory.GetItem(firearmRef.ammoType); if(ammoRef == null) { totalAmmo = 0; ammoRef = new ItemRef(firearmRef.ammoType, 0); } else { totalAmmo = ammoRef.quantity; } itemSlotUi.Setup(ammoRef, null, null, false); textAmmoType.text = firearmRef.ammoType.title; textAmmo.text = firearmRef.ammo + "/" + totalAmmo; } public void Update() { if(canvasParent.activeSelf) UpdateAmmo(); } private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { currentScene = scene.name; } } }