/// <summary> /// Parse and execute a conversation string. /// </summary> public virtual IEnumerator DoConversation(string conv) { if (string.IsNullOrEmpty(conv)) { yield break; } var conversationItems = Parse(conv); if (conversationItems.Count == 0) { yield break; } // Track the current and previous parameter values Character currentCharacter = null; Sprite currentPortrait = null; RectTransform currentPosition = null; Character previousCharacter = null; // Play the conversation for (int i = 0; i < conversationItems.Count; ++i) { ConversationItem item = conversationItems[i]; if (item.Character != null) { currentCharacter = item.Character; } currentPortrait = item.Portrait; currentPosition = item.Position; var sayDialog = GetSayDialog(currentCharacter); if (sayDialog == null) { // Should never happen yield break; } sayDialog.SetActive(true); if (currentCharacter != null && currentCharacter != previousCharacter) { sayDialog.SetCharacter(currentCharacter); } //Handle stage changes var stage = Stage.GetActiveStage(); if (currentCharacter != null && !currentCharacter.State.onScreen && currentPortrait == null) { // No call to show portrait of hidden character // so keep hidden item.Hide = true; } if (stage != null && currentCharacter != null && (currentPortrait != currentCharacter.State.portrait || currentPosition != currentCharacter.State.position)) { var portraitOptions = new PortraitOptions(true); portraitOptions.display = item.Hide ? DisplayType.Hide : DisplayType.Show; portraitOptions.character = currentCharacter; portraitOptions.fromPosition = currentCharacter.State.position; portraitOptions.toPosition = currentPosition; portraitOptions.portrait = currentPortrait; //Flip option - Flip the opposite direction the character is currently facing if (item.Flip) { portraitOptions.facing = item.FacingDirection; } // Do a move tween if the character is already on screen and not yet at the specified position if (currentCharacter.State.onScreen && currentPosition != currentCharacter.State.position) { portraitOptions.move = true; } if (item.Hide) { stage.Hide(portraitOptions); } else { stage.Show(portraitOptions); } } if (stage == null && currentPortrait != null) { sayDialog.SetCharacterImage(currentPortrait); } previousCharacter = currentCharacter; if (!string.IsNullOrEmpty(item.Text)) { exitSayWait = false; sayDialog.Say(item.Text, true, true, true, true, false, null, () => { exitSayWait = true; }); while (!exitSayWait) { yield return(null); } exitSayWait = false; } } }
/// <summary> /// Using the string of say parameters before the ':', /// set the current character, position and portrait if provided. /// </summary> /// <returns>The conversation item.</returns> /// <param name="sayParams">The list of say parameters.</param> /// <param name="text">The text for the character to say.</param> /// <param name="currentCharacter">The currently speaking character.</param> protected virtual ConversationItem CreateConversationItem(string[] sayParams, string text, Character currentCharacter) { var item = new ConversationItem(); // Populate the story text to be written item.Text = text; if (sayParams == null || sayParams.Length == 0) { // Text only, no params - early out. return(item); } // try to find the character param first, since we need to get its portrait int characterIndex = -1; if (characters == null) { PopulateCharacterCache(); } for (int i = 0; item.Character == null && i < sayParams.Length; i++) { for (int j = 0; j < characters.Length; j++) { if (characters[j].NameStartsWith(sayParams[i])) { characterIndex = i; item.Character = characters[j]; break; } } } // Assume last used character if none is specified now if (item.Character == null) { item.Character = currentCharacter; } // Check if there's a Hide parameter int hideIndex = -1; if (item.Character != null) { for (int i = 0; i < sayParams.Length; i++) { if (i != characterIndex && string.Compare(sayParams[i], "hide", true) == 0) { hideIndex = i; item.Hide = true; break; } } } int flipIndex = -1; if (item.Character != null) { for (int i = 0; i < sayParams.Length; i++) { if (i != characterIndex && i != hideIndex && (string.Compare(sayParams[i], ">>>", true) == 0 || string.Compare(sayParams[i], "<<<", true) == 0)) { if (string.Compare(sayParams[i], ">>>", true) == 0) { item.FacingDirection = FacingDirection.Right; } if (string.Compare(sayParams[i], "<<<", true) == 0) { item.FacingDirection = FacingDirection.Left; } flipIndex = i; item.Flip = true; break; } } } // Next see if we can find a portrait for this character int portraitIndex = -1; if (item.Character != null) { for (int i = 0; i < sayParams.Length; i++) { if (item.Portrait == null && item.Character != null && i != characterIndex && i != hideIndex && i != flipIndex) { Sprite s = item.Character.GetPortrait(sayParams[i]); if (s != null) { portraitIndex = i; item.Portrait = s; break; } } } } // Next check if there's a position parameter Stage stage = Stage.GetActiveStage(); if (stage != null) { for (int i = 0; i < sayParams.Length; i++) { if (i != characterIndex && i != portraitIndex && i != hideIndex) { RectTransform r = stage.GetPosition(sayParams[i]); if (r != null) { item.Position = r; break; } } } } return(item); }