GameCore.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using Comp;
  4. using HttpApi;
  5. using Ragdoll;
  6. using UnityEngine;
  7. namespace Game
  8. {
  9. public class GameCore : RdSingletonStateMachine<GameState, GameCore>
  10. {
  11. [RdReset] public GameState pendingState;
  12. [RdReset] public GameState battleState;
  13. [RdReset] public GameState liftState;
  14. [RdReset] public GameState endState;
  15. [RdReset] public GamePlayer localPlayer;
  16. [RdReset] public GamePlayer antiPlayer;
  17. [RdReset] public int winPlayerId;
  18. [RdReset] public int diamond;
  19. [RdReset] public Dictionary<int, CockActionComp> cockActionComps;
  20. [RdReset] public Dictionary<int, CockHpComp> cockHpComps;
  21. [RdReset] public Dictionary<int, MasterActionComp> masterDict;
  22. [RdReset] public GameObject parent;
  23. [RdReset] public BattleMainComp battleMainComp;
  24. [RdReset] public GameObject camera;
  25. [RdReset] public GameObject resultPanel;
  26. [RdReset] public LinkedList<BattleDetailObj> battleDetailObjs;
  27. [RdReset] public (bool, bool) masterBack; // 主人复位
  28. [RdReset] public string curBattleSession;
  29. [RdReset(2)] public int liftTimes;
  30. [RdReset] public bool inBattleState = false;
  31. public GameCore()
  32. {
  33. }
  34. public void Exit()
  35. {
  36. currentState = null;
  37. RdResetTool.ResetAttribute(this);
  38. }
  39. public void Init(GamePlayer localPlayer, GamePlayer antiPlayer, GameObject parent, GameObject camera,
  40. BattleDetailListObj jsonLogs, GameObject resultPanel)
  41. {
  42. if (currentState != null) // 防止多重初始化
  43. return;
  44. pendingState = new GamePendingState(this);
  45. battleState = new GameBattleState(this);
  46. liftState = new GameLiftState(this);
  47. endState = new GameEndState(this);
  48. // 举J次数限制
  49. liftTimes = 2;
  50. this.localPlayer = localPlayer;
  51. this.antiPlayer = antiPlayer;
  52. this.parent = parent;
  53. this.battleMainComp = parent.GetComponent<BattleMainComp>();
  54. this.camera = camera;
  55. this.resultPanel = resultPanel;
  56. cockActionComps = new Dictionary<int, CockActionComp>();
  57. cockHpComps = new Dictionary<int, CockHpComp>();
  58. masterDict = new Dictionary<int, MasterActionComp>();
  59. winPlayerId = jsonLogs.winPlayer;
  60. diamond = jsonLogs.diamond;
  61. Array.Sort(jsonLogs.logArray, (obj1, obj2) => obj1.index.CompareTo(obj2.index));
  62. battleDetailObjs = new LinkedList<BattleDetailObj>();
  63. foreach (var log in jsonLogs.logArray)
  64. {
  65. battleDetailObjs.AddFirst(log);
  66. }
  67. Start();
  68. }
  69. public override void TransitionToState(GameState nextState)
  70. {
  71. inBattleState = nextState == battleState;
  72. base.TransitionToState(nextState);
  73. }
  74. protected override void Start()
  75. {
  76. TransitionToState(pendingState);
  77. currentState.PlayBattle();
  78. }
  79. public ICockController GetController(GamePlayer gamePlayer)
  80. {
  81. return GetControllerByPlayerId(gamePlayer.playerId);
  82. }
  83. private ICockController GetControllerByPlayerId(int playerId)
  84. {
  85. cockActionComps.TryGetValue(playerId, out var cockActionComp);
  86. return cockActionComp != null ? cockActionComp.CockController : null;
  87. }
  88. public CockActionComp GetCockActionCompByPlayerId(int playerId)
  89. {
  90. cockActionComps.TryGetValue(playerId, out var cockActionComp);
  91. return cockActionComp != null ? cockActionComp : null;
  92. }
  93. public CockHpComp GetCockHpCompByPlayerId(int playerId)
  94. {
  95. cockHpComps.TryGetValue(playerId, out var cockHpComp);
  96. return cockHpComp != null ? cockHpComp : null;
  97. }
  98. public void ReportMasterBack(bool leftSide)
  99. {
  100. if (leftSide)
  101. {
  102. masterBack.Item1 = true;
  103. }
  104. else
  105. {
  106. masterBack.Item2 = true;
  107. }
  108. if (masterBack.Item1 && masterBack.Item2)
  109. {
  110. if (currentState != liftState) return;
  111. masterBack = (false, false);
  112. TransitionToState(battleState);
  113. }
  114. }
  115. }
  116. }