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

106 lines
4.4 KiB
C#
Raw Normal View History

2020-10-25 13:17:01 +00:00
using OWML.Common;
2020-12-14 21:20:53 +00:00
using QSB.Patches;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2020-10-25 13:17:01 +00:00
using QSB.Utility;
using QSB.WorldSync;
2020-09-18 14:32:22 +00:00
using System.Collections.Generic;
2020-10-24 14:31:20 +00:00
using UnityEngine;
2020-09-18 14:32:22 +00:00
namespace QSB.ConversationSync
{
2020-12-02 21:23:01 +00:00
public class ConversationPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
2020-11-03 21:11:10 +00:00
2020-12-02 21:23:01 +00:00
public static void StartConversation(CharacterDialogueTree __instance)
{
2020-12-11 13:14:58 +00:00
var index = QSBWorldSync.OldDialogueTrees.FindIndex(x => x == __instance);
2020-12-02 21:23:01 +00:00
if (index == -1)
{
DebugLog.ToConsole($"Warning - Index for tree {__instance.name} was -1.", MessageType.Warning);
}
QSBPlayerManager.LocalPlayer.CurrentDialogueID = index;
ConversationManager.Instance.SendConvState(index, true);
}
2020-12-02 21:23:01 +00:00
public static bool EndConversation(CharacterDialogueTree __instance)
{
if (!__instance.enabled)
{
return false;
}
if (QSBPlayerManager.LocalPlayer.CurrentDialogueID == -1)
{
DebugLog.ToConsole($"Warning - Ending conversation with CurrentDialogueId of -1! Called from {__instance.name}", MessageType.Warning);
2020-12-19 18:52:36 +00:00
return true;
2020-12-02 21:23:01 +00:00
}
ConversationManager.Instance.SendConvState(QSBPlayerManager.LocalPlayer.CurrentDialogueID, false);
ConversationManager.Instance.CloseBoxCharacter(QSBPlayerManager.LocalPlayer.CurrentDialogueID);
QSBPlayerManager.LocalPlayer.CurrentDialogueID = -1;
ConversationManager.Instance.CloseBoxPlayer();
return true;
}
2020-09-18 14:32:22 +00:00
2020-12-02 21:23:01 +00:00
public static bool InputDialogueOption(int optionIndex, DialogueBoxVer2 ____currentDialogueBox)
{
if (optionIndex < 0)
{
// in a page where there is no selectable options
ConversationManager.Instance.CloseBoxPlayer();
return true;
}
2020-09-29 18:42:45 +00:00
2020-12-02 21:23:01 +00:00
var selectedOption = ____currentDialogueBox.OptionFromUIIndex(optionIndex);
ConversationManager.Instance.SendPlayerOption(selectedOption.Text);
return true;
}
2020-09-22 20:11:29 +00:00
2020-12-02 21:23:01 +00:00
public static void GetNextPage(string ____name, List<string> ____listPagesToDisplay, int ____currentPage)
{
var key = ____name + ____listPagesToDisplay[____currentPage];
// Sending key so translation can be done on client side - should make different language-d clients compatible
2020-12-14 16:24:52 +00:00
QSBCore.Helper.Events.Unity.RunWhen(() => QSBPlayerManager.LocalPlayer.CurrentDialogueID != -1,
2020-12-02 21:23:01 +00:00
() => ConversationManager.Instance.SendCharacterDialogue(QSBPlayerManager.LocalPlayer.CurrentDialogueID, key));
}
2020-09-22 20:11:29 +00:00
2020-12-02 21:23:01 +00:00
public static bool OnAnimatorIK(float ___headTrackingWeight,
bool ___lookOnlyWhenTalking,
bool ____playerInHeadZone,
bool ____inConversation,
ref float ____currentLookWeight,
ref Vector3 ____currentLookTarget,
DampedSpring3D ___lookSpring,
Animator ____animator,
CharacterDialogueTree ____dialogueTree)
{
var playerId = ConversationManager.Instance.GetPlayerTalkingToTree(____dialogueTree);
2020-12-14 21:20:53 +00:00
var position = playerId == uint.MaxValue
? Locator.GetActiveCamera().transform.position
2020-12-22 09:13:08 +00:00
: QSBPlayerManager.GetPlayer(playerId).CameraBody.transform.position;
2020-12-14 21:20:53 +00:00
var b = ___headTrackingWeight * Mathf.Min(1, !___lookOnlyWhenTalking
? !____playerInHeadZone ? 0 : 1
: !____inConversation || !____playerInHeadZone ? 0 : 1);
2020-12-02 21:23:01 +00:00
____currentLookWeight = Mathf.Lerp(____currentLookWeight, b, Time.deltaTime * 2f);
____currentLookTarget = ___lookSpring.Update(____currentLookTarget, position, Time.deltaTime);
____animator.SetLookAtPosition(____currentLookTarget);
____animator.SetLookAtWeight(____currentLookWeight);
return false;
}
2020-10-24 14:31:20 +00:00
2020-12-02 21:23:01 +00:00
public static bool OnZoneExit(CharacterDialogueTree ____dialogueTree)
{
var playerId = ConversationManager.Instance.GetPlayerTalkingToTree(____dialogueTree);
2020-12-14 20:41:56 +00:00
return playerId == uint.MaxValue;
2020-12-14 21:20:53 +00:00
}
2020-10-24 14:31:20 +00:00
2020-12-02 21:23:01 +00:00
public override void DoPatches()
{
2020-12-14 16:24:52 +00:00
QSBCore.Helper.HarmonyHelper.AddPostfix<DialogueNode>("GetNextPage", typeof(ConversationPatches), nameof(GetNextPage));
QSBCore.Helper.HarmonyHelper.AddPrefix<CharacterDialogueTree>("InputDialogueOption", typeof(ConversationPatches), nameof(InputDialogueOption));
QSBCore.Helper.HarmonyHelper.AddPostfix<CharacterDialogueTree>("StartConversation", typeof(ConversationPatches), nameof(StartConversation));
QSBCore.Helper.HarmonyHelper.AddPrefix<CharacterDialogueTree>("EndConversation", typeof(ConversationPatches), nameof(EndConversation));
QSBCore.Helper.HarmonyHelper.AddPrefix<CharacterAnimController>("OnAnimatorIK", typeof(ConversationPatches), nameof(OnAnimatorIK));
QSBCore.Helper.HarmonyHelper.AddPrefix<CharacterAnimController>("OnZoneExit", typeof(ConversationPatches), nameof(OnZoneExit));
2020-12-02 21:23:01 +00:00
}
}
2020-12-03 08:28:05 +00:00
}