using System; using System.Collections; using System.Collections.Generic; using Photon.Pun; using TMPro; using Unity.VisualScripting; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class UIManager : MonoBehaviour { public GameObject loadingPanel; public TMP_Text loadingText; public static UIManager instance; public Text UsernameTxt; public TMP_Text PickupText; public TMP_Text EnemyPopupText; public Image OnScreenDamageFx; public float OnScreenDmgFxFadeoutTime = 0.1f; public Image Healthbar; public Image ShieldBar; public Text HealthTxt; public Image StaminaBar; public CanvasGroup DamageDirectionIndicator; public float DamageDirectionIndicatorOffset = 90; public Image weaponSlot1, weaponSlot2, pistolSlot; public Image throwableSlot; public TMP_Text weaponSlot1Ammo, weaponSlot2Ammo, pistolSlotAmmo; public TMP_Text throwableSlotAmount; public GameObject Crosshair; public CrosshairsScriptable crosshairs; public RectTransform rectParent; public GameObject pauseMenuPanel; [Header("Inventory")] public GameObject inventoryPanel; public Transform inventoryGridParent; public GameObject inventoryGridItemPrefab; public Image weaponSlot1Inventory, weaponSlot2Inventory, pistolSlotInventory, throwableSlotInventory; [Header("Deathmatch")] public TMP_Text TimerTxt; public TMP_Text LastChampTxt; public double StartTime = 0; public float RoundLength; public GameObject DeathmatchPanel; public TMP_Text DeathmatchPositionTxt; public TMP_Text DeathmatchWinnerTxt; public TMP_Text DeathmatchAutoRestartTxt; public float timer => (StartTime<=0) ? RoundLength : RoundLength - (float)(PhotonNetwork.Time - StartTime); private void Awake() { instance = this; PickupText.text = ""; EnemyPopupText.text = ""; SetCroshair(false); loadingPanel.SetActive(true); SetStatusText("Connecting"); } void Start(){ OnSettingsChanged(); SettingsManager.instance.OnSettingsChanged.AddListener(OnSettingsChanged); StartCoroutine(Coroutine_UpdateLastChampTxt()); } IEnumerator Coroutine_UpdateLastChampTxt(){ WWW req= new WWW(UserDataManager.GetApiUrl("get_last_champ")); yield return req; LastChampTxt.text = $"Last session Champion: {req.text}"; } public void OnSettingsChanged(){ Crosshair.GetComponent().sprite = crosshairs.crosshairs[PlayerPrefs.GetInt(SettingsManager.CROSSHAIR_PREF_KEY)]; Crosshair.GetComponent().color = new Color(1,1,1, PlayerPrefs.GetFloat(SettingsManager.CROSSHAIR_OPACITY_PREF_KEY)); } public static void WaitingForPlayers(){ instance.loadingPanel.SetActive(false); SetStatusText("Waiting For Players"); } public static void SetStatusText(string txt){ instance.loadingText.text = txt; } public static bool isPaused => instance.pauseMenuPanel.activeSelf; private void Update() { if(Input.GetKeyUp(KeyCode.Escape) && !SettingsManager.isShowing){ TogglePauseMenu(); } if (OnScreenDamageFx.color.a > 0) { OnScreenDamageFx.color = new UnityEngine.Color(1, 1, 1, OnScreenDamageFx.color.a - (OnScreenDmgFxFadeoutTime * Time.deltaTime)); } if(DamageDirectionIndicator.alpha > 0) { DamageDirectionIndicator.alpha -= Time.deltaTime; } inventoryPanel.SetActive(Input.GetKey(KeyCode.I)); if(timer >0){ TimerTxt.text = Helpers.secondsToTimerString(timer); } } public void TogglePauseMenu(){ pauseMenuPanel.SetActive(!pauseMenuPanel.activeSelf); if(pauseMenuPanel.activeSelf){ Cursor.visible=true; Cursor.lockState = CursorLockMode.None; }else{ Cursor.visible=false; Cursor.lockState = CursorLockMode.Locked; } } public static void ShowDeathmatchFinishScreen(int position, string winnerName){ instance.m_ShowDeathmatchFinishScreen(position, winnerName); } public static void ShowTDMFinishScreen(string winnerName){ instance.m_ShowTDMFinishScreen(winnerName); } void m_ShowTDMFinishScreen(string winnerName){ DeathmatchPanel.SetActive(true); // DeathmatchPositionTxt.text = $"You finished {Helpers.numberToPosition(position+1)}"; DeathmatchWinnerTxt.text = $"The Winner is {winnerName}"; StartCoroutine(StartDeathmatchRestartCountdown()); } void m_ShowDeathmatchFinishScreen(int position, string winnerName){ DeathmatchPanel.SetActive(true); DeathmatchPositionTxt.text = $"You finished {Helpers.numberToPosition(position+1)}"; if(position==0){ DeathmatchWinnerTxt.text = "Well Played!"; }else{ DeathmatchWinnerTxt.text = $"The Winner is {winnerName}"; } StartCoroutine(StartDeathmatchRestartCountdown()); } IEnumerator StartDeathmatchRestartCountdown(){ int t = 10; while(t > 0){ DeathmatchAutoRestartTxt.text = $"Restarting in {t} seconds"; t--; yield return new WaitForSecondsRealtime(1f); } } public void SetTimer(double startTime, float roundLength){ StartTime=startTime; RoundLength = roundLength; } public static void SetCroshair(bool value) { //instance.Crosshair.SetActive(value); } public static void OnRespawn(){ instance.OnScreenDamageFx.color = new UnityEngine.Color(1, 1, 1, 0); } public static void SetHealth(float health,float shield) { instance.Healthbar.fillAmount = health / 100f; instance.ShieldBar.fillAmount = shield /100f; instance.HealthTxt.text = health.ToString("n0") + " / 100"; } public static void SetStamina(float stamina, float maxStamina){ instance.StaminaBar.fillAmount = stamina / maxStamina; } public static void SetUsername(string username) { instance.UsernameTxt.text= username; } public static void SetDamageDirection(float angle) { instance.DamageDirectionIndicator.transform.rotation = Quaternion.Euler(0, 0, instance.DamageDirectionIndicatorOffset -angle); instance.DamageDirectionIndicator.alpha = 1; } public static void SetWeaponSlot1(LootItemScriptableObject type) { if (type == null) { } else { instance.weaponSlot1.sprite = type.UIImage; instance.weaponSlot1Inventory.sprite = type.UIImage; } instance.weaponSlot1Inventory.enabled = type != null; instance.weaponSlot1.enabled = type != null; } public static void SetWeaponSlot2(LootItemScriptableObject type) { if(type == null) {} else { instance.weaponSlot2.sprite = type.UIImage; instance.weaponSlot2Inventory.sprite = type.UIImage; } instance.weaponSlot2Inventory.enabled = type != null; instance.weaponSlot2.enabled = type != null; } public static void SetThrowableSlot(LootItemScriptableObject type) { instance.throwableSlotInventory.enabled = type != null; instance.throwableSlot.enabled = type != null; if (type != null) { instance.throwableSlotInventory.sprite = type.UIImage; instance.throwableSlot.sprite = type.UIImage; } } public static void SetPrimarySlot(LootItemScriptableObject type) { instance.pistolSlotInventory.enabled = type != null; instance.pistolSlot.enabled = type != null; if (type != null) { instance.pistolSlotInventory.sprite = type.UIImage; instance.pistolSlot.sprite = type.UIImage; } } public static void SetWeaponSlot(int i) { float activeOpacity =0.8f; float inactiveOpacity = 0.4f; if(i == 0) { Image img1 = instance.weaponSlot1.transform.parent.GetComponent(); img1.color = new Color(img1.color.r, img1.color.g, img1.color.b, activeOpacity); Image img2 = instance.weaponSlot2.transform.parent.GetComponent(); img2.color = new Color(img2.color.r, img2.color.g, img2.color.b, inactiveOpacity); Image img3 = instance.pistolSlot.transform.parent.GetComponent(); img3.color = new Color(img3.color.r,img3.color.g, img3.color.b, inactiveOpacity); }else if(i == 1) { Image img1 = instance.weaponSlot1.transform.parent.GetComponent(); img1.color = new Color(img1.color.r, img1.color.g, img1.color.b, inactiveOpacity); Image img2 = instance.weaponSlot2.transform.parent.GetComponent(); img2.color = new Color(img2.color.r, img2.color.g, img2.color.b, activeOpacity); Image img3 = instance.pistolSlot.transform.parent.GetComponent(); img3.color = new Color(img3.color.r,img3.color.g, img3.color.b, inactiveOpacity); }else if(i == 2){ Image img1 = instance.weaponSlot1.transform.parent.GetComponent(); img1.color = new Color(img1.color.r, img1.color.g, img1.color.b, inactiveOpacity); Image img2 = instance.weaponSlot2.transform.parent.GetComponent(); img2.color = new Color(img2.color.r, img2.color.g, img2.color.b, inactiveOpacity); Image img3 = instance.pistolSlot.transform.parent.GetComponent(); img3.color = new Color(img3.color.r,img3.color.g, img3.color.b, activeOpacity); } else { Image img1 = instance.weaponSlot1.transform.parent.GetComponent(); img1.color = new Color(img1.color.r, img1.color.g, img1.color.b, inactiveOpacity); Image img2 = instance.weaponSlot2.transform.parent.GetComponent(); img2.color = new Color(img2.color.r, img2.color.g, img2.color.b, inactiveOpacity); Image img3 = instance.pistolSlot.transform.parent.GetComponent(); img3.color = new Color(img3.color.r,img3.color.g, img3.color.b, inactiveOpacity); } } public static void OnDeath(){ } public static void UpdateInventory(InventoryManager inventory) { //PURGE for (int i = 0; i < instance.inventoryGridParent.childCount; i++) { Destroy(instance.inventoryGridParent.GetChild(i).gameObject); } foreach (KeyValuePair keyValuePair in InventoryManager.instance.Stock) { if(keyValuePair.Value <= 0) { continue; } GameObject newItem = Instantiate(instance.inventoryGridItemPrefab, instance.inventoryGridParent); newItem.transform.GetChild(0).GetComponent().sprite = keyValuePair.Key.UIImage; newItem.transform.GetChild(1).GetComponent().text = keyValuePair.Key.displayName; newItem.transform.GetChild(2).GetComponent().text = keyValuePair.Value.ToString(); } // instance.throwableSlot.sprite = inventory.EquippedThrowable.UIImage; SetWeaponSlot1(inventory.FirstWeapon); SetWeaponSlot2(inventory.SecondWeapon); SetThrowableSlot(inventory.EquippedThrowable); SetPrimarySlot(inventory.PrimaryWeapon); instance.weaponSlot1Ammo.text = inventory.FirstWeapon == null ? "" : (inventory.loadedRounds1 + " / " + inventory.getStock(inventory.FirstWeapon.ammo)); instance.weaponSlot2Ammo.text = inventory.SecondWeapon == null ? "" : (inventory.loadedRounds2 + " / " + inventory.getStock(inventory.SecondWeapon.ammo)); instance.pistolSlotAmmo.text = inventory.PrimaryWeapon == null ? "" : (inventory.loadedRoundsPrimary + " / " + inventory.getStock(inventory.PrimaryWeapon.ammo)); instance.throwableSlotAmount.text = inventory.EquippedThrowable == null ? "" : (inventory.Stock.GetValueOrDefault(inventory.EquippedThrowable, 0).ToString("n0")); } public void LoadMainMenu(){ SceneManager.LoadScene("MainMenu"); } }