// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : // // Last Modified By : Kimch // Last Modified On : // *********************************************************************** // // // *********************************************************************** namespace G { using F; using System.Collections.Generic; using UnityEngine; /// /// 新手引导管理类 /// public sealed partial class TutorialProxy : GameProxy { #region Field /// /// 关闭教程 /// public bool closeTutorial; private Dictionary _groups; private Dictionary _stages; private Dictionary _actions; private Dictionary _conditions; /// /// 当前 /// private TutorialGroup _currGroup; /// /// 活跃的 /// private readonly List _activeGroups = new List(); /// /// 已经触发的 /// private readonly List _triggeredGroups = new List(); #endregion #region Property /// /// 当前引导 /// public TutorialGroup currTutorial { get { return _currGroup; } } #endregion #region Method /// /// 触发条件(驱动) /// /// public void TriggerCondition(int condition, int value = 0) { if (closeTutorial) { return; } if (_currGroup != null) { _currGroup.currStage.CheckCondition(condition, value); return; } foreach (var group in _activeGroups) { if (!group.completed && group.CheckCondition(condition, value)) { _triggeredGroups.Add(group); } } } /// /// 强制完成 /// public void CompleteStage(bool autoNext = false) { if (_currGroup != null) { _currGroup.CompleteStep(); if (_currGroup.completed) { _activeGroups.Remove(_currGroup); OnGroupCompleted(); _currGroup = null; } } } /// /// 获取指定组 /// /// /// internal TutorialGroup GetGroup(int id) { _groups.TryGetValue(id, out TutorialGroup result); return result; } /// /// 获取指定步 /// /// /// internal TutorialStage GetStage(int id) { _stages.TryGetValue(id, out TutorialStage result); return result; } /// /// 获取指定所有步 /// /// /// internal void GetAllStages(int group, List stages) { foreach (var stage in _stages.Values) { if (stage.group == group) { stages.Add(stage); } } } /// /// /// /// /// internal TutorialAction GetAction(int id) { _actions.TryGetValue(id, out TutorialAction result); return result; } /// /// /// /// /// internal TutorialCondition GetCondition(int id) { _conditions.TryGetValue(id, out TutorialCondition result); return result; } private void LoadInternal() { // var conditionItems = ItemProxy.Instance.GetStaticItems(); _conditions = new Dictionary(conditionItems.Count); for (int i = 0; i < conditionItems.Count; i++) { var condition = new TutorialCondition(conditionItems[i]); _conditions.Add(condition.id, condition); } // var actionItems = ItemProxy.Instance.GetStaticItems(); _actions = new Dictionary(actionItems.Count); for (int i = 0; i < actionItems.Count; i++) { var action = new TutorialAction(actionItems[i]); _actions.Add(action.id, action); } // var stageItems = ItemProxy.Instance.GetStaticItems(); _stages = new Dictionary(stageItems.Count); for (int i = 0; i < stageItems.Count; i++) { var stage = new TutorialStage(stageItems[i]); _stages.Add(stage.id, stage); } // var groupItems = ItemProxy.Instance.GetStaticItems(); _groups = new Dictionary(groupItems.Count); for (int i = 0; i < groupItems.Count; i++) { var group = new TutorialGroup(groupItems[i]); _groups.Add(group.id, group); } } private void OnGroupTrigger() { #if UNITY_EDITOR Debug.Log("引导触发 " + _currGroup.id + _currGroup.text); #endif KStatistics.Instance.ReportGuide(_currGroup.id, _currGroup.text); } private void OnGroupCompleted() { #if UNITY_EDITOR Debug.Log("引导结束 " + _currGroup.id + _currGroup.text); #endif } public void SaveStage(TutorialStage stage) { //string key = "t_s_" + stage.id; //PlayerPrefs.SetInt(key, 1); //PlayerPrefs.Save(); var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V1); if (dataList != null) { dataList.Clear(); foreach (var kv in _groups) { var item = kv.Value; var stageId = kv.Value.GetCompletedStageId(); if (stageId > 0) { dataList.Add(kv.Key); dataList.Add(stageId); } } ArchiveProxy.Instance.SetIntList(ARCHIVE_KEY_V1, dataList); } } const string ARCHIVE_KEY_V1 = "tutorial_v1"; public void ProcessData(object data) { var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V1); if (dataList != null && dataList.Count > 0) { for (int i = 0; i < dataList.Count; i += 2) { int groupId = dataList[i]; int stageId = dataList[i + 1]; var group = GetGroup(groupId); if (group != null) { group.SetCompletedStageId(stageId); } } } else { foreach (var kv in _stages) { var key = "t_s_" + kv.Key; bool complete = PlayerPrefs.HasKey(key); if (complete) { kv.Value.completed = complete; } PlayerPrefs.DeleteKey(key); } SaveStage(null); } _activeGroups.Clear(); foreach (var kv in _groups) { var item = kv.Value; if (item.HasValidStage()) { _activeGroups.Add(item); } } _activeGroups.Sort((a, b) => a.priority.CompareTo(b.priority)); } #endregion #region Unity public static TutorialProxy Instance; private void Awake() { Instance = this; gameObject.AddComponent(); } public override void LoadCompleted() { } public override void ReadArchive() { LoadInternal(); //本地存 ProcessData(null); } private void LateUpdate() { if (_currGroup == null && _triggeredGroups.Count > 0) { _currGroup = _triggeredGroups[0]; _triggeredGroups.RemoveAt(0); _currGroup.Trigger(); OnGroupTrigger(); } if (_currGroup != null) { _currGroup.OnUpdate(); if (_currGroup.completed) { OnGroupCompleted(); _currGroup = null; } } } #endregion } }