CraftSelectionPanel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class CraftSelectionPanel : PopUpPanel
  6. {
  7. public GameObject craftItemPrefab;
  8. public RectTransform containerTrans;
  9. public GameObject closeBtn;
  10. public Text titleTxt;
  11. public bool useSwap;
  12. public Player myPlayer;
  13. void Start()
  14. {
  15. myPlayer = Session.GetInstance().GetBattleSession().myPlayer;
  16. List<CraftConfigData> list = CraftManager.GetInstance().GetDataList();
  17. for(int i=0; i<list.Count; i++)
  18. {
  19. CraftConfigData craftData = list[i];
  20. GameObject craftObj = Instantiate<GameObject>(craftItemPrefab);
  21. craftObj.transform.SetParent(containerTrans);
  22. craftObj.transform.localPosition = Vector3.zero;
  23. craftObj.transform.localScale = new Vector3(1f, 1f, 1f);
  24. CraftSelectionItem craftSelectionItem = craftObj.GetComponent<CraftSelectionItem>();
  25. craftSelectionItem.craftSelectionPanel = this;
  26. craftSelectionItem.index = i;
  27. craftSelectionItem.SetCraftData(craftData);
  28. }
  29. if(useSwap)
  30. titleTxt.text = Language.GetStr("GameInfo", "swapCraft");
  31. else
  32. titleTxt.text = Language.GetStr("GameInfo", "selectCraft");
  33. Session.GetInstance().GetBattleSession().GetBattleController().battleUI.powerIconContainer.Hide();
  34. }
  35. public static void SelectCraft(Player player, int craftId)
  36. {
  37. BattleSession battleSession = Session.GetInstance().GetBattleSession();
  38. if(currentPanel != null && currentPanel.useSwap)
  39. {
  40. if (player != null)
  41. {
  42. Player.Hero hero = player.GetHero();
  43. if(hero.GetCraft() != null)
  44. battleSession.GetMessageManager ().PrepareSwap (hero.GetCraft().id, craftId);
  45. }
  46. }
  47. else
  48. {
  49. Vector3 pos = battleSession.GetBattleController().GetMap().GetStartPosition(player);
  50. battleSession.GetMessageManager().SelectCraft(player, craftId, Map.XToColumn(pos.x), Map.ZToRow(pos.z));
  51. }
  52. }
  53. void FixedUpdate()
  54. {
  55. if(useSwap)
  56. {
  57. Player.Hero hero = myPlayer.GetHero();
  58. if(hero.GetCraft() == null || !hero.GetCraft().IsLeaveDamage())
  59. Close();
  60. }
  61. }
  62. void OnDestroy()
  63. {
  64. BattleController battleController = Session.GetInstance ().GetBattleSession ().GetBattleController ();
  65. if(battleController.GetCtrlCraft() != null)
  66. Session.GetInstance().GetBattleSession().GetBattleController().battleUI.powerIconContainer.Show();
  67. if(BattleController.battleType == BattleController.BattleType.Menu)
  68. Session.GetInstance ().GetBattleSession ().GetBattleController ().menuUI.startBtnContainer.Show();
  69. currentPanel = null;
  70. }
  71. public static bool IsShown()
  72. {
  73. return currentPanel != null;
  74. }
  75. private static CraftSelectionPanel currentPanel;
  76. public static CraftSelectionPanel Show(bool useSwap=false)
  77. {
  78. if(currentPanel == null)
  79. {
  80. GameObject panelObj = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/UI/Battle/CraftSelectionPanel"));
  81. currentPanel = panelObj.GetComponent<CraftSelectionPanel>();
  82. PopUpManager.AddToMainCanvas(panelObj);
  83. }
  84. currentPanel.useSwap = useSwap;
  85. currentPanel.closeBtn.SetActive(useSwap);
  86. if (useSwap) {
  87. Session.GetInstance ().GetBattleSession ().GetBattleController ().battleUI.powerIconContainer.Hide ();
  88. if(BattleController.battleType == BattleController.BattleType.Menu)
  89. Session.GetInstance ().GetBattleSession ().GetBattleController ().menuUI.startBtnContainer.Hide();
  90. }
  91. return currentPanel;
  92. }
  93. public static void Hide()
  94. {
  95. if(currentPanel != null)
  96. Destroy(currentPanel.gameObject);
  97. }
  98. }