quantum-space-buddies/QSB/ConversationSync/ConversationManager.cs

132 lines
4.2 KiB
C#
Raw Normal View History

using OWML.Common;
2021-12-20 11:48:50 +00:00
using QSB.ConversationSync.WorldObjects;
2020-12-14 16:24:52 +00:00
using QSB.Events;
2020-11-03 21:33:48 +00:00
using QSB.Player;
using QSB.Utility;
2020-10-24 10:17:52 +00:00
using QSB.WorldSync;
using System.Collections.Generic;
2020-10-24 14:31:20 +00:00
using System.Linq;
2020-09-22 20:11:29 +00:00
using UnityEngine;
using UnityEngine.UI;
2020-09-22 20:11:29 +00:00
namespace QSB.ConversationSync
{
2021-12-20 11:48:50 +00:00
public class ConversationManager : WorldObjectManager
2020-12-14 21:20:53 +00:00
{
public override WorldObjectType WorldObjectType => WorldObjectType.Both;
2020-12-14 21:20:53 +00:00
public static ConversationManager Instance { get; private set; }
public Dictionary<CharacterDialogueTree, GameObject> BoxMappings { get; } = new Dictionary<CharacterDialogueTree, GameObject>();
private GameObject _boxPrefab;
public void Start()
{
Instance = this;
2021-11-25 11:27:32 +00:00
_boxPrefab = QSBCore.ConversationAssetBundle.LoadAsset<GameObject>("assets/Prefabs/dialoguebubble.prefab");
2021-10-26 10:59:41 +00:00
2021-10-26 13:08:44 +00:00
var font = (Font)Resources.Load(@"fonts\english - latin\HVD Fonts - BrandonGrotesque-Bold_Dynamic");
2020-12-14 21:20:53 +00:00
if (font == null)
{
DebugLog.ToConsole("Error - Font is null!", MessageType.Error);
}
2021-06-18 21:38:32 +00:00
2020-12-14 21:20:53 +00:00
_boxPrefab.GetComponent<Text>().font = font;
_boxPrefab.GetComponent<Text>().color = Color.white;
}
2021-12-20 11:48:50 +00:00
protected override void RebuildWorldObjects(OWScene scene)
=> QSBWorldSync.Init<QSBRemoteDialogueTrigger, RemoteDialogueTrigger>();
2020-12-14 21:20:53 +00:00
public uint GetPlayerTalkingToTree(CharacterDialogueTree tree)
{
var treeIndex = QSBWorldSync.OldDialogueTrees.IndexOf(tree);
2021-04-29 17:30:45 +00:00
return QSBPlayerManager.PlayerList.All(x => x.CurrentCharacterDialogueTreeId != treeIndex)
2020-12-14 21:20:53 +00:00
? uint.MaxValue
2021-04-29 17:30:45 +00:00
: QSBPlayerManager.PlayerList.First(x => x.CurrentCharacterDialogueTreeId == treeIndex).PlayerId;
2020-12-14 21:20:53 +00:00
}
public void SendPlayerOption(string text) =>
QSBEventManager.FireEvent(EventNames.QSBConversation, QSBPlayerManager.LocalPlayerId, text, ConversationType.Player);
2020-12-14 21:20:53 +00:00
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;
}
2021-06-18 21:38:32 +00:00
QSBEventManager.FireEvent(EventNames.QSBConversation, (uint)id, text, ConversationType.Character);
2020-12-14 21:20:53 +00:00
}
public void CloseBoxPlayer() =>
QSBEventManager.FireEvent(EventNames.QSBConversation, QSBPlayerManager.LocalPlayerId, "", ConversationType.ClosePlayer);
2020-12-14 21:20:53 +00:00
public void CloseBoxCharacter(int id) =>
QSBEventManager.FireEvent(EventNames.QSBConversation, (uint)id, "", ConversationType.CloseCharacter);
2020-12-14 21:20:53 +00:00
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;
}
2021-06-18 21:38:32 +00:00
QSBEventManager.FireEvent(EventNames.QSBConversationStartEnd, charId, QSBPlayerManager.LocalPlayerId, state);
2020-12-14 21:20:53 +00:00
}
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, 2, text);
2020-12-14 21:20:53 +00:00
}
public void DisplayCharacterConversationBox(int index, string text)
{
if (QSBWorldSync.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 = QSBWorldSync.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.SetParent(parent);
2020-12-14 21:20:53 +00:00
newBox.transform.localPosition = new Vector3(0, vertOffset, 0);
newBox.transform.rotation = parent.rotation;
newBox.GetComponent<Text>().text = text;
newBox.SetActive(true);
return newBox;
}
}
2020-12-03 08:28:05 +00:00
}