Cute_demon_attacks/meng_yao/Assets/script/A_Fight/SkillBox.cs
2024-12-24 16:44:04 +08:00

196 lines
5.3 KiB
C#

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class SkillBox : Base
{
public static SkillBox instance;
[Header("战魂(经验)")] public int expNumber;
[Header("战魂(经验)")] public Text expNumberText;
[Header("最大战魂(经验)")] public int maxExpNumber;
[Header("生成的战魂")] public GameObject zhanhunPrefab;
public Camera MainCamera;
public RectTransform zhanhunicon;
public float shakeDuration = 0.5f; // 摆动的时间
public float shakeStrength = 0.1f; // 摆动的幅度
public float finalMoveDuration = 1f; // 飘动到经验槽的最终时间
public GameObject infobox_skill;
public GameObject Skill;
public Transform SkillParent;
public Image ExpFill;
public int ExpNumber
{
get => expNumber;
set
{
expNumber = value;
}
}
[Header("战魂(经验)填充")] public GameObject ExpFileObj;
private int ExpFileObjNumber=0;
[Header("战魂(经验)填充父节点")] public GameObject ExpFilePrante;
public List<GameObject> expList = new List<GameObject>();
private void Start()
{
ExpFill.fillAmount = 0;
if (instance != null) return;
instance = this;
InitSkil();
}
public void UpdataExp(int number)
{
if (number==0)
{
return;
}
if (number > 0)
{
for (int i = 0; i < number; i++)
{
if (expList.Count < maxExpNumber)
{
GameObject obj = Instantiate(ExpFileObj, ExpFilePrante.transform);
expList.Add(obj);
}
}
}
else if(number < 0)
{
number *= -1;
for (int i = 0; i < number; i++)
{
GameObject obj = expList[expList.Count - 1];
expList.RemoveAt(expList.Count - 1);
Destroy(obj.gameObject);
}
}
expNumberText.text = expList.Count+"/"+maxExpNumber;
}
void InitSkil()
{
foreach (string _mengyaoRoleId in Base.GlobalObj.GetComponent<gameGlobal>().CarryCardId)
{
foreach (Monster info in MengyaoInfo.Instance.m_SkillData)
{
if (string.Equals(_mengyaoRoleId, info.temp_id))
{
for (int j = 0; j < 2; j++)
{
GameObject obj = Instantiate(Skill, SkillParent);
obj.GetComponent<Skill_Spend>().infobox_skill_up = infobox_skill;
obj.GetComponent<Skill_Spend>().MySkill = info.skills[j];
obj.GetComponent<Skill_Spend>().mengyao = MengyaoInfo.Instance.mengyaoIdToRole[info.temp_id];
obj.GetComponent<Skill_Spend>().index = j + 1;
}
break;
}
}
}
}
public void GetZhanhun(int number)
{
minZhanHun(number);
}
void minZhanHun(int number)
{
// 获取当前经验值(从经验条的填充比例计算)
int currentExp = (int)(ExpFill.fillAmount * 10);
// 累加经验
currentExp += number;
// 检查是否升级,并更新经验条
while (currentExp >= 10)
{
currentExp -= 10;
UpdataExp(1);
}
// 更新经验条显示(计算当前经验和最大经验的比例)
float expFillAmount = (float)currentExp / 10;
ExpFill.fillAmount = expFillAmount; // 更新经验条UI显示
}
public void AddExperience(int amount,Transform pos)
{
//生成预制体并设置初始位置
GameObject expObj = Instantiate(zhanhunPrefab,pos.position, Quaternion.identity);
//expObj.transform.localScale = Vector3.zero;
//CreateAni(expObj);
ShakeAndMoveToExpSlot(expObj, pos.position, amount);
}
// 物体生成动画
private void CreateAni(GameObject expObj)
{
expObj.transform.DOScale(new Vector3(1, 1, 1), 0.5f).SetEase(Ease.InBack);
}
// 物体摆动并飞到经验槽
private void ShakeAndMoveToExpSlot(GameObject expObj, Vector3 spawnPosition, int amount)
{
// 原地摆动
expObj.transform.DOShakePosition(shakeDuration, shakeStrength, 10, 90, false, true).OnComplete(() =>
{
// 获取UI目标的屏幕空间位置
Vector3 targetScreenPosition = ExpFill.transform.position;
// 将屏幕空间位置转换为世界空间位置
Vector3 targetWorldPosition = MainCamera.ScreenToWorldPoint(targetScreenPosition);
// 将z轴坐标设置为适合场景的值
targetWorldPosition.z = expObj.transform.position.z;
// 使用DOTween使得预制体飘动到目标位置
expObj.transform.DOMove(targetWorldPosition, 1f).SetEase(Ease.InOutQuad).OnComplete(() =>
{
// 飘动完成后更新经验条
GetZhanhun(amount);
AnimateExpSlotIcon();
Destroy(expObj); // 销毁飘动的物体
});
});
}
private void AnimateExpSlotIcon()
{
// 图标放大至原来的1.2倍
zhanhunicon.DOScale(1.2f, 0.1f).SetEase(Ease.OutBack).OnComplete(() =>
{
// 回到原来大小
zhanhunicon.DOScale(1f, 0.1f).SetEase(Ease.InBack);
});
}
}