GameCore.cs 4.3 KB

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