using System; using System.Collections.Generic; using Comp; using HttpApi; using Ragdoll; using UnityEngine; namespace Game { public class GameCore : RdSingletonStateMachine { [RdReset] public GameState pendingState, battleState, liftState, endState; [RdReset] public GamePlayer localPlayer, antiPlayer; [RdReset] public int winPlayerId, diamond; // 存储组件的容器 [RdReset] public Dictionary cockActionComps; [RdReset] public Dictionary cockHpComps; [RdReset] public Dictionary masterDict; // 需要用到的GameObject [RdReset] public GameObject parent, camera, resultPanel; [RdReset] public BattleMainComp battleMainComp; // 游戏运行数据 [RdReset] public LinkedList battleDetailObjs; [RdReset] public (bool, bool) masterBack; // 主人复位 [RdReset] public string curBattleSession; [RdReset(2)] public int liftTimes; [RdReset(true)] public bool showResultPanel = true; public GameCore() { } public void Exit() { currentState = null; RdResetTool.ResetAttribute(this); } public void Init(GamePlayer localPlayer, GamePlayer antiPlayer, GameObject parent, GameObject camera, BattleDetailListObj jsonLogs, GameObject resultPanel) { if (currentState != null) // 防止多重初始化 return; pendingState = new GamePendingState(this); battleState = new GameBattleState(this); liftState = new GameLiftState(this); endState = new GameEndState(this); // 举J次数限制 liftTimes = 2; this.localPlayer = localPlayer; this.antiPlayer = antiPlayer; this.parent = parent; this.battleMainComp = parent.GetComponent(); this.camera = camera; this.resultPanel = resultPanel; cockActionComps = new Dictionary(); cockHpComps = new Dictionary(); masterDict = new Dictionary(); winPlayerId = jsonLogs.winPlayer; diamond = jsonLogs.diamond; Array.Sort(jsonLogs.logArray, (obj1, obj2) => obj1.index.CompareTo(obj2.index)); battleDetailObjs = new LinkedList(); foreach (var log in jsonLogs.logArray) { battleDetailObjs.AddFirst(log); } Start(); } protected override void Start() { TransitionToState(pendingState); currentState.PlayBattle(); } public ICockController GetController(GamePlayer gamePlayer) { return GetControllerByPlayerId(gamePlayer.playerId); } private ICockController GetControllerByPlayerId(int playerId) { cockActionComps.TryGetValue(playerId, out var cockActionComp); return cockActionComp != null ? cockActionComp.CockController : null; } public CockActionComp GetCockActionCompByPlayerId(int playerId) { cockActionComps.TryGetValue(playerId, out var cockActionComp); return cockActionComp != null ? cockActionComp : null; } public CockHpComp GetCockHpCompByPlayerId(int playerId) { cockHpComps.TryGetValue(playerId, out var cockHpComp); return cockHpComp != null ? cockHpComp : null; } public void ReportMasterBack(bool leftSide) { if (leftSide) { masterBack.Item1 = true; } else { masterBack.Item2 = true; } if (masterBack.Item1 && masterBack.Item2) { if (currentState != liftState) return; masterBack = (false, false); TransitionToState(battleState); } } } }