GameBattleState.cs 5.3 KB

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