unity_art_puzzle_playable_luna/APPlayableLuna/Assets/Scripts/GameModel.cs

332 lines
9.8 KiB
C#

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