GameBattleState.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if (i == 0 && log.value == 0) // 攻击落空了
  94. {
  95. counterattackCockActionComp.CreateMiss();
  96. }
  97. else if (i == 0 && log.value > 0)
  98. {
  99. var cockHpComp = _gameCore.GetCockHpCompByPlayerId(log.to);
  100. cockHpComp.reduceHp = log.value;
  101. }
  102. switch (log.type)
  103. {
  104. case 1:
  105. attackCockActionComp.curAction = attackCockActionComp.CockController.JumpAndAttack;
  106. break;
  107. case 2:
  108. attackCockActionComp.curAction = attackCockActionComp.CockController.Attack;
  109. break;
  110. }
  111. }
  112. }
  113. private void ExecuteLastBattleObj()
  114. {
  115. var fromComp = _gameCore.GetCockActionCompByPlayerId(_gameCore.winPlayerId);
  116. int toPlayerId;
  117. if (_gameCore.winPlayerId == _gameCore.localPlayer.playerId)
  118. {
  119. toPlayerId = _gameCore.antiPlayer.playerId;
  120. }
  121. else
  122. {
  123. toPlayerId = _gameCore.localPlayer.playerId;
  124. }
  125. var toComp = _gameCore.GetCockActionCompByPlayerId(toPlayerId);
  126. fromComp.SetHighJump(true);
  127. toComp.SetHighJump(false);
  128. fromComp.curAction = fromComp.CockController.JumpAndAttack;
  129. toComp.curAction = toComp.CockController.Die;
  130. }
  131. }
  132. }