// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : // // Last Modified By : Kimch // Last Modified On : // *********************************************************************** // // // *********************************************************************** namespace G { using System.Collections.Generic; public sealed class TutorialGroup { public enum Status { kNone = 0, kStart = 1,//已经开始 kFinish = 2, } #region Field private readonly ItemTutorialGroup _item; private List _stages; private List _conditions; #endregion #region Property /// /// /// public int id => _item.id; /// /// /// public int priority => _item.priority; /// /// /// public int[] conditionIds => _item.conditions; /// /// /// public string text => _item.text; /// /// /// public int status { get; private set; } /// /// /// public bool completed { get { return status == (int)Status.kFinish; } set { status = (int)Status.kFinish; } } /// /// /// public TutorialStage currStage { get; private set; } /// /// /// public TutorialStage nextStage { get { return currStage != null ? GetNextStage(currStage.step) : null; } } /// /// /// public List stages { get { if (_stages == null) { _stages = new List(); TutorialProxy.Instance.GetAllStages(id, _stages); _stages.Sort((a, b) => a.step.CompareTo(b.step)); } return _stages; } } /// /// /// public List conditions { get { if (_conditions == null) { var conditionIds = this.conditionIds; if (conditionIds != null) { _conditions = new List(conditionIds.Length); foreach (var conditionId in conditionIds) { var condition = TutorialProxy.Instance.GetCondition(conditionId); _conditions.Add(condition); } } else { _conditions = new List(0); } } return _conditions; } } /// /// 无效 /// public bool invalid { get { if (_item.invalid > 0) { var mission = MissionProxy.Instance.GetMission(_item.invalid); if (mission != null && mission.isCompleted) return true; } return false; } } #endregion public TutorialGroup(ItemTutorialGroup item) { _item = item; } #region Method /// /// /// /// public void SetCompletedStageId(int stageId) { if (stageId > 0) { var tmpStages = this.stages; if (tmpStages != null && tmpStages.Count > 0) { for (int i = 0; i < tmpStages.Count; i++) { tmpStages[i].completed = true; if (tmpStages[i].id == stageId) break; } if (tmpStages[tmpStages.Count - 1].completed) { completed = true; } } } } /// /// /// /// public int GetCompletedStageId() { var tmpStages = this.stages; if (tmpStages != null && tmpStages.Count > 0) { if (!tmpStages[0].completed) return 0; // int i = tmpStages.Count - 1; if (tmpStages[i].completed) { return tmpStages[i].id; } // for (i = i - 1; i > 0; i--) { if (tmpStages[i].completed) return tmpStages[i].id; } return tmpStages[0].id; } return 0; } /// /// 检查 /// /// /// public bool CheckCondition(int type, int target) { if (this.invalid) return false; if (conditions != null) foreach (var condition in conditions) { if (!((condition.type == 10 || condition.type == 9 || condition.type == type) && condition.GetResult(target))) return false; } return true; } /// /// 触发 /// /// public bool Trigger() { var activeS = GetValidStage(); if (activeS != null) { activeS.Start(); currStage = activeS; return true; } return false; } /// /// 完成当前步 /// public void CompleteStep() { if (currStage != null) { currStage.Finish(); if (!HasNext()) { currStage = null; Complete(); } } } /// /// /// /// public bool HasNext() { return currStage != null ? currStage.step < stages.Count : false; } /// /// 强制完成 /// public void Complete() { this.status = (int)Status.kFinish; } /// /// /// /// /// public TutorialStage GetStage(int step) { if (step > 0 && step <= stages.Count) { return stages[step - 1]; } return null; } /// /// /// /// /// public TutorialStage GetNextStage(int step) { if (step > 0 && step < stages.Count) { return stages[step]; } return null; } /// /// /// /// public TutorialStage GetValidStage() { int validIndex = 0; var tmpStages = stages; for (int i = tmpStages.Count - 1; i >= 0; i--) { var tmpStage = tmpStages[i]; if (tmpStage.isKey && tmpStage.completed) { validIndex = i + 1; break; } } if (validIndex < tmpStages.Count) { return tmpStages[validIndex]; } else { completed = true; return null; } } /// /// /// /// public bool HasValidStage() { if (!completed) { foreach (var item in stages) { if (item.isKey && !item.started) { return true; } } } return false; } /// /// /// internal void OnUpdate() { if (currStage != null) { if (currStage.completed) { currStage = nextStage; if (currStage != null) { currStage.Start(); } else { currStage = null; Complete(); } } else if (currStage.started) { currStage.OnUpdate(); } } } #endregion } }