using System; using System.Collections.Generic; using Comp; using HttpApi; using JetBrains.Annotations; using Newtonsoft.Json; using Ragdoll; using UnityEngine; namespace Game { public class GameCore : RdSingletonStateMachine { public GameState pendingState; public GameState battleState; public GameState liftState; public GameState endState; [CanBeNull] public GamePlayer localPlayer; [CanBeNull] public GamePlayer antiPlayer; public int winPlayerId; public int diamond; public Dictionary cockDict; public Dictionary masterDict; [CanBeNull] public GameObject parent; [CanBeNull] public BattleMainComp battleMainComp; [CanBeNull] public GameObject camera; [CanBeNull] public GameObject resultPanel; [CanBeNull] public LinkedList battleDetailObjs; public (bool, bool) masterBack; // 主人复位 public string curBattleSession; public int liftTimes; public bool inBattleState = false; public GameCore() { } public void Exit() { currentState = null; } 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); this.liftTimes = 2; this.localPlayer = localPlayer; this.antiPlayer = antiPlayer; this.parent = parent; this.battleMainComp = parent.GetComponent(); this.camera = camera; this.resultPanel = resultPanel; localPlayer.cockId = 2; cockDict = new Dictionary(); masterDict = new Dictionary(); // var battleDetailList = JsonConvert.DeserializeObject(jsonLogs); var battleDetailList = jsonLogs; winPlayerId = battleDetailList.winPlayer; diamond = battleDetailList.diamond; Array.Sort(battleDetailList.logArray, (obj1, obj2) => obj1.index.CompareTo(obj2.index)); battleDetailObjs = new LinkedList(); foreach (var log in battleDetailList.logArray) { battleDetailObjs.AddFirst(log); } Start(); } public override void TransitionToState(GameState nextState) { inBattleState = nextState == battleState; base.TransitionToState(nextState); } protected override void Start() { TransitionToState(pendingState); currentState.PlayBattle(); } public ICockController GetController(GamePlayer gamePlayer) { return GetControllerByPlayerId(gamePlayer.playerId); } public ICockController GetControllerByPlayerId(int playerId) { cockDict.TryGetValue(playerId, out var cockActionComp); return cockActionComp != null ? cockActionComp.CockController : null; } public IMasterController GetMasterControllerByPlayerId(int playerId) { masterDict.TryGetValue(playerId, out var masterActionComp); return masterActionComp != null ? masterActionComp.MasterController : null; } public CockActionComp GetCockActionCompByPlayerId(int playerId) { cockDict.TryGetValue(playerId, out var cockActionComp); return cockActionComp != null ? cockActionComp : null; } public void ReportMasterBack(bool leftSide) { if (leftSide) { masterBack.Item1 = true; } else { masterBack.Item2 = true; } if (masterBack.Item1 && masterBack.Item2) { if (currentState == liftState) { masterBack = (false, false); TransitionToState(battleState); } } } } }