using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace GuruClient { public class GameModel { //当前关卡数据 private ALevel _aLevel; //关卡的打开方式 StartMode _startMode; //所有拼图数据 private Dictionary _patchDB = new Dictionary(); //可操作拼图数据 private List _operablePatch = new List(); //所有stage对应的拼图 Dictionary> _stageAllPatch = new Dictionary>(); Dictionary> _autoAllPatch = new Dictionary>(); Dictionary> _lineAllPatch = new Dictionary>(); APatch _finishPatch = new APatch(); //已完成拼图数量 private int _completePatchCount; //当前关卡 private int _currStage = 0; //当前关卡下的第N步 private int _currStep = 0; //总步数 private int _totalStep = 0; /// /// 初始化关卡 /// /// 关卡ID /// 是否为重玩 public bool InitializeLevel(string psdID, StartMode startMode, LevelType levelType) { _aLevel = ConfigManager.Instance.GetLevel(psdID, levelType); if(_aLevel == null ) { return false; } _startMode = startMode; return true; } /// /// 加载所有拼图元素 /// /// 关卡对应的CustonAssetBundle public bool LoadAllPatches() { TextAsset ta = Resources.Load("Levels/395/PatchDB"); if (ta != null) { _patchDB = ConfigManager.Instance.ParsePatches(ta.text); ParsePatches(); //筛选出可操作的拼图 FilterOperablePatch(); return true; } return false; } /// /// 解析关卡的拼图 /// public void ParsePatches() { _stageAllPatch.Clear(); _autoAllPatch.Clear(); _lineAllPatch.Clear(); _finishPatch = new APatch(); Dictionary patchDB = GetPatchDB(); //筛选当前关卡 foreach (KeyValuePair item in patchDB) { switch(item.Value.tag) { case "patch": if(_stageAllPatch.TryGetValue(item.Value.stage.ToString(), out List li)) { li.Add(item.Value); } else { li = new List { item.Value }; _stageAllPatch[item.Value.stage.ToString()] = li; } break; case "auto": if(_autoAllPatch.TryGetValue(item.Value.stage.ToString(), out List li1)) { li1.Add(item.Value); } else { li1 = new List { item.Value }; _autoAllPatch[item.Value.stage.ToString()] = li1; } break; case "line": if(_lineAllPatch.TryGetValue(item.Value.stage.ToString(), out List li2)) { li2.Add(item.Value); } else { li2 = new List { item.Value }; _lineAllPatch[item.Value.stage.ToString()] = li2; } break; case "finish": _finishPatch = item.Value; break; } } } /// /// 过去关卡的拼图字典 /// /// public Dictionary GetPatchDB() { return _patchDB; } /// /// 过滤可操作的拼图列表 /// private void FilterOperablePatch() { _operablePatch.Clear(); //主线第一关,不随机 if (_aLevel.level == 1 && _aLevel.type == LevelType.MainLine) { foreach (KeyValuePair item in _patchDB) { if (item.Key.Contains("patch_")) { _operablePatch.Add(item.Value); } } }else { UnityEngine.Random.InitState(_aLevel.level); foreach (KeyValuePair item in _patchDB) { if (item.Key.Contains("patch_")) { int idx = UnityEngine.Random.Range(0, _operablePatch.Count + 1); _operablePatch.Insert(idx, item.Value); } } } } /// /// 获取当前的阶段 /// /// public int GetCurrentStage() { return _currStage; } /// /// 获取当前阶段的拼图进度 /// /// public int GetCurrentStep() { return _currStep; } /// /// 获取关卡数据 /// /// public ALevel GetCurrentLevel() { return _aLevel; } /// /// 设置当前的阶段 /// /// 阶段 public void SetCurrentStage(int stage) { _currStage = stage; EventManager.Dispatch("UPDATE_PROGRESS_DOT"); } /// /// 设置当前阶段的拼图进度 /// /// public void SetCurrentStep(int step) { _currStep = step; } /// /// 获取指定阶段的所有拼图 /// /// /// public List GetStageAllPatch(int stage) { return _stageAllPatch.TryGetValue(stage.ToString(), out List li) ? li : null; } /// /// 获取指定阶段的所有描线 /// /// /// public List GetLineAllPatch(int stage) { return _lineAllPatch.TryGetValue(stage.ToString(), out List li) ? li : null; } /// /// 获取指定阶段的所有自动拼图 /// /// /// public List GetAutoAllPatch(int stage) { return _autoAllPatch.TryGetValue(stage.ToString(), out List li) ? li : null; } /// /// 获取完成拼图 /// /// public APatch GetFinishPatch() { return _finishPatch; } public bool IsHideLineWhenPatched() { return true; } public bool IsShowCurrentPatche() { return false; } /// /// 叠加总拼图数 /// public int AddTotalStep() { _totalStep += 1; return _totalStep; } /// /// 叠加已完成的拼图数 /// /// public void AddCompletePatchCount(int count) { _completePatchCount += count; EventManager.Dispatch("UPDATE_PROGRESS"); } public bool IsCompleteByPatch() { return _operablePatch.Count == _totalStep && _totalStep != 0; } /// /// 获取可操作的拼图字典 /// /// public List GetOperablePatch() { return _operablePatch; } /// /// 获取拼图总数 /// /// public int GetTotalPatchCount() { return _operablePatch.Count; } /// /// 获取已完成的拼图数 /// /// public int GetCompletePatchCount() { return _completePatchCount; } public Tuple GetStageProgress() { return new Tuple(_currStage == 0 ? 1: _currStage, _stageAllPatch.Count); } } }