//! Marks state as unlocked and triggers OnStateUnlocked. /* * \param state State to be unlocked. */ private void UnlockState(StoryGraphState state) { Debug.Log("Unlocking state: " + state.title); storyScript.OnStateUnlocked(state.title); state.unlocked = true; UpdateCurrentDialogue(state); }
//! Marks state as completed and triggers OnStateCompleted, raises error if requirements not met or invalid state name. /* * \param stateTitle State requested for completion. */ public void CompleteState(string stateTitle) { /* Marks the state with the given title as being completed. * Also calls the StoryScript's OnStateCompleted hook for the given state. * If any new states are unlocked by this completion, the OnStateUnlocked hook is called for each one. * Raises an Exception if the state with the given title was not found (probably due to typo). * Raises an Exception if the state's requirements were not met (StoryScripts should not try and do this). */ Debug.Log("CompleteState called for " + stateTitle); StoryGraphState state = GetStateWithTitle(stateTitle); // Check if this state's requirements are actually met foreach (var req in state.requirements) { if (!GetStateWithTitle(req).completed) { throw new StateRequirementsNotMet(stateTitle); } } Debug.Log("All requirements are met"); // If they are met, mark the state completed Debug.Log("Marking state as completed and calling storyScript's OnStateCompleted"); storyScript.OnStateCompleted(stateTitle); state.completed = true; UpdateCurrentDialogue(state); // Check if completing this state unlocks any new states Debug.Log("Checking for newly unlocked states"); foreach (var otherState in states) { //Debug.Log("Iterating: " + otherState.title); if (otherState.title == stateTitle || otherState.unlocked) { //Debug.Log("Skipping " + otherState.title); continue; } bool allReqsComplete = true; foreach (var req in otherState.requirements) { if (!GetStateWithTitle(req).completed) { allReqsComplete = false; break; } } if (allReqsComplete) { //Debug.Log("All reqs completed"); UnlockState(otherState); } else { //Debug.Log("All reqs not completed"); } } }
//! Adds a new state to the list of states. /*! * \param state State object to be added. */ protected void AddState(StoryGraphState state) { // Adds a new state to the list of states states.Add(state); if (state.requirements.Length == 0) { // Some states (such as the intro state) have no requirements. UnlockState(state); } }
//! Updates the dialogue for a state. /*! * \param state State that should be updated. */ private void UpdateCurrentDialogue(StoryGraphState state) { // Updates the dialogue for a state, either when it is freshly unlocked or freshly completed if (state.completed) { UpdateCurrentDialogueFromDict(state.dialogueOnCompleted); } else if (state.unlocked) { UpdateCurrentDialogueFromDict(state.dialogueOnUnlocked); } }