GameCore.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, battleState, liftState, endState;
  12. [RdReset] public GamePlayer localPlayer, antiPlayer;
  13. [RdReset] public int winPlayerId, diamond;
  14. // 存储组件的容器
  15. [RdReset] public Dictionary<int, CockActionComp> cockActionComps;
  16. [RdReset] public Dictionary<int, CockHpComp> cockHpComps;
  17. [RdReset] public Dictionary<int, MasterActionComp> masterDict;
  18. // 需要用到的GameObject
  19. [RdReset] public GameObject parent, camera, resultPanel;
  20. [RdReset] public BattleMainComp battleMainComp;
  21. // 游戏运行数据
  22. [RdReset] public LinkedList<BattleDetailObj> battleDetailObjs;
  23. [RdReset] public (bool, bool) masterBack; // 主人复位
  24. [RdReset] public string curBattleSession;
  25. [RdReset(2)] public int liftTimes;
  26. [RdReset(true)] public bool showResultPanel = true;
  27. public GameCore()
  28. {
  29. }
  30. public void Exit()
  31. {
  32. currentState = null;
  33. RdResetTool.ResetAttribute(this);
  34. }
  35. public void Init(GamePlayer localPlayer, GamePlayer antiPlayer, GameObject parent, GameObject camera,
  36. BattleDetailListObj jsonLogs, GameObject resultPanel)
  37. {
  38. if (currentState != null) // 防止多重初始化
  39. return;
  40. pendingState = new GamePendingState(this);
  41. battleState = new GameBattleState(this);
  42. liftState = new GameLiftState(this);
  43. endState = new GameEndState(this);
  44. // 举J次数限制
  45. liftTimes = 2;
  46. this.localPlayer = localPlayer;
  47. this.antiPlayer = antiPlayer;
  48. this.parent = parent;
  49. this.battleMainComp = parent.GetComponent<BattleMainComp>();
  50. this.camera = camera;
  51. this.resultPanel = resultPanel;
  52. cockActionComps = new Dictionary<int, CockActionComp>();
  53. cockHpComps = new Dictionary<int, CockHpComp>();
  54. masterDict = new Dictionary<int, MasterActionComp>();
  55. winPlayerId = jsonLogs.winPlayer;
  56. diamond = jsonLogs.diamond;
  57. Array.Sort(jsonLogs.logArray, (obj1, obj2) => obj1.index.CompareTo(obj2.index));
  58. battleDetailObjs = new LinkedList<BattleDetailObj>();
  59. foreach (var log in jsonLogs.logArray)
  60. {
  61. battleDetailObjs.AddFirst(log);
  62. }
  63. Start();
  64. }
  65. protected override void Start()
  66. {
  67. TransitionToState(pendingState);
  68. currentState.PlayBattle();
  69. }
  70. public ICockController GetController(GamePlayer gamePlayer)
  71. {
  72. return GetControllerByPlayerId(gamePlayer.playerId);
  73. }
  74. private ICockController GetControllerByPlayerId(int playerId)
  75. {
  76. cockActionComps.TryGetValue(playerId, out var cockActionComp);
  77. return cockActionComp != null ? cockActionComp.CockController : null;
  78. }
  79. public CockActionComp GetCockActionCompByPlayerId(int playerId)
  80. {
  81. cockActionComps.TryGetValue(playerId, out var cockActionComp);
  82. return cockActionComp != null ? cockActionComp : null;
  83. }
  84. public CockHpComp GetCockHpCompByPlayerId(int playerId)
  85. {
  86. cockHpComps.TryGetValue(playerId, out var cockHpComp);
  87. return cockHpComp != null ? cockHpComp : null;
  88. }
  89. public void ReportMasterBack(bool leftSide)
  90. {
  91. if (leftSide)
  92. {
  93. masterBack.Item1 = true;
  94. }
  95. else
  96. {
  97. masterBack.Item2 = true;
  98. }
  99. Debug.Log("report master back " + masterBack.Item1 + " " + masterBack.Item2);
  100. if (masterBack.Item1 && masterBack.Item2)
  101. {
  102. if (currentState != liftState) return;
  103. TransitionToState(battleState);
  104. }
  105. }
  106. }
  107. }