GameBattleState.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Timers;
  2. using Comp;
  3. using HttpApi;
  4. using UnityEngine;
  5. namespace Game
  6. {
  7. public class GameBattleState : GameState
  8. {
  9. private Timer _battleTimer;
  10. private bool _runBattle = false;
  11. public GameBattleState(GameCore gameCore) : base(gameCore)
  12. {
  13. }
  14. public override void PlayBattle()
  15. {
  16. // 啥都不做
  17. }
  18. public override void BattleStart()
  19. {
  20. Debug.Log("battle start");
  21. if (_battleTimer == null)
  22. {
  23. Debug.Log("battle timer start");
  24. foreach (var cockActionComp in _gameCore.cockActionComps)
  25. {
  26. if (cockActionComp.Key == _gameCore.localPlayer.playerId)
  27. {
  28. cockActionComp.Value.gameObject.GetComponent<CockActionComp>().rightForward = true;
  29. }
  30. else
  31. {
  32. cockActionComp.Value.gameObject.GetComponent<CockActionComp>().rightForward = false;
  33. }
  34. cockActionComp.Value.gameObject.GetComponent<CockActionComp>().CockController.JumpAndAttack();
  35. }
  36. var followObject = _gameCore.GetCockActionCompByPlayerId(_gameCore.localPlayer.playerId).gameObject;
  37. _gameCore.camera.AddComponent<CameraFollow>().SetTarget(followObject.transform);
  38. _battleTimer = new Timer();
  39. _battleTimer.Interval = 1000;
  40. _battleTimer.Enabled = true;
  41. _battleTimer.Elapsed += OnBattleTimer;
  42. _battleTimer.Start();
  43. }
  44. }
  45. public override void LiftCock()
  46. {
  47. if (_gameCore.liftTimes > 0)
  48. _gameCore.TransitionToState(_gameCore.liftState);
  49. }
  50. public override void ExitBattle()
  51. {
  52. _runBattle = false;
  53. if (_battleTimer != null)
  54. {
  55. _battleTimer.Dispose();
  56. _battleTimer = null;
  57. }
  58. }
  59. public override void EndBattle()
  60. {
  61. }
  62. public override void EnterState()
  63. {
  64. _runBattle = true;
  65. }
  66. public override void ExitState()
  67. {
  68. _runBattle = false;
  69. }
  70. private void OnBattleTimer(object o, ElapsedEventArgs args)
  71. {
  72. if (!_runBattle)
  73. return;
  74. if (_gameCore.battleDetailObjs == null || _gameCore.battleDetailObjs.Count < 0)
  75. return;
  76. ExecuteBattleObj(_gameCore.battleDetailObjs.Last.Value);
  77. _gameCore.battleDetailObjs.RemoveLast();
  78. if (_gameCore.battleDetailObjs.Count != 0) return;
  79. Debug.Log("end battle");
  80. ExecuteLastBattleObj();
  81. _gameCore.battleMainComp.end = true;
  82. _battleTimer.Dispose();
  83. _battleTimer = null;
  84. }
  85. private void ExecuteBattleObj(BattleDetailObj battleDetailObj)
  86. {
  87. for (int i = 0; i <= battleDetailObj.logs.Length - 1; i++)
  88. {
  89. var log = battleDetailObj.logs[i];
  90. var attackCockActionComp = _gameCore.GetCockActionCompByPlayerId(log.from);
  91. attackCockActionComp.SetHighJump(i == 0);
  92. var counterattackCockActionComp = _gameCore.GetCockActionCompByPlayerId(log.to);
  93. var counterattackPlayer = _gameCore.localPlayer.playerId == log.to
  94. ? _gameCore.localPlayer
  95. : _gameCore.antiPlayer;
  96. counterattackPlayer.runTimeHp -= log.value;
  97. if (i == 0 && log.value == 0) // 攻击落空了
  98. {
  99. counterattackCockActionComp.CreateMiss();
  100. }
  101. else if (i == 0 && log.value > 0)
  102. {
  103. var cockHpComp = _gameCore.GetCockHpCompByPlayerId(log.to);
  104. cockHpComp.reduceHp = log.value;
  105. }
  106. switch (log.type)
  107. {
  108. case 1:
  109. attackCockActionComp.curAction = attackCockActionComp.CockController.JumpAndAttack;
  110. break;
  111. case 2:
  112. attackCockActionComp.curAction = attackCockActionComp.CockController.Attack;
  113. break;
  114. }
  115. }
  116. }
  117. private void ExecuteLastBattleObj()
  118. {
  119. var fromComp = _gameCore.GetCockActionCompByPlayerId(_gameCore.winPlayerId);
  120. int toPlayerId;
  121. if (_gameCore.winPlayerId == _gameCore.localPlayer.playerId)
  122. {
  123. toPlayerId = _gameCore.antiPlayer.playerId;
  124. }
  125. else
  126. {
  127. toPlayerId = _gameCore.localPlayer.playerId;
  128. }
  129. var toComp = _gameCore.GetCockActionCompByPlayerId(toPlayerId);
  130. fromComp.SetHighJump(true);
  131. toComp.SetHighJump(false);
  132. fromComp.curAction = fromComp.CockController.JumpAndAttack;
  133. toComp.curAction = toComp.CockController.Die;
  134. }
  135. }
  136. }