LunarInfantry/Assets/Scripts/ActionUIHandler.cs

169 lines
4.6 KiB
C#

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;
public bool opened;
[Header("Main UI")]
[SerializeField] private Vector3 offset;
[SerializeField] private GameObject actionUI;
[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;
transform.position = Input.mousePosition + offset;
actionUI.SetActive(true);
UpdateUI();
}
public void UpdateUI()
{
if (selectedEntity.currentWeapon.TryGetComponent(out RangedWeapon isRanged))
{
possibleRanged = isRanged;
}
if ((!isRanged || !isRanged.fired) && !selectedEntity.hasAttacked)
{
attackButton.gameObject.SetActive(true);
reloadButton.gameObject.SetActive(false);
}
else
{
attackButton.gameObject.SetActive(false);
if (isRanged && isRanged.fired)
{
if (selectedEntity.hasMoved || selectedEntity.hasAttacked)
{
reloadButton.gameObject.SetActive(false);
}
else
{
reloadButton.gameObject.SetActive(true);
}
}
}
if (!selectedEntity.hasMoved || !selectedEntity.hasAttacked)
{
moveButton.gameObject.SetActive(true);
switchButton.gameObject.SetActive(true);
}
if (selectedEntity.hasAttacked && selectedEntity.hasMoved)
{
HideUI();
}
}
public void HideUI()
{
opened = false;
actionUI.SetActive(false);
weaponUIPanel.gameObject.SetActive(false);
}
public void ReloadGun()
{
possibleRanged.Reload();
selectedEntity.hasAttacked = true;
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<TextMeshProUGUI>().text = weapon.weaponName;
}
else
{
newButton.GetComponentInChildren<TextMeshProUGUI>().text = $"{weapon.weaponName} (Equipped)";
}
}
}
public void SelectWeapon(Weapon weaponSelected)
{
MoveAction();
selectedEntity.SwitchWeapon(weaponSelected);
HideWeaponList();
UpdateUI();
}
private void HideWeaponList()
{
weaponUIPanel.gameObject.SetActive(false);
}
private void MoveAction()
{
if (!selectedEntity.hasMoved)
{
selectedEntity.hasMoved = true;
}
else if (!selectedEntity.hasAttacked)
{
selectedEntity.hasAttacked = true;
}
}
}