using System; using System.Collections.Generic; using UnityEngine; using System.Linq; namespace GuruClient { public class GameManager : MonoBehaviour { private static GameManager _instance; public static GameManager Instance { get { return _instance; } } [HideInInspector] [LunaPlaygroundField("Amount of lives1", 1, "Game Settings")] public int maxLives1 = 3; [HideInInspector] [LunaPlaygroundField("Amount of lives2", 1, "Game Settings")] public int maxLives2 = 3; [HideInInspector] [LunaPlaygroundField("Amount of lives3", 1, "Game Settings")] public int maxLives3 = 3; public Camera UICamera; public GameObjectPool PatchPool { set; get; } private bool _poolInited = false; private GameModel _model; public UIGameView uiGameView; public GameObject uiGameWin; public GameObject patchTemplate; //本关需要填充的补丁 private List _stageAllPatch = new List(); private List _stageAllLine = new List(); private List _stageCompletePatch = new List(); private List _needAutoPatch = new List(); private List _finishAutoPatch = new List(); //继续游戏时,需要恢复的拼图数据 private List _recoverPatch = new List(); [HideInInspector] public bool _canDragging = false; //是否正在跨越关卡 private bool _isStageCross = false; //是否已完成 private bool _isGameWin = false; [HideInInspector] public StartMode curStartMode; //当前局的bPlay次数 private int _curBPlay; //操作人员 public string Operator {get; set;} //从LevelStart时间开始记录,截止到LevelEnd事件的关卡时长 private int _levelDuration; //从LevelStart时间开始记录,截止到LevelEnd事件的拼图数量 private int _levelDurationStep; public bool isOrientationPortrait = true; // Start is called before the first frame update void Start() { _instance = this; if(Screen.width > Screen.height) { Debug.Log("这是横屏"); isOrientationPortrait = true; } else { Debug.Log("这是竖屏"); isOrientationPortrait = false; } GameManager.Instance.InitPatchPool(); _model = new GameModel(); bool isSuccess = _model.InitializeLevel("395", StartMode.Continue, LevelType.MainLine); if (isSuccess) { Debug.Log("关卡初始化成功"); _model.LoadAllPatches(); Debug.Log("关卡拼图加载成功"); uiGameView.StartGame(); } } public void InitPatchPool() { if (_poolInited) { return; } PatchPool = GameObjectPool.CreatePool(patchTemplate, 50, false); _poolInited = true; } public GameModel GetModel() { return _model; } /// /// 开始关卡的Stage /// public void StartStage() { //根据当前数据恢复关卡 if (_model.GetCurrentStage() > 1 || _model.GetCurrentStep() > 0 || _stageCompletePatch.Count > 0) { ToNextLevel(true); }else { ToNextLevel(false); } } /// /// 前往下一关 /// private void ToNextLevel() { ToNextLevel(false); } /// /// 前往下一关 /// /// 是否从进度文件恢复 public void ToNextLevel(bool isRecover) { if (!isRecover) { _model.SetCurrentStage(_model.GetCurrentStage() + 1); _model.SetCurrentStep(0); _stageCompletePatch.Clear(); }else { uiGameView.LoadAutoPatches(_recoverPatch, false, true); } _stageAllPatch.Clear(); _stageAllLine.Clear(); _needAutoPatch.Clear(); _finishAutoPatch.Clear(); int curStage = _model.GetCurrentStage(); List tmp = _model.GetStageAllPatch(curStage); if (tmp != null) { _stageAllPatch = tmp; } tmp = _model.GetLineAllPatch(curStage); if (tmp != null) { _stageAllLine = tmp; } tmp = _model.GetAutoAllPatch(curStage); if (tmp != null) { _needAutoPatch = tmp; } _finishAutoPatch.Add(_model.GetFinishPatch()); if (_stageAllPatch.Count > 0) { //加载关卡 LoadLevel(isRecover, null); }else { CompletePage(null); } _isStageCross = false; } /// /// 关卡完成 /// public void CompletePage(Action callback) { //自动补图 if (uiGameView != null) { try { uiGameView.LoadRole(true, false, callback); Timer.Instance.SetTimeoutAsync(2.0f, () => { GameWin(); }); } catch (Exception e) { Debug.Log(e); } } } public void GameWin() { if (!_isGameWin) { //显示GameWin _isGameWin = true; uiGameWin.SetActive(true); uiGameWin.GetComponent().OnBeforStart(_model.GetCurrentLevel()); } } /// /// 加载关卡 /// /// public void LoadLevel(bool isRecover, Action callback) { _canDragging = false; if (uiGameView != null) { if (!isRecover) { Timer.Instance.SetTimeoutAsync(0.5f, callback); } if (uiGameView != null) { if (_model.GetCurrentStage() == 1 || isRecover) { uiGameView.LoadBg(callback); } LoadLevelInner(isRecover, callback); } } } private void LoadLevelInner(bool isRecover, Action callback) { uiGameView.LoadRole(isRecover, false, callback); LoadLineTemplate(isRecover,null); } /// /// 加载线稿 /// /// private void LoadLineTemplate(bool isRecover, Action callback) { if(uiGameView != null) { uiGameView.LoadLineTemplate(callback); InitPatches(isRecover); } } /// /// 是否可拖拽(拼图) /// /// public bool CanDragging() { return _canDragging; } /// /// 初始化拼图 /// /// 是否为恢复关卡 private void InitPatches(bool isRecover) { bool isShowCurrentPatche = _model.IsShowCurrentPatche(); if(_model.GetCurrentStage() == 1 || isRecover || isShowCurrentPatche) { if(uiGameView != null) { uiGameView.InitPatches(isShowCurrentPatche ? _model.GetCurrentStage() : -1, () => { //第一阶段和恢复的保证加载完成后,才可拼图 _canDragging = true; uiGameView.ShowTips(true); }); } }else { //第二阶段,不需要加载拼图,直接可拼图 _canDragging = true; } if(isRecover) { CheckNextLevel(); } EventManager.Dispatch("START_NEXT_LEVEL"); } /// /// 匹配后增加score分数 /// /// 拼图数据 /// public int AddScore(APatch aPatch) { LevelDurationStepUpdate(); _model.SetCurrentStep(_model.GetCurrentStep() + 1); int step = _model.AddTotalStep(); _model.AddCompletePatchCount(1); Luna.Unity.Analytics.LogEvent($"CompleteStep_{step}", 0); _stageCompletePatch.Add(aPatch); return _stageCompletePatch.Count; } public bool IsCompleteStage() { return _stageAllPatch.Count == _stageCompletePatch.Count && _stageAllPatch.Count != 0; } //关卡步数+1步 public void LevelDurationStepUpdate() { _levelDurationStep ++; } //获取一个未完成拼图 public APatch GetOneUnCompletePatch() { IEnumerable collection = _stageAllPatch.Except(_stageCompletePatch); foreach (APatch item in collection) { return item; } return new APatch(); } /// /// 检测某个拼图数据是否已完成 /// /// /// public bool IsCompletedPatch(APatch aPatch) { return _stageCompletePatch.Contains(aPatch); } /// /// 游戏是否已结束 /// /// public bool IsGameWin() { return _isGameWin; } /// /// 获取当前阶段需要拼接的数据 /// /// public List GetCurrentStageData() { return _stageAllPatch; } /// /// 获取当前阶段的线稿数据 /// /// public List GetCurrentStageLineData() { return _stageAllLine; } /// /// 检测是否应该进入下一关 /// /// public void CheckNextLevel(int step = -1) { if (!_isStageCross && (step == -1 ? _model.GetCurrentStep() : step) >= _stageAllPatch.Count) { _isStageCross = true; if (uiGameView != null) { uiGameView.RemoveLineTemplate(null); } if (_needAutoPatch.Count > 0) { //自动补图 if (uiGameView != null) { if(uiGameView != null) { int curStage = _model.GetCurrentStage(); List tmp = _model.GetStageAllPatch(curStage + 1); if(tmp == null) { uiGameView.LoadAutoPatches(_needAutoPatch, false, false, () => { ToNextLevel(); }); }else { uiGameView.LoadAutoPatches(_needAutoPatch); ToNextLevel(); } } } }else { ToNextLevel(); } } } void OnApplicationQuit() { Luna.Unity.LifeCycle.GameEnded(); } } }