Cute_demon_attacks/meng_yao/Assets/script/A_Fight/cardBox.cs
wulongxiao ab29549151 bug
2024-12-14 17:34:12 +08:00

149 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class MengyaoCardData
{
[Tooltip("萌妖卡ID")]
public string cardID;
[Tooltip("萌妖卡预制体")]
public GameObject cardPrefab;
}
public class cardBox : MonoBehaviour
{
public static cardBox instance;
[Header("萌妖数量")] public int mengyaoNumber = 0;
[HideInInspector] public int inPlaceNumber = 0;
[Header("开始按钮obj")] public GameObject btnObj;
[Header("怪物信息提示按钮obj")] public GameObject IconTipObj;
[Header("萌妖卡数据列表")] public List<MengyaoCardData> mengyaoCardDataList = new List<MengyaoCardData>();
[Header("萌妖卡生成父节点")] public Transform parentPos;
public float scaleUpDuration = 0.5f; // 缩放动画时长
public float fadeInDuration = 0.5f; // 淡入动画时长
// ID到萌妖卡预制体的映射字典
private Dictionary<string, GameObject> IDTomangyaoPrefab = new Dictionary<string, GameObject>();
[HideInInspector] public List<GameObject> card = new List<GameObject>();
private void Awake()
{
instance = this;
}
void Start()
{
if (btnObj == null)
{
Debug.LogError("btnObj==null");
return;
}
btnObj.SetActive(false);
InitializeDictionary();
Init();
}
public void ChangeInPlaceNumber(int number)
{
inPlaceNumber += number;
if (inPlaceNumber == mengyaoNumber)
{
gameGlobal.GameStart();
//Debug.Log("布置完成");
btnObj.SetActive(true);
btnObj.GetComponent<Button>().onClick.AddListener(() =>
{
IconTipObj.gameObject.SetActive(true);
btnObj.SetActive(false);
});
}
}
/// <summary>
/// 初始化ID到萌妖卡预制体的映射字典
/// </summary>
void InitializeDictionary()
{
foreach (var cardData in mengyaoCardDataList)
{
string id = cardData.cardID;
GameObject prefab = cardData.cardPrefab;
// 检查ID和预制体是否为空
if (string.IsNullOrEmpty(id))
{
Debug.LogWarning("萌妖卡ID为空跳过该条数据");
continue;
}
if (prefab == null)
{
Debug.LogWarning($"萌妖卡ID '{id}' 对应的预制体为空,跳过该条数据!");
continue;
}
// 检查ID是否重复
if (IDTomangyaoPrefab.ContainsKey(id))
{
Debug.LogWarning($"萌妖卡ID '{id}' 在字典中已经存在,预制体将被忽略。");
continue;
}
// 添加到字典
IDTomangyaoPrefab.Add(id, prefab);
}
// 测试输出
foreach (var kvp in IDTomangyaoPrefab)
{
Debug.Log($"初始化字典 - ID: {kvp.Key}, Prefab: {kvp.Value.name}");
}
}
/// <summary>
/// 根据CarryCardId列表生成对应的萌妖卡牌
/// </summary>
void Init()
{
// 获取 CarryCardId 列表
List<string> carryCardIdList = Base.GlobalObj.GetComponent<gameGlobal>().CarryCardId;
foreach (string id in carryCardIdList)
{
if (IDTomangyaoPrefab.TryGetValue(id, out GameObject prefab))
{
// 实例化预制体,设置父节点为 parentPos
GameObject card = Instantiate(prefab, parentPos);
//card.GetComponent<enemy>().enemyId = id;
//this.card.Add(card);
// 确保卡牌的缩放为0准备进行放大动画
card.transform.localScale = Vector3.zero;
// 确保卡牌的位置为父节点的本地位置
card.transform.localPosition = Vector3.zero;
// 播放放大动画
card.transform.DOScale(Vector3.one, scaleUpDuration).SetEase(Ease.OutBack);
}
else
{
Debug.LogWarning($"未找到ID为 '{id}' 的萌妖卡预制体!");
}
}
//Base.GlobalObj.GetComponent<gameGlobal>().CarryCardId.Clear();
}
}