MayHeCome/Assets/Scripts/UI/BuildingTechItemUI.cs

179 lines
5.3 KiB
C#
Raw Normal View History

2024-12-18 09:55:34 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class BuildingTechItemUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler,IPointerClickHandler
{
//79 130
[SerializeField] private Image selectLine;
[SerializeField] private GameObject Title;
[SerializeField] private GameObject currentValue;
[SerializeField] private GameObject Divider;
[SerializeField] private GameObject FaithNeedPrefab;
[SerializeField] private GameObject WoodNeedPrefab;
[SerializeField] private GameObject BlobNeedPrefab;
[SerializeField] private Transform ResourceNeedParent;
private UnityEvent OnClickEvent = new UnityEvent();
public UI_InformationPanel BelongPanel;
private SpecailBuff_Buildings.BuildingTechItem buildingTechItem;
private void FixedUpdate()
{
if (faithNOT != null)
{
if (buildingTechItem.faithCost > Singleton<GameProcedureManager>.Instance.FaithCount)
{
faithNOT.color = Color.red;
}
else
{
faithNOT.color = Color.white;
}
}
if (blobNOT != null)
{
if (buildingTechItem.blobCost > BlobManager.Instance.AvailableBlobs.Count)
{
blobNOT.color = Color.red;
}
else
{
blobNOT.color = Color.white;
}
}
if (woodNOT != null)
{
if (buildingTechItem.woodCost > Singleton<GameProcedureManager>.Instance.WoodCount)
{
woodNOT.color = Color.red;
}
else
{
woodNOT.color = Color.white;
}
}
}
public void CreateBuildingTechItemUI(SpecailBuff_Buildings.BuildingTechItem bti,UI_InformationPanel p)
{
buildingTechItem = bti;
BelongPanel = p;
Title.GetComponent<TMP_Text>().text = bti.TechNameCN;
HasCurrentValue();
GenerateResourceNeed(
bti.faithCost,bti.blobCost,bti.woodCost,
ResourceNeedParent
);
OnClickEvent.AddListener(delegate ()
{
bti.TechNameEN.Invoke();
bti.CostResource();
int siblingIndex = BelongPanel.RemoveTechItem(bti);
bti.InvokeNextTech(BelongPanel,siblingIndex);
});
}
private TMP_Text faithNOT;
private TMP_Text woodNOT;
private TMP_Text blobNOT;
public void GenerateResourceNeed(int faith,int blob,int wood,Transform parent)
{
for (int i = parent.childCount - 1; i >= 0; i--)
{
// 获取子物体并销毁
Destroy(parent.GetChild(i).gameObject);
}
if (faith != 0)
{
GameObject faithNO = Instantiate(FaithNeedPrefab, parent);
faithNOT = faithNO.transform.GetChild(0).GetComponent<TMP_Text>();
faithNOT.text = NumUtils.IntToString(faith);
}
if (wood != 0)
{
GameObject woodNO = Instantiate(WoodNeedPrefab, parent);
woodNOT = woodNO.transform.GetChild(0).GetComponent<TMP_Text>();
woodNOT.text = NumUtils.IntToString(wood);
}
if (blob != 0)
{
GameObject blobNO = Instantiate(BlobNeedPrefab, parent);
blobNOT = blobNO.transform.GetChild(0).GetComponent<TMP_Text>();
blobNOT.text = blob.ToString();
}
}
//检测当前资源是否满足需求
public bool CheckUnlock(int faithNeed, int blobNeed, int woodNeed)
{
if (GameProcedureManager.Instance.FaithCount >= faithNeed &&
BlobManager.Instance.AvailableBlobs.Count >= blobNeed &&
GameProcedureManager.Instance.WoodCount >= woodNeed)
{
return true;
}
return false;
}
public void OnPointerEnter(PointerEventData eventData)
{
CursorManager.Instance.SetHover();
BelongPanel.PlayUIAudio(0);
selectLine.color = Color.white;
BelongPanel.OpenSecondInfo(buildingTechItem.TechNameCN);
}
public void OnPointerExit(PointerEventData eventData)
{
CursorManager.Instance.SetNormal();
selectLine.color = Color.black;
BelongPanel.CloseSecondInfo();
}
public void OnPointerClick(PointerEventData eventData)
{
BelongPanel.PlayUIAudio(1);
bool unlock = CheckUnlock(buildingTechItem.faithCost, buildingTechItem.blobCost, buildingTechItem.woodCost);
if (unlock)
{
OnClickEvent.Invoke();
}
}
private void HasCurrentValue()
{
if (buildingTechItem.currentNum != -1)
{
//有当前值
Title.GetComponent<RectTransform>().sizeDelta = new Vector2(160,30);
currentValue.SetActive(true);
Divider.SetActive(true);
currentValue.GetComponent<TMP_Text>().text = buildingTechItem.currentNum.ToString();
}
else
{
Title.GetComponent<RectTransform>().sizeDelta = new Vector2(210,30);
currentValue.SetActive(false);
Divider.SetActive(false);
}
}
}