GameCore.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using Comp;
  4. using HttpApi;
  5. using JetBrains.Annotations;
  6. using Ragdoll;
  7. using UnityEngine;
  8. namespace Game
  9. {
  10. public class GameCore : RdSingletonStateMachine<GameState, GameCore>
  11. {
  12. public GameState pendingState;
  13. public GameState battleState;
  14. public GameState liftState;
  15. public GameState endState;
  16. [RdReset(null)] public GamePlayer localPlayer;
  17. [RdReset(null)] public GamePlayer antiPlayer;
  18. [RdReset("0")] public int winPlayerId;
  19. public int diamond;
  20. public Dictionary<int, CockActionComp> cockDict;
  21. public Dictionary<int, MasterActionComp> masterDict;
  22. [CanBeNull] public GameObject parent;
  23. [CanBeNull] public BattleMainComp battleMainComp;
  24. [CanBeNull] public GameObject camera;
  25. [CanBeNull] public GameObject resultPanel;
  26. [CanBeNull] public LinkedList<BattleDetailObj> battleDetailObjs;
  27. public (bool, bool) masterBack; // 主人复位
  28. public string curBattleSession;
  29. public int liftTimes;
  30. 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. this.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. localPlayer.cockId = 2;
  56. cockDict = new Dictionary<int, CockActionComp>();
  57. masterDict = new Dictionary<int, MasterActionComp>();
  58. // var battleDetailList = JsonConvert.DeserializeObject<BattleDetailListObj>(jsonLogs);
  59. var battleDetailList = jsonLogs;
  60. winPlayerId = battleDetailList.winPlayer;
  61. diamond = battleDetailList.diamond;
  62. Array.Sort(battleDetailList.logArray, (obj1, obj2) => obj1.index.CompareTo(obj2.index));
  63. battleDetailObjs = new LinkedList<BattleDetailObj>();
  64. foreach (var log in battleDetailList.logArray)
  65. {
  66. battleDetailObjs.AddFirst(log);
  67. }
  68. Start();
  69. }
  70. public override void TransitionToState(GameState nextState)
  71. {
  72. inBattleState = nextState == battleState;
  73. base.TransitionToState(nextState);
  74. }
  75. protected override void Start()
  76. {
  77. TransitionToState(pendingState);
  78. currentState.PlayBattle();
  79. }
  80. public ICockController GetController(GamePlayer gamePlayer)
  81. {
  82. return GetControllerByPlayerId(gamePlayer.playerId);
  83. }
  84. public ICockController GetControllerByPlayerId(int playerId)
  85. {
  86. cockDict.TryGetValue(playerId, out var cockActionComp);
  87. return cockActionComp != null ? cockActionComp.CockController : null;
  88. }
  89. public IMasterController GetMasterControllerByPlayerId(int playerId)
  90. {
  91. masterDict.TryGetValue(playerId, out var masterActionComp);
  92. return masterActionComp != null ? masterActionComp.MasterController : null;
  93. }
  94. public CockActionComp GetCockActionCompByPlayerId(int playerId)
  95. {
  96. cockDict.TryGetValue(playerId, out var cockActionComp);
  97. return cockActionComp != null ? cockActionComp : null;
  98. }
  99. public void ReportMasterBack(bool leftSide)
  100. {
  101. if (leftSide)
  102. {
  103. masterBack.Item1 = true;
  104. }
  105. else
  106. {
  107. masterBack.Item2 = true;
  108. }
  109. if (masterBack.Item1 && masterBack.Item2)
  110. {
  111. if (currentState == liftState)
  112. {
  113. masterBack = (false, false);
  114. TransitionToState(battleState);
  115. }
  116. }
  117. }
  118. }
  119. }