// Retrieve a sprite from a registered theme public Sprite RetrieveSprite(string themeName, string spriteName) { if (string.IsNullOrEmpty(themeName)) { DialogueLogger.Log("Trying to retrieve sprite from repo, but the themeName parameter is empty"); return(null); } if (!_themeSprites.ContainsKey(themeName)) { DialogueLogger.LogError($"Trying to retrieve sprite {themeName} for the theme {themeName}, but it is not registered in the repo."); return(null); } // Seems to be consistently faster than Linq return(_themeSprites[themeName].ThemeSprites.Find(s => s.Name == spriteName)?.Sprite); }
// Return the sprite for the character if found public Sprite RetrieveSprite(string character, string name = "Default") { if (string.IsNullOrEmpty(character)) { DialogueLogger.Log("Trying to retrieve sprite from repo, but the character parameter is empty"); return(null); } if (!_characterSprites.ContainsKey(character)) { DialogueLogger.LogError($"trying to retrieve sprite {name} for the character {character}, but it is not registered in the repo."); return(null); } // Seems to be consistently faster than Linq return(_characterSprites[character].CharacterSprites.Find(s => s.Name == name)?.Sprite); }
// Validate then execute private static void performAction(DialogueAction action) { if (!validateAction(action)) { return; } switch (action.ActionType) { case DialogueAction.Types.LOG: DialogueLogger.Log(action.Message); break; case DialogueAction.Types.LOG_WARNING: DialogueLogger.LogWarning(action.Message); break; case DialogueAction.Types.LOG_ERROR: DialogueLogger.LogError(action.Message); break; case DialogueAction.Types.CLOSE_CONVERSATION: DialogueController.Instance?.StopCurrentConversation(); break; case DialogueAction.Types.SEND_MESSAGE: var targetObject = GameObject.Find(action.Target); if (targetObject == null) { DialogueLogger.LogError($"Trying to execute a send message action, but GameObject {action.Target} was not found. Skipping action"); return; } targetObject.SendMessage(action.Message, SendMessageOptions.DontRequireReceiver); break; case DialogueAction.Types.CHANGE_THEME: DialogueController.Instance?.ChangeTheme(action.Message); break; case DialogueAction.Types.CLOSE_BG_CONVERSATIONS: BackgroundDialogueController.Instance?.CloseConversations(); break; case DialogueAction.Types.START_BG_CONVERSATION: BackgroundDialogueController.Instance?.StartConversation(action.Message); break; default: DialogueLogger.LogError($"Action with the name {action.Name} has na unrecognised action type {action.ActionType}. The action type loaded from the conversation JSON is {action.Type}. Skipping action"); break; } }
// Find in the repo and start public void StartConversation(string convoName) { var tempConvo = findConversation(convoName); if (tempConvo == null) { return; } // Check if it's a normal conversation and redirect if (tempConvo.ConversationType == Conversation.Types.DEFAULT) { DialogueLogger.Log($"Trying to start conversation {convoName} as a background conversation, redirecting to the default DialogueController"); DialogueController.Instance?.StartConversation(tempConvo); return; } StartConversation(tempConvo); }
// Find and start the conversation public void StartConversation(string convoName) { var tempConvo = findConversation(convoName); if (tempConvo == null) { return; } // Check if it's a background conversation if (tempConvo.ConversationType == Conversation.Types.BACKGROUND) { DialogueLogger.Log($"Trying to start background conversation {convoName} as a default conversation, redirecting to BackgroundConversationController."); BackgroundDialogueController.Instance?.StartConversation(tempConvo); return; } StartConversation(tempConvo); }
// If you're UI supports tags, use this to execute all tags at a given position in the sentence // I don't like this here. But unfortunately, the default and background conversations are too different (in my example implementation) to put it in a parent class. // Please note, not all tags are used here protected IEnumerator processTagsForPosition(TextModifications textMod, int index) { var mods = textMod.GetAnyTextModsForPosition(index); // Check for custom modifications foreach (var mod in mods) { // Commands if (mod.ModType == TextModifications.Modifications.CLOSE_BG_CONVERSATIONS) { BackgroundDialogueController.Instance?.CloseConversations(); } // Simple modifications e.g. <command=value> if (mod.ModType == TextModifications.Modifications.SPEED) { _speedMultiplyer = (mod as SimpleModification).GetValue <float>(); } else if (mod.ModType == TextModifications.Modifications.REMOVE_VARAIBLE) { VariableRepo.Instance?.Remove((mod as SimpleModification).GetValue <string>()); } else if (mod.ModType == TextModifications.Modifications.WAIT) { yield return(new WaitForSeconds((mod as SimpleModification).GetValue <float>())); } else if (mod.ModType == TextModifications.Modifications.ACTION) { performAction((mod as SimpleModification).GetValue <string>()); } else if (mod.ModType == TextModifications.Modifications.LOG) { DialogueLogger.Log((mod as SimpleModification).GetValue <string>()); } else if (mod.ModType == TextModifications.Modifications.LOG_WARNING) { DialogueLogger.LogWarning((mod as SimpleModification).GetValue <string>()); } else if (mod.ModType == TextModifications.Modifications.LOG_ERROR) { DialogueLogger.LogError((mod as SimpleModification).GetValue <string>()); } else if (mod.ModType == TextModifications.Modifications.BG_CONVERSATION) { BackgroundDialogueController.Instance?.StartConversation((mod as SimpleModification).GetValue <string>()); } // Complex modifications e.g. <command=value>content</command> else if (mod.ModType == TextModifications.Modifications.SEND_MESSAGE) { var revievingObject = GameObject.Find((mod as SimpleModification).GetValue <string>()); if (revievingObject == null) { DialogueLogger.LogError($"Trying to execute a send message command, but GameObject {(mod as SimpleModification).GetValue<string>()} was not found"); continue; } revievingObject.SendMessage((mod as ComplexModification).GetContent <string>(), SendMessageOptions.DontRequireReceiver); } else if (mod.ModType == TextModifications.Modifications.ACTION_WITH_MESSAGE) { performActionWithMessage((mod as SimpleModification).GetValue <string>(), (mod as ComplexModification).GetContent <string>()); } else if (mod.ModType == TextModifications.Modifications.ACTION_WITH_TARGET) { performActionWithTarget((mod as SimpleModification).GetValue <string>(), (mod as ComplexModification).GetContent <string>()); } } }