using OWML.Common; using OWML.ModHelper.Events; using QSB.Events; using QSB.Player; using QSB.Utility; using QSB.WorldSync; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; namespace QSB.ConversationSync { public class ConversationManager : MonoBehaviour { public static ConversationManager Instance { get; private set; } public AssetBundle ConversationAssetBundle { get; private set; } private GameObject BoxPrefab; public Dictionary BoxMappings = new Dictionary(); private void Start() { Instance = this; ConversationAssetBundle = QSB.Helper.Assets.LoadBundle("assets/conversation"); DebugLog.LogState("ConversationBundle", ConversationAssetBundle); BoxPrefab = ConversationAssetBundle.LoadAsset("assets/dialoguebubble.prefab"); var font = (Font)Resources.Load(@"fonts\english - latin\spacemono-bold"); if (font == null) { DebugLog.ToConsole("Error - Font is null!", MessageType.Error); } BoxPrefab.GetComponent().font = font; BoxPrefab.GetComponent().color = Color.white; DebugLog.LogState("BoxPrefab", BoxPrefab); } public uint GetPlayerTalkingToTree(CharacterDialogueTree tree) { var treeIndex = WorldRegistry.OldDialogueTrees.IndexOf(tree); if (!QSBPlayerManager.PlayerList.Any(x => x.CurrentDialogueID == treeIndex)) { // No player talking to tree return uint.MaxValue; } // .First() should be fine here as only one player should be talking to a character. return QSBPlayerManager.PlayerList.First(x => x.CurrentDialogueID == treeIndex).PlayerId; } public void SendPlayerOption(string text) { GlobalMessenger .FireEvent(EventNames.QSBConversation, QSBPlayerManager.LocalPlayerId, text, ConversationType.Player); } public void SendCharacterDialogue(int id, string text) { if (id == -1) { DebugLog.ToConsole("Warning - Tried to send conv. event with char id -1.", MessageType.Warning); return; } GlobalMessenger .FireEvent(EventNames.QSBConversation, (uint)id, text, ConversationType.Character); } public void CloseBoxPlayer() { GlobalMessenger .FireEvent(EventNames.QSBConversation, QSBPlayerManager.LocalPlayerId, "", ConversationType.ClosePlayer); } public void CloseBoxCharacter(int id) { GlobalMessenger .FireEvent(EventNames.QSBConversation, (uint)id, "", ConversationType.CloseCharacter); } public void SendConvState(int charId, bool state) { if (charId == -1) { DebugLog.ToConsole("Warning - Tried to send conv. start/end event with char id -1.", MessageType.Warning); return; } GlobalMessenger .FireEvent(EventNames.QSBConversationStartEnd, charId, QSBPlayerManager.LocalPlayerId, state); } public void DisplayPlayerConversationBox(uint playerId, string text) { if (playerId == QSBPlayerManager.LocalPlayerId) { DebugLog.ToConsole("Error - Cannot display conversation box for local player!", MessageType.Error); return; } var player = QSBPlayerManager.GetPlayer(playerId); // Destroy old box if it exists var playerBox = player.CurrentDialogueBox; if (playerBox != null) { Destroy(playerBox); } QSBPlayerManager.GetPlayer(playerId).CurrentDialogueBox = CreateBox(player.Body.transform, 25, text); } public void DisplayCharacterConversationBox(int index, string text) { if (WorldRegistry.OldDialogueTrees.ElementAtOrDefault(index) == null) { DebugLog.ToConsole($"Error - Tried to display character conversation box for id {index}! (Doesn't exist!)", MessageType.Error); return; } // Remove old box if it exists var oldDialogueTree = WorldRegistry.OldDialogueTrees[index]; if (BoxMappings.ContainsKey(oldDialogueTree)) { Destroy(BoxMappings[oldDialogueTree]); BoxMappings.Remove(oldDialogueTree); } BoxMappings.Add(oldDialogueTree, CreateBox(oldDialogueTree.gameObject.transform, 2, text)); } private GameObject CreateBox(Transform parent, float vertOffset, string text) { var newBox = Instantiate(BoxPrefab); newBox.SetActive(false); newBox.transform.parent = parent; newBox.transform.localPosition = new Vector3(0, vertOffset, 0); newBox.transform.rotation = parent.rotation; var lookAt = newBox.AddComponent(); lookAt.SetValue("_useLookAt", false); lookAt.SetValue("_localFacingVector", Vector3.back); lookAt.SetValue("_localRotationAxis", Vector3.up); newBox.GetComponent().text = text; newBox.SetActive(true); return newBox; } } }