BattleMainComp.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Collections;
  2. using Api;
  3. using Game;
  4. using HttpApi;
  5. using Message;
  6. using Newtonsoft.Json;
  7. using Sound;
  8. using TMPro;
  9. using UI;
  10. using UnityEngine;
  11. namespace Comp
  12. {
  13. public class BattleMainComp : MonoBehaviour
  14. {
  15. public GameObject mainCamera;
  16. public GameObject resultPanel;
  17. public TextMeshProUGUI localName, antiName;
  18. public volatile bool end = false;
  19. private bool _debug = false; // 是否单机测试模式
  20. private void Start()
  21. {
  22. SoundCore.Instance.PlaySound(SoundType.BattleBgmMain, SoundCtrl.BattleBgmPlayer, true, true);
  23. MessageComp.Instance.OnBattleClientMsg += OnClientMsg;
  24. MessageComp.Instance.OnBattlePlayerQuit += OnPlayerQuit;
  25. // #if UNITY_EDITOR
  26. // _debug = true;
  27. // #endif
  28. if (_debug)
  29. {
  30. var logs =
  31. "{\"fromPlayer\":100020,\"fromPlayerHp\":210,\"toPlayerHp\":210,\"toPlayer\":100019,\"fromCock\":1,\"toCock\":2,\"winPlayer\":100020,\"lostPlayer\":100019,\"diamond\":200,\"battleCount\":34,\"winPlayerHp\":30,\"logArray\":[{\"index\":33,\"logs\":[{\"id\":33,\"msg\":\"第33回合:玩家[100019]跳起来啄击,玩家[100020]受到24点伤害,剩下30血量\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":24},{\"id\":33,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":2,\"value\":0}]},{\"index\":11,\"logs\":[{\"id\":11,\"msg\":\"第11回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下150血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":11,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]},{\"index\":17,\"logs\":[{\"id\":17,\"msg\":\"第17回合:玩家[100019]普通啄击,玩家[100020]受到12点伤害,剩下150血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":12},{\"id\":17,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":2,\"value\":0}]},{\"index\":20,\"logs\":[{\"id\":20,\"msg\":\"第20回合:玩家[100019]跳起来啄击落空了,玩家[100020]受到0点伤害,剩下126血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0},{\"id\":20,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":27,\"logs\":[{\"id\":27,\"msg\":\"第27回合:玩家[100019]跳起来啄击落空了,玩家[100020]受到0点伤害,剩下78血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0},{\"id\":27,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":29,\"logs\":[{\"id\":29,\"msg\":\"第29回合:玩家[100019]普通啄击,玩家[100020]受到12点伤害,剩下54血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":12},{\"id\":29,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":2,\"value\":0}]},{\"index\":4,\"logs\":[{\"id\":4,\"msg\":\"第4回合:玩家[100020]普通啄击落空了,玩家[100019]受到0点伤害,剩下186血量\",\"from\":100020,\"to\":100019,\"type\":2,\"value\":0},{\"id\":4,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0}]},{\"index\":18,\"logs\":[{\"id\":18,\"msg\":\"第18回合:玩家[100019]普通啄击,玩家[100020]受到12点伤害,剩下138血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":12},{\"id\":18,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":23,\"logs\":[{\"id\":23,\"msg\":\"第23回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下90血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":23,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0}]},{\"index\":28,\"logs\":[{\"id\":28,\"msg\":\"第28回合:玩家[100019]普通啄击,玩家[100020]受到12点伤害,剩下66血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":12},{\"id\":28,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":34,\"logs\":[{\"id\":34,\"msg\":\"第34回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下-6血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":34,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]},{\"index\":6,\"logs\":[{\"id\":6,\"msg\":\"第6回合:玩家[100019]跳起来啄击落空了,玩家[100020]受到0点伤害,剩下198血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0},{\"id\":6,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":21,\"logs\":[{\"id\":21,\"msg\":\"第21回合:玩家[100019]跳起来啄击,玩家[100020]受到24点伤害,剩下102血量\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":24},{\"id\":21,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":24,\"logs\":[{\"id\":24,\"msg\":\"第24回合:玩家[100020]跳起来啄击,玩家[100019]受到24点伤害,剩下66血量\",\"from\":100020,\"to\":100019,\"type\":2,\"value\":24},{\"id\":24,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0}]},{\"index\":32,\"logs\":[{\"id\":32,\"msg\":\"第32回合:玩家[100020]普通啄击落空了,玩家[100019]受到0点伤害,剩下6血量\",\"from\":100020,\"to\":100019,\"type\":2,\"value\":0},{\"id\":32,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]},{\"index\":3,\"logs\":[{\"id\":3,\"msg\":\"第3回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下186血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":3,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]},{\"index\":15,\"logs\":[{\"id\":15,\"msg\":\"第15回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下114血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":15,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0}]},{\"index\":16,\"logs\":[{\"id\":16,\"msg\":\"第16回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下102血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":16,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]},{\"index\":12,\"logs\":[{\"id\":12,\"msg\":\"第12回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下138血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":12,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]},{\"index\":14,\"logs\":[{\"id\":14,\"msg\":\"第14回合:玩家[100019]跳起来啄击,玩家[100020]受到24点伤害,剩下162血量\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":24},{\"id\":14,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":2,\"value\":0}]},{\"index\":19,\"logs\":[{\"id\":19,\"msg\":\"第19回合:玩家[100019]普通啄击,玩家[100020]受到12点伤害,剩下126血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":12},{\"id\":19,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":22,\"logs\":[{\"id\":22,\"msg\":\"第22回合:玩家[100019]跳起来啄击,玩家[100020]受到24点伤害,剩下78血量\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":24},{\"id\":22,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":25,\"logs\":[{\"id\":25,\"msg\":\"第25回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下54血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":25,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0}]},{\"index\":1,\"logs\":[{\"id\":1,\"msg\":\"第1回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下198血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":1,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0}]},{\"index\":5,\"logs\":[{\"id\":5,\"msg\":\"第5回合:玩家[100019]普通啄击,玩家[100020]受到12点伤害,剩下198血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":12},{\"id\":5,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":9,\"logs\":[{\"id\":9,\"msg\":\"第9回合:玩家[100019]普通啄击,玩家[100020]受到12点伤害,剩下186血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":12},{\"id\":9,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":26,\"logs\":[{\"id\":26,\"msg\":\"第26回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下42血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":26,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0}]},{\"index\":30,\"logs\":[{\"id\":30,\"msg\":\"第30回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下30血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":30,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0}]},{\"index\":2,\"logs\":[{\"id\":2,\"msg\":\"第2回合:玩家[100020]跳起来啄击落空了,玩家[100019]受到0点伤害,剩下198血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0},{\"id\":2,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]},{\"index\":7,\"logs\":[{\"id\":7,\"msg\":\"第7回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下174血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":7,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0}]},{\"index\":8,\"logs\":[{\"id\":8,\"msg\":\"第8回合:玩家[100019]跳起来啄击落空了,玩家[100020]受到0点伤害,剩下198血量\",\"from\":100019,\"to\":100020,\"type\":1,\"value\":0},{\"id\":8,\"msg\":\"\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":0}]},{\"index\":10,\"logs\":[{\"id\":10,\"msg\":\"第10回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下162血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":10,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]},{\"index\":13,\"logs\":[{\"id\":13,\"msg\":\"第13回合:玩家[100020]普通啄击,玩家[100019]受到12点伤害,剩下126血量\",\"from\":100020,\"to\":100019,\"type\":1,\"value\":12},{\"id\":13,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]},{\"index\":31,\"logs\":[{\"id\":31,\"msg\":\"第31回合:玩家[100020]跳起来啄击,玩家[100019]受到24点伤害,剩下6血量\",\"from\":100020,\"to\":100019,\"type\":2,\"value\":24},{\"id\":31,\"msg\":\"\",\"from\":100019,\"to\":100020,\"type\":2,\"value\":0}]}],\"dbId\":0}";
  32. var obj = JsonConvert.DeserializeObject<BattleDetailListObj>(logs);
  33. StartCoroutine(StartGame(obj));
  34. }
  35. else
  36. {
  37. localName.text = AccountManager.Instance.selfInfo.name;
  38. Debug.Log("curSession===" + GameCore.Instance.curBattleSession);
  39. ApiComp.Instance.GetBattleDetail(GameCore.Instance.curBattleSession,
  40. (obj) => { StartCoroutine(StartGame(obj)); }, (code, msg) =>
  41. {
  42. // TODO Do nothing
  43. Debug.LogWarning("request battle err " + msg);
  44. });
  45. }
  46. }
  47. // ReSharper disable Unity.PerformanceAnalysis
  48. private IEnumerator StartGame(BattleDetailListObj obj)
  49. {
  50. Debug.Log("Debug模式 " + _debug);
  51. var localPlayerId = _debug ? obj.fromPlayer : AccountManager.Instance.selfInfo.playerId; // 兼容测试模式
  52. GamePlayer localPlayer, antiPlayer;
  53. if (localPlayerId == obj.fromPlayer)
  54. {
  55. localPlayer = new GamePlayer()
  56. {
  57. cockId = obj.fromCock, playerId = obj.fromPlayer, hp = obj.fromPlayerHp,
  58. runTimeHp = obj.fromPlayerHp
  59. };
  60. antiPlayer = new GamePlayer()
  61. { cockId = obj.toCock, playerId = obj.toPlayer, hp = obj.toPlayerHp, runTimeHp = obj.toPlayerHp };
  62. }
  63. else
  64. {
  65. localPlayer = new GamePlayer()
  66. { cockId = obj.toCock, playerId = obj.toPlayer, hp = obj.toPlayerHp, runTimeHp = obj.toPlayerHp };
  67. antiPlayer = new GamePlayer()
  68. {
  69. cockId = obj.fromCock, playerId = obj.fromPlayer, hp = obj.fromPlayerHp,
  70. runTimeHp = obj.fromPlayerHp
  71. };
  72. }
  73. Debug.Log("local player is " + localPlayer.playerId + "==== anti player is " + antiPlayer.playerId);
  74. ApiComp.Instance.QueryPlayerInfo(antiPlayer.playerId, (data) => { antiName.text = data.name; },
  75. (code, msg) => { antiName.text = "net error"; });
  76. GameCore.Instance.Init(localPlayer, antiPlayer, gameObject, mainCamera, obj, resultPanel);
  77. yield return new WaitForEndOfFrame();
  78. }
  79. private void OnClientMsg(int playerId, MsgContent content)
  80. {
  81. if (content.contentType == "lift")
  82. {
  83. Debug.Log("接收lift");
  84. GameCore.Instance.GetCurState().LiftCock();
  85. }
  86. }
  87. private void OnPlayerQuit(int playerId, MsgContent content)
  88. {
  89. Debug.Log("接收quit" + playerId);
  90. if (playerId != AccountManager.Instance.selfInfo.playerId) // 其他玩家退出战斗
  91. {
  92. GameCore.Instance.winPlayerId = AccountManager.Instance.selfInfo.playerId;
  93. GameCore.Instance.TransitionToState(GameCore.Instance.endState);
  94. }
  95. }
  96. public void Lift()
  97. {
  98. Debug.Log("click lift");
  99. ApiComp.Instance.MsgInBattle(GameCore.Instance.curBattleSession, new MsgContent() { contentType = "lift" });
  100. GameCore.Instance.GetCurState().LiftCock();
  101. }
  102. public void QuitBattle()
  103. {
  104. var self = this;
  105. ApiComp.Instance.playerQuitGame(GameCore.Instance.curBattleSession, (data) => { self.ForceEnd(); },
  106. (code, msg) => { self.ForceEnd(); });
  107. }
  108. public void ClickResult()
  109. {
  110. GameStartJumper.BackToUI();
  111. }
  112. private void Update()
  113. {
  114. if (!end) return;
  115. end = false;
  116. StartCoroutine(End());
  117. }
  118. private static IEnumerator End()
  119. {
  120. yield return new WaitForSeconds(2f);
  121. GameCore.Instance.TransitionToState(GameCore.Instance.endState);
  122. }
  123. // ReSharper disable Unity.PerformanceAnalysis
  124. private void ForceEnd()
  125. {
  126. GameCore.Instance.showResultPanel = false;
  127. GameCore.Instance.TransitionToState(GameCore.Instance.endState);
  128. GameStartJumper.BackToUI();
  129. }
  130. }
  131. }