using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using FSEvent; using FSBattle; using FSCard; using FSRole; using FSAssist; public class CardPanelManager : MonoBehaviour { public Transform cardPanel; public GameObject cardPrefab; // Use this for initialization void Start () { InitEvents(); } // Update is called once per frame void Update () { } private void OnDestroy() { RemoveEvents(); } /* * 注册监听事件 */ private void InitEvents() { print("Card Panel 注册事件 ... "); EventListener.Instance.RegisterEvent(EventEnum.EVENT_PLAYER_SHUFFLING_CARD, ShufflingCard); EventListener.Instance.RegisterEvent(EventEnum.EVENT_CLEAR_HAND_CARDS, ClearHandCards); EventListener.Instance.RegisterEvent(EventEnum.EVENT_CLEAR_COMPAIRE_CARDS, ClearCompaireCards); EventListener.Instance.RegisterEvent(EventEnum.EVENT_REMOVE_TARGET_CARD, RemoveTargetCard); EventListener.Instance.RegisterEvent(EventEnum.EVENT_ENEMY_DEAL_CARD, EnemyDealCard); EventListener.Instance.RegisterEvent(EventEnum.EVENT_ARMY_DEAL_CARD, ArmyDealCard); } /* * 移除监听事件 */ private void RemoveEvents() { EventListener.Instance.RemoveEvent(EventEnum.EVENT_PLAYER_SHUFFLING_CARD); EventListener.Instance.RemoveEvent(EventEnum.EVENT_CLEAR_HAND_CARDS); EventListener.Instance.RemoveEvent(EventEnum.EVENT_CLEAR_COMPAIRE_CARDS); EventListener.Instance.RemoveEvent(EventEnum.EVENT_REMOVE_TARGET_CARD); EventListener.Instance.RemoveEvent(EventEnum.EVENT_ENEMY_DEAL_CARD); EventListener.Instance.RemoveEvent(EventEnum.EVENT_ARMY_DEAL_CARD); } /* * 发牌 */ private void ShufflingCard() { // 如果手牌小于5,则 if (BattleFieldManager.Instance.ArmyHandCards.Count < 5 && BattleFieldManager.Instance.EnemyHandCards.Count < 5) { AddPlayerCard(); AddEnemyCard(); Invoke("ShufflingCard", 1.0f); } else { Invoke("NoticeEnemyDeal", 2.0f); } } /* * 通知可以进入敌人出牌的回合了 */ private void NoticeEnemyDeal() { EventListener.Instance.PostEvent(EventEnum.EVENT_ENTER_ENEMY_DEAL_ROUND); } /* * 添加卡牌 */ public void AddPlayerCard() { // 保证卡池中有牌 if (BattleFieldManager.Instance.ArmyTakePool.Count > 0) { // 从卡池中取出一张牌放入手牌中 Transform cardTrs = (Transform)BattleFieldManager.Instance.ArmyTakePool[0]; // 加入手牌 BattleFieldManager.Instance.ArmyHandCards.Add(cardTrs); // 从卡池中移除 BattleFieldManager.Instance.ArmyTakePool.Remove(cardTrs); cardTrs.SetParent(cardPanel, false); // 添加卡片事件组件,如果没有,则添加事件 CardAction action = cardTrs.GetComponent(); if (action == null){ cardTrs.gameObject.AddComponent(); } // 通知修改 Dictionary info = new Dictionary(); info.Add("index", 0); info.Add("num", BattleFieldManager.Instance.ArmyTakePool.Count); EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_TABLE_INFO, info); } else { // 如果卡池中没有牌了,将弃牌区的卡全部放回卡池 foreach(Transform cardTrs in BattleFieldManager.Instance.ArmyThrowPool) { BattleFieldManager.Instance.ArmyTakePool.Add(cardTrs); } BattleFieldManager.Instance.ArmyThrowPool.Clear(); // 更新牌池数量显示 Dictionary info = new Dictionary(); info.Add("index", 0); info.Add("num", BattleFieldManager.Instance.ArmyThrowPool.Count); EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_TABLE_INFO, info); info["index"] = 1; info["num"] = 0; EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_TABLE_INFO, info); } ArrangePlayerCards(); } /* * 添加敌人卡牌 */ private void AddEnemyCard() { // 获取位置 int index = BattleFieldManager.Instance.EnemyHandCards.Count; // 从卡池中获取卡片 if (BattleFieldManager.Instance.EnemyTakePool.Count > 0) { // 从卡池取出手牌 Transform cardTrs = (Transform)BattleFieldManager.Instance.EnemyTakePool[0]; // 加入手牌 BattleFieldManager.Instance.EnemyHandCards.Add(cardTrs); // 从卡池中移除 BattleFieldManager.Instance.EnemyTakePool.Remove(cardTrs); cardTrs.SetParent(cardPanel); cardTrs.localScale = new Vector3(AssistConfig.CardScale, AssistConfig.CardScale, 1); cardTrs.localPosition = new Vector3(Screen.width / 2, Screen.height / 2 + index * 30, 0); // 如果有卡片事件组件,如果有则将事件移除 //CardAction action = cardTrs.GetComponent(); //if (action != null) { // Object.Destroy(action); //} } else { // 如果卡池中没有牌了,将弃牌区的卡全部放回卡池 foreach(Transform cardTrs in BattleFieldManager.Instance.EnemyThrowPool) { BattleFieldManager.Instance.EnemyTakePool.Add(cardTrs); } BattleFieldManager.Instance.EnemyThrowPool.Clear(); } } /* * 重新排列卡牌 */ private void ArrangePlayerCards() { // 空隙 float space = 50; // 配置的半径 float radius = 400; // 获取偏移的角度 float angle = Mathf.Atan(space / radius) * 180 / Mathf.PI; // 遍历数组 for (int i = 0; i < BattleFieldManager.Instance.ArmyHandCards.Count; i++) { // 重定义位置 float loc = i - (float)(BattleFieldManager.Instance.ArmyHandCards.Count - 1) / 2; Transform cardTransform = (Transform)BattleFieldManager.Instance.ArmyHandCards[i]; // 设置位置 cardTransform.localPosition = new Vector3(loc * space, 0, 0); CardAction action = cardTransform.GetComponent(); action.StartPos = cardTransform.position; // 设置角度 cardTransform.localRotation = Quaternion.Euler(0, 0, (int)(angle * (loc * -1))); float scale = 1 - (Mathf.Abs(loc) / 5); cardTransform.localScale = new Vector3(scale, scale, 1); } } /* * 玩家出牌,由外部通知进行处理 */ private void ArmyDealCard(Dictionary info) { print("我的牌出了 ... "); Transform cardTrs = (Transform)info["card"]; int index = (int)info["index"]; // 将传入的牌从手牌数组中移,再加入到比较牌数组中 BattleFieldManager.Instance.ArmyHandCards.Remove(cardTrs); BattleFieldManager.Instance.AddCardToIndex(AssistConfig.Army, index, cardTrs); // 重新排列 ArrangePlayerCards(); } /* * 敌人出牌 */ private void EnemyDealCard() { // 敌人出牌 print("敌人开始出牌 ... "); int index = 0; // 从数组中选出几张牌放入卡牌管理器的选择数组中 while (BattleFieldManager.Instance.EnemyHandCards.Count > 0) { Transform cardTrs = (Transform)BattleFieldManager.Instance.EnemyHandCards[0]; // 根据卡片的类型来处理如角色这样的属性 Card card = cardTrs.GetComponent(); // 选择的卡片如果花费过高,则结束出牌 if (card.Cost > card.Source.CurrentAttr.Mp) { break; } // 将此卡牌放入选择数组中 BattleFieldManager.Instance.AddCardToCpArray(AssistConfig.Enemy, cardTrs); BattleFieldManager.Instance.EnemyHandCards.Remove(cardTrs); card.Source.CurrentAttr.Mp -= card.Cost; if (card.Type == CardType.CARD_TYPE_ATTACK) { // 设置我方为卡片处理对象 int random = Random.Range(0, BattleFieldManager.Instance.ArmyArray.Count); Transform trs = (Transform)BattleFieldManager.Instance.RoleArray[random]; if (trs != null) { Role role = trs.GetComponent(); if (role != null) { card.Target = role; } } } else { // 设置敌方为处理对象 int random = Random.Range(0, BattleFieldManager.Instance.EnemyArray.Count); Transform trs = (Transform)BattleFieldManager.Instance.EnemyArray[random]; if (trs != null) { Role role = trs.GetComponent(); if (role != null) { card.Target = role; } } } // 第奇数个和偶数个处理 if (index % 2 == 0) { // 偶数个 cardTrs.position = new Vector3(index / 2 * AssistConfig.CpCardWidthSpace * -1 + Screen.width / 2, Screen.height / 2 + AssistConfig.CpCardHeightSpace, 0); } else { // 奇数个 float loc = (float)index / 2 + 0.5f; cardTrs.position = new Vector3(loc * AssistConfig.CpCardWidthSpace + Screen.width / 2, Screen.height / 2 + AssistConfig.CpCardHeightSpace, 0); } index++; } Invoke("NotifyPlayerDeal", 0.2f); } /* * 通知玩家出牌 */ private void NotifyPlayerDeal() { EventListener.Instance.PostEvent(EventEnum.EVENT_ENTER_PLAYER_DEAL_ROUND); } /* * 移除指定的卡片 */ private void RemoveTargetCard(Dictionary info) { // 首先获取卡片 Transform card = (Transform)info["card"]; // 从视图和数组中 Destroy(card.gameObject); BattleFieldManager.Instance.ArmyHandCards.Remove(card); // 重新排列 ArrangePlayerCards(); } /* * 清理手牌 */ private void ClearHandCards() { // 清理我方手牌 while (BattleFieldManager.Instance.ArmyHandCards.Count > 0) { Transform cardTrs = (Transform)BattleFieldManager.Instance.ArmyHandCards[0]; // 首先将卡片从数组移除,计入弃牌数组 BattleFieldManager.Instance.ArmyHandCards.Remove(cardTrs); BattleFieldManager.Instance.ArmyThrowPool.Add(cardTrs); Dictionary info = new Dictionary(); info.Add("card", cardTrs); info.Add("num", BattleFieldManager.Instance.ArmyThrowPool.Count); info.Add("index", 1); EventListener.Instance.PostEvent(EventEnum.EVENT_ERASE_CARD, info); EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_TABLE_INFO, info); } // 清理敌方手牌 while (BattleFieldManager.Instance.EnemyHandCards.Count > 0) { Transform cardTrs = (Transform)BattleFieldManager.Instance.EnemyHandCards[0]; // 将卡片从数组移除,放入弃牌卡组中 BattleFieldManager.Instance.EnemyHandCards.Remove(cardTrs); BattleFieldManager.Instance.ArmyThrowPool.Add(cardTrs); // 通知更新位置 Dictionary info = new Dictionary(); info.Add("card", cardTrs); EventListener.Instance.PostEvent(EventEnum.EVENT_ERASE_CARD, info); } EventListener.Instance.PostEvent(EventEnum.EVENT_ENTER_PLAY_A_HAND_ROUND); } /* * 清理比较牌 */ private void ClearCompaireCards() { // 清理比较牌 while (BattleFieldManager.Instance.CpArray.Count > 0) { Dictionary cpCard = (Dictionary)BattleFieldManager.Instance.CpArray[0]; // 提取两张牌 // 处理我方卡片 if (cpCard.ContainsKey(AssistConfig.Army)) { Transform armyCardTrs = cpCard[AssistConfig.Army]; // 将卡片移除,加入弃牌数组 BattleFieldManager.Instance.ArmyThrowPool.Add(armyCardTrs); cpCard.Remove(AssistConfig.Army); Dictionary eraseInfo = new Dictionary(); eraseInfo.Add("card", armyCardTrs); EventListener.Instance.PostEvent(EventEnum.EVENT_ERASE_CARD, eraseInfo); } // 清理敌方数据 if (cpCard.ContainsKey(AssistConfig.Enemy)) { Transform enemyCardTrs = cpCard[AssistConfig.Enemy]; // 将卡片移除,加入弃牌数组 BattleFieldManager.Instance.EnemyThrowPool.Add(enemyCardTrs); cpCard.Remove(AssistConfig.Enemy); Dictionary eraseInfo = new Dictionary(); eraseInfo.Add("card", enemyCardTrs); EventListener.Instance.PostEvent(EventEnum.EVENT_ERASE_CARD, eraseInfo); } BattleFieldManager.Instance.CpArray.RemoveAt(0); } // 通知更新数据 Dictionary info = new Dictionary(); info.Add("num", BattleFieldManager.Instance.ArmyThrowPool.Count); info.Add("index", 1); EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_TABLE_INFO, info); // 清理比较牌 BattleFieldManager.Instance.CpArray.Clear(); } }