/*//initialize the story in runtime * private void OnEnable() * { #if UNITY_EDITOR * if (!EditorApplication.isPlayingOrWillChangePlaymode) return; #endif * SceneManager.sceneLoaded += SceneLoaded; * } * * private void SceneLoaded(Scene scene, LoadSceneMode mode) * { * SceneManager.sceneLoaded -= SceneLoaded; * if(initializeEmpty) SetState(CalculateInitialState()); * }*/ public StoryState CalculateInitialState() { var stateDictionary = new Dictionary <int, QuestState>(); ForEach(rootQuest, (quest) => { var open = (quest._parent == null || quest._parent._state != QuestState.Blocked) && quest._requirements.Length == 0; stateDictionary.Add(quest._id, open?QuestState.Available : QuestState.Blocked); }); var state = new StoryState() { Ids = stateDictionary.Keys.ToArray(), States = stateDictionary.Values.ToArray() }; return(state); }
public void SetState(StoryState state) { var stateDictionary = new Dictionary <int, QuestState>(); for (var i = 0; i < state.Ids.Length; i++) { stateDictionary.Add(state.Ids[i], state.States[i]); } ForEach(rootQuest, (quest) => { quest._state = stateDictionary[quest._id]; }); ForEach(rootQuest, (quest) => { var requiredLeft = (uint)quest._requirements.Length; foreach (var requirement in quest._requirements) { if (requirement._state == QuestState.Completed) { requiredLeft--; } } quest._requiredLeft = requiredLeft; if (!(quest is GroupQuest groupQuest)) { return; } var childrenLeft = (uint)groupQuest.children.Length; foreach (var child in groupQuest.children) { if (child._state == QuestState.Completed) { childrenLeft--; } } groupQuest._childrenLeft = childrenLeft; });