PreparePageComp.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using Api;
  3. using Message;
  4. using Plugins.CxShine.page;
  5. using UI.BattleRequest;
  6. using UI.Common;
  7. using Unity.VisualScripting;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace UI
  11. {
  12. public class PreparePageComp : MonoBehaviour
  13. {
  14. public Text MyNameText;
  15. public Text EnemyNameText;
  16. public Text TimeText;
  17. public CockUIItemComp MyCockUIComp;
  18. public CockUIItemComp EnemyCockUIComp;
  19. public DiamondChooseComp DiamondChooseComp;
  20. public GameObject GameStartPanel;
  21. private int AllTimeSeconds = 10;
  22. private string _battleSession;
  23. private int _enemyId;
  24. private int currentTimeSeconds;
  25. private static readonly string ChooseCock = "ChooseCock";
  26. private static readonly string SelectDiamond = "SelectDiamond";
  27. private bool _ready;
  28. // private int _selectDiamond;
  29. // private bool _gameStarted;
  30. public void init(string battleSession, int enemy)
  31. {
  32. _battleSession = battleSession;
  33. _enemyId = enemy;
  34. initMyPlayerInfo();
  35. initEnemyInfo(_enemyId);
  36. InvokeRepeating(nameof(TrickTimeSecond), 0, 1.0f);
  37. DiamondChooseComp.OnDiamondChange += diamond =>
  38. {
  39. // _selectDiamond = diamond;
  40. if (_ready)
  41. {
  42. return;
  43. }
  44. ApiComp.Instance.MsgInBattle(_battleSession, new MsgContent
  45. {
  46. contentType = MsgContentType.SelectDiamond,
  47. diamond = diamond
  48. });
  49. };
  50. }
  51. private void initMyPlayerInfo()
  52. {
  53. MyNameText.text = AccountManager.Instance.selfInfo.name;
  54. MyCockUIComp.ModeCockMy();
  55. // 点击更改cock的时候,发送消息
  56. MyCockUIComp.OnCockChanged += () =>
  57. {
  58. ApiComp.Instance.MsgInBattle(_battleSession, new MsgContent
  59. {
  60. contentType = ChooseCock,
  61. cockId = MyCockUIComp.GetSelectCockId(),
  62. });
  63. };
  64. }
  65. private void TrickTimeSecond()
  66. {
  67. // if (_gameStarted)
  68. // {
  69. // TimeText.text = "Start";
  70. // return;
  71. // }
  72. if (_ready)
  73. {
  74. TimeText.text = "Ready";
  75. return;
  76. }
  77. else
  78. {
  79. currentTimeSeconds += 1;
  80. TimeText.text = (AllTimeSeconds - currentTimeSeconds) + "";
  81. }
  82. if (currentTimeSeconds == AllTimeSeconds)
  83. {
  84. A__ClickReady();
  85. _ready = true;
  86. }
  87. }
  88. public void A__ClickReady()
  89. {
  90. if (_ready)
  91. {
  92. return;
  93. }
  94. MyCockUIComp.DisplayReadyImage();
  95. MyCockUIComp.HideSelectPanel();
  96. int selectCockId = MyCockUIComp.GetSelectCockId();
  97. int diamond = DiamondChooseComp.getSelectDiamond();
  98. ApiComp.Instance.PlayerReady(_battleSession, selectCockId, diamond);
  99. Invoke(nameof(WaitClose), AllTimeSeconds - currentTimeSeconds + 10f);
  100. }
  101. private void WaitClose()
  102. {
  103. // gameObject.GetComponent<PageComp>().A__ClosePage();
  104. }
  105. private void initEnemyInfo(int enemyId)
  106. {
  107. EnemyCockUIComp.ModeCockEnemy();
  108. ApiComp.Instance.QueryPlayerInfo(enemyId, info => { EnemyNameText.text = "" + info.name; }, null);
  109. }
  110. private void Start()
  111. {
  112. MessageComp.Instance.OnBattleClientMsg += OnReceivedBattleMsg;
  113. MessageComp.Instance.OnBattlePlayerReady += OnBattlePlayerReady;
  114. MessageComp.Instance.OnBattleStart += OnBattleStart;
  115. HallComp.Instance.CloseLoading();
  116. }
  117. private void OnDestroy()
  118. {
  119. MessageComp.Instance.OnBattleClientMsg -= OnReceivedBattleMsg;
  120. MessageComp.Instance.OnBattlePlayerReady -= OnBattlePlayerReady;
  121. MessageComp.Instance.OnBattleStart -= OnBattleStart;
  122. MessageComp.Instance.ClearMessage();
  123. BattleInviteManager.Instance.ClosePreparePage();
  124. }
  125. private void OnBattleStart(MsgContent msgContent)
  126. {
  127. // _gameStarted = true;
  128. GameStartPanel.SetActive(true);
  129. DiamondChooseComp.gameStart();
  130. Debug.Log("收到战斗开始的消息了" + msgContent);
  131. GetComponent<PageComp>().A__ClosePage();
  132. GameStartJumper.JumpGameScene(msgContent.battleSession);
  133. }
  134. private void OnBattlePlayerReady(int playerId, MsgContent msgContent)
  135. {
  136. Debug.Log("Enemy Ready:" + playerId);
  137. if (playerId == _enemyId)
  138. {
  139. EnemyCockUIComp.DisplayReadyImage();
  140. }
  141. }
  142. private void OnReceivedBattleMsg(int fromPlayer, MsgContent msgContent)
  143. {
  144. // Debug.Log("收到敌人的消息了" + fromPlayer + ":" + msgContent);
  145. // Debug.Log("_enemyId" + _enemyId);
  146. // Debug.Log(">>>>>>>>>>>>");
  147. // Debug.Log(msgContent);
  148. if (fromPlayer != _enemyId)
  149. {
  150. Debug.Log("收非常规的消息 enemy:" + _enemyId + "from:" + fromPlayer);
  151. return;
  152. }
  153. if (ChooseCock.Equals(msgContent.contentType))
  154. {
  155. // TipsComp.ShowTips("Enemy Choose Cock:" + msgContent.cockId);
  156. EnemyCockUIComp.ChangeCook(msgContent.cockId);
  157. }
  158. else if (SelectDiamond.Equals(msgContent.contentType))
  159. {
  160. // TipsComp.ShowTips("Enemy Select Diamond:" + msgContent.diamond);
  161. }
  162. }
  163. }
  164. }