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

113 lines
3.6 KiB
C#
Raw Normal View History

2022-01-29 04:49:07 +00:00
using Cysharp.Threading.Tasks;
using OWML.Common;
2021-12-26 00:29:00 +00:00
using QSB.ConversationSync.Messages;
2021-12-20 11:48:50 +00:00
using QSB.ConversationSync.WorldObjects;
2021-12-26 00:29:00 +00:00
using QSB.Messaging;
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;
2022-01-29 04:49:07 +00:00
using System.Threading;
2020-09-22 20:11:29 +00:00
using UnityEngine;
using UnityEngine.UI;
2020-09-22 20:11:29 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.ConversationSync;
public class ConversationManager : WorldObjectManager
2020-09-22 20:11:29 +00:00
{
2022-03-03 03:46:33 +00:00
public override WorldObjectScene WorldObjectScene => WorldObjectScene.Both;
2020-12-14 21:20:53 +00:00
2022-03-03 03:46:33 +00:00
public static ConversationManager Instance { get; private set; }
2022-05-13 21:38:06 +00:00
public Dictionary<CharacterDialogueTree, GameObject> BoxMappings { get; } = new();
2022-03-03 03:46:33 +00:00
private GameObject _boxPrefab;
public void Start()
{
Instance = this;
2022-03-03 03:46:33 +00:00
_boxPrefab = QSBCore.ConversationAssetBundle.LoadAsset<GameObject>("assets/Prefabs/dialoguebubble.prefab");
var font = (Font)Resources.Load(@"fonts\english - latin\HVD Fonts - BrandonGrotesque-Bold_Dynamic");
if (font == null)
2020-12-14 21:20:53 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole("Error - Font is null!", MessageType.Error);
}
2020-12-14 21:20:53 +00:00
2022-03-03 03:46:33 +00:00
_boxPrefab.GetComponent<Text>().font = font;
_boxPrefab.GetComponent<Text>().color = Color.white;
}
2021-10-26 10:59:41 +00:00
2022-03-03 03:46:33 +00:00
public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct)
{
2022-05-13 21:38:06 +00:00
QSBWorldSync.Init<QSBRemoteDialogueTrigger, RemoteDialogueTrigger>();
QSBWorldSync.Init<QSBCharacterDialogueTree, CharacterDialogueTree>();
2022-03-03 03:46:33 +00:00
}
2020-12-14 21:20:53 +00:00
public uint GetPlayerTalkingToTree(CharacterDialogueTree tree) =>
QSBPlayerManager.PlayerList.FirstOrDefault(x => x.CurrentCharacterDialogueTree?.AttachedObject == tree)
?.PlayerId ?? uint.MaxValue;
2021-12-20 11:48:50 +00:00
2022-05-13 21:38:06 +00:00
public void SendPlayerOption(string text)
=> new ConversationMessage(ConversationType.Player, (int)QSBPlayerManager.LocalPlayerId, text).Send();
2020-12-14 21:20:53 +00:00
2022-05-13 21:38:06 +00:00
public void SendCharacterDialogue(int id, string text)
=> new ConversationMessage(ConversationType.Character, id, text).Send();
2020-12-14 21:20:53 +00:00
2022-03-03 03:46:33 +00:00
public void CloseBoxPlayer() =>
new ConversationMessage(ConversationType.ClosePlayer, (int)QSBPlayerManager.LocalPlayerId).Send();
2022-03-03 03:46:33 +00:00
public void CloseBoxCharacter(int id) =>
new ConversationMessage(ConversationType.CloseCharacter, id).Send();
2020-12-14 21:20:53 +00:00
public void SendConvState(QSBCharacterDialogueTree tree, bool state)
=> tree.SendMessage(new ConversationStartEndMessage(QSBPlayerManager.LocalPlayerId, state));
2022-03-03 03:46:33 +00:00
public void DisplayPlayerConversationBox(uint playerId, string text)
{
if (playerId == QSBPlayerManager.LocalPlayerId)
2020-12-14 21:20:53 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole("Error - Cannot display conversation box for local player!", MessageType.Error);
return;
}
2022-03-03 03:46:33 +00:00
var player = QSBPlayerManager.GetPlayer(playerId);
// Destroy old box if it exists
var playerBox = player.CurrentDialogueBox;
if (playerBox != null)
2020-12-14 21:20:53 +00:00
{
2022-03-03 03:46:33 +00:00
Destroy(playerBox);
2020-12-14 21:20:53 +00:00
}
2022-03-03 03:46:33 +00:00
QSBPlayerManager.GetPlayer(playerId).CurrentDialogueBox = CreateBox(player.Body.transform, 2, text);
}
public void DisplayCharacterConversationBox(int index, string text)
{
// Remove old box if it exists
2022-05-13 21:38:06 +00:00
var oldDialogueTree = index.GetWorldObject<QSBCharacterDialogueTree>().AttachedObject;
2022-03-03 03:46:33 +00:00
if (BoxMappings.ContainsKey(oldDialogueTree))
2020-12-14 21:20:53 +00:00
{
2022-03-03 03:46:33 +00:00
Destroy(BoxMappings[oldDialogueTree]);
BoxMappings.Remove(oldDialogueTree);
2020-12-14 21:20:53 +00:00
}
2022-03-03 03:46:33 +00:00
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);
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-14 21:20:53 +00:00
}
}