MayHeCome/Assets/ChengHui/Script/SpecialBuff/SpecialBuff_Button.cs
2024-12-18 17:55:34 +08:00

159 lines
4.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class SpecialBuff_Button : MonoBehaviour
{
public SpecailBuff_Buildings buildings_Belong;
public string buffName;
public bool canBeClosed;
[Header("升级项目")]
public List<SpecailBuff_Buildings.Resource> resourceNeeded;
public int Level = 0;
public int MaxLevel;
// Start is called before the first frame update
void Start()
{
if(resourceNeeded != null)//可以被升级
{
GetComponent<Button>().interactable = false;
GetComponent<Button>().onClick.AddListener(delegate ()
{
this.SpecialBuffUpGrade();
});
}
}
// Update is called once per frame
void Update()
{
if (resourceNeeded != null&&Level<MaxLevel)
{
if (resourceNeeded[Level].Faith <= GameProcedureManager.Instance.FaithCount && resourceNeeded[Level].Wood <= GameProcedureManager.Instance.WoodCount)
{
GetComponent<Button>().interactable = true;
}
}
}
public void SpecialBuffActive()//直接激活无需后续
{
//激活功能
buildings_Belong.Step2_Type_1(buffName);
//按钮亮起来
GetComponent<Button>().interactable = true;
if (canBeClosed)
{
//状态改为激活
buildings_Belong.buff_ifActive[buffName] = true;
GetComponent<Button>().onClick.AddListener(delegate ()
{
this.SpecialBuffDisactive();
});
transform.Find("Text").GetComponent<TMP_Text>().text = buildings_Belong.EN_CN[buffName] + ":开";
}
else
{
transform.Find("Text").GetComponent<TMP_Text>().text = buildings_Belong.EN_CN[buffName];
}
}
public void SpecialBuffUpGrade()//需要升级
{
//消耗资源升一级
GameProcedureManager.Instance.WoodCount -= resourceNeeded[Level].Wood;
GameProcedureManager.Instance.FaithCount -= resourceNeeded[Level].Faith;
Level++;
GetComponent<Button>().interactable = false;
//激活功能
buildings_Belong.Step2_Type_2(buffName,Level);
if (Level == MaxLevel)//已达最高级
{
GetComponent<Button>().interactable = true;
//重设按钮
GetComponent<Button>().onClick.RemoveAllListeners();
if (canBeClosed)
{
//状态改为激活
buildings_Belong.buff_ifActive[buffName] = true;
//添加关闭方法
GetComponent<Button>().onClick.AddListener(delegate ()
{
this.SpecialBuffDisactive();
});
transform.Find("Text").GetComponent<TMP_Text>().text = buildings_Belong.EN_CN[buffName] + ":开";
}
else
{
transform.Find("Text").GetComponent<TMP_Text>().text = buildings_Belong.EN_CN[buffName];
}
}
else
{
SetText();
}
}
public void SpecialBuffDisactive()//关闭Buff
{
//状态改为关闭
buildings_Belong.buff_ifActive[buffName] = false;
//重设文字
transform.Find("Text").GetComponent<TMP_Text>().text = buildings_Belong.EN_CN[buffName] + ":关";
buildings_Belong.Step2_Type_1(buffName);
//重设按钮
GetComponent<Button>().onClick.RemoveAllListeners();
GetComponent<Button>().onClick.AddListener(delegate ()
{
this.SpecialBuffActive();
});
}
public void InitalSet(SpecailBuff_Buildings buff_Buildings, string BuffName,bool CanBeClosed)
{
buildings_Belong = buff_Buildings;
buffName = BuffName;
canBeClosed = CanBeClosed;
}
public void InitalSet(SpecailBuff_Buildings buff_Buildings, string BuffName, bool CanBeClosed, int maxLevel, List<SpecailBuff_Buildings.Resource> ResourceNeeded)
{
buildings_Belong = buff_Buildings;
buffName = BuffName;
canBeClosed = CanBeClosed;
MaxLevel = maxLevel;
resourceNeeded = ResourceNeeded;
}
public void SetText()
{
string s1, s2, s3;
if (resourceNeeded[Level].Faith > 0)
{
s1 = ": 信仰 " + resourceNeeded[Level].Faith;
}
else
{
s1 = "";
}
if (resourceNeeded[Level].Wood > 0)
{
s2 = ",木头 " + resourceNeeded[Level].Wood;
}
else
{
s2 = "";
}
if (resourceNeeded[Level].TuanZi > 0)
{
s3 = ",团子 " + resourceNeeded[Level].TuanZi;
}
else
{
s3 = "";
}
transform.Find("Text").GetComponent<TMP_Text>().text = buildings_Belong.EN_CN[buffName] + s1 + s2 + s3;
}
}