using System; using TMPro; using UnityEngine; using UnityEngine.UI; public class ActionUIHandler : MonoBehaviour { #region Statication public static ActionUIHandler instance; private void Awake() { if (instance != null && instance != this) { Destroy(gameObject); return; } instance = this; } #endregion private PlayerEntity selectedEntity; private RangedWeapon possibleRanged; [Header("Main UI")] [SerializeField] private GameObject actionUI; [SerializeField] private TextMeshProUGUI actionText; public bool opened; [Header("Info UI")] [SerializeField] private GameObject infoUI; [SerializeField] private TextMeshProUGUI nameLabel; [SerializeField] private TextMeshProUGUI classLabel; [SerializeField] private Image unitPortrait; [SerializeField] private TextMeshProUGUI attackLabel; [SerializeField] private TextMeshProUGUI speedLabel; [SerializeField] private Transform healthBar; [SerializeField] private TextMeshProUGUI healthText; [Header("Action Buttons")] [SerializeField] private Button moveButton; [SerializeField] private Button attackButton; [SerializeField] private Button reloadButton; [SerializeField] private Button switchButton; [Header("Weapon Switcher")] [SerializeField] private RectTransform weaponUIPanel; [SerializeField] private Button templateWeaponButton; [SerializeField] private float templateButtonHeight; //500 if statements in this script lol public void ShowUI(PlayerEntity target) { opened = true; possibleRanged = null; selectedEntity = target; actionUI.SetActive(true); actionText.gameObject.SetActive(true); infoUI.SetActive(true); UpdateUI(); } public void UpdateUI() { if (selectedEntity.currentWeapon.TryGetComponent(out RangedWeapon isRanged)) { possibleRanged = isRanged; } if (!isRanged || !isRanged.fired) { attackButton.gameObject.SetActive(true); reloadButton.gameObject.SetActive(false); } else { attackButton.gameObject.SetActive(false); if (isRanged && isRanged.fired) { if (selectedEntity.actions < 2) { reloadButton.gameObject.SetActive(false); } else { reloadButton.gameObject.SetActive(true); } } } moveButton.gameObject.SetActive(true); switchButton.gameObject.SetActive(true); actionText.text = $"Remaining Actions: {selectedEntity.actions}"; UpdateInfo(); if (selectedEntity.actions == 0) { HideUI(); } } public void UpdateInfo() { healthBar.localScale = new Vector3(selectedEntity.health / selectedEntity.maxHealth,1,1); healthText.text = $"{selectedEntity.health}/{selectedEntity.maxHealth}"; attackLabel.text = $"Attack: {selectedEntity.currentWeapon.damage}"; speedLabel.text = $"Speed: {selectedEntity.maxMovement}"; } public void HideUI() { opened = false; foreach (Transform uiObject in transform) { uiObject.gameObject.SetActive(false); } infoUI.SetActive(false); } public void ReloadGun() { possibleRanged.Reload(); selectedEntity.actions -= 2; HideUI(); } public void Attack() { selectedEntity.Attack(); HideUI(); } public void SkipTurn() { selectedEntity.SkipTurn(); HideUI(); } public void MoveEntity() { HideUI(); PlayerEntityMovement.instance.SelectEntity(selectedEntity); } public void ShowWeaponList() { foreach (Transform oldButton in weaponUIPanel) { Destroy(oldButton.gameObject); } if (weaponUIPanel.gameObject.activeSelf) { HideWeaponList(); return; } weaponUIPanel.gameObject.SetActive(true); weaponUIPanel.sizeDelta = new Vector2(weaponUIPanel.sizeDelta.x, templateButtonHeight * selectedEntity.weaponInstances.Count); foreach (Weapon weapon in selectedEntity.weaponInstances) { Button newButton = Instantiate(templateWeaponButton, weaponUIPanel); newButton.gameObject.SetActive(true); if (weapon != selectedEntity.currentWeapon) { newButton.onClick.AddListener(() => SelectWeapon(weapon)); newButton.GetComponentInChildren().text = weapon.weaponName; } else { newButton.GetComponentInChildren().text = $"{weapon.weaponName} (Equipped)"; } } } public void SelectWeapon(Weapon weaponSelected) { selectedEntity.actions--; selectedEntity.SwitchWeapon(weaponSelected); HideWeaponList(); UpdateUI(); } private void HideWeaponList() { weaponUIPanel.gameObject.SetActive(false); } }