quantum-space-buddies/QSB/ConversationSync/Events/ConversationStartEndEvent.cs

71 lines
3.0 KiB
C#
Raw Normal View History

2020-10-24 15:31:20 +01:00
using OWML.Common;
using OWML.ModHelper.Events;
2020-11-03 21:42:14 +00:00
using QSB.EventsCore;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2020-10-24 15:31:20 +01:00
using QSB.Utility;
using QSB.WorldSync;
using System.Linq;
using UnityEngine;
2020-11-03 17:56:48 +00:00
namespace QSB.ConversationSync.Events
2020-10-24 15:31:20 +01:00
{
public class ConversationStartEndEvent : QSBEvent<ConversationStartEndMessage>
{
2020-11-03 22:13:12 +00:00
public override EventsCore.EventType Type => EventsCore.EventType.ConversationStartEnd;
2020-10-24 15:31:20 +01:00
public override void SetupListener() => GlobalMessenger<int, uint, bool>.AddListener(EventNames.QSBConversationStartEnd, Handler);
public override void CloseListener() => GlobalMessenger<int, uint, bool>.RemoveListener(EventNames.QSBConversationStartEnd, Handler);
private void Handler(int charId, uint playerId, bool state) => SendEvent(CreateMessage(charId, playerId, state));
private ConversationStartEndMessage CreateMessage(int charId, uint playerId, bool state) => new ConversationStartEndMessage
{
AboutId = LocalPlayerId,
CharacterId = charId,
PlayerId = playerId,
State = state
};
public override void OnReceiveRemote(ConversationStartEndMessage message)
{
if (message.CharacterId == -1)
{
2020-10-25 13:17:01 +00:00
DebugLog.ToConsole("Warning - Received conv. start/end event with char id -1.", MessageType.Warning);
2020-10-24 15:31:20 +01:00
return;
}
var dialogueTree = WorldRegistry.OldDialogueTrees[message.CharacterId];
2020-10-24 19:04:50 +01:00
var animController = Resources.FindObjectsOfTypeAll<CharacterAnimController>().FirstOrDefault(x => x.GetValue<CharacterDialogueTree>("_dialogueTree") == dialogueTree);
2020-10-25 15:19:23 +00:00
// Make character face player and talk
2020-10-24 19:04:50 +01:00
if (animController != default(CharacterAnimController))
2020-10-24 15:31:20 +01:00
{
2020-10-24 19:04:50 +01:00
if (message.State)
2020-10-24 15:31:20 +01:00
{
2020-10-25 15:19:23 +00:00
// Start talking
2020-11-03 21:18:40 +00:00
QSBPlayerManager.GetPlayer(message.PlayerId).CurrentDialogueID = message.CharacterId;
2020-10-24 19:04:50 +01:00
animController.SetValue("_inConversation", true);
animController.SetValue("_playerInHeadZone", true);
if (animController.GetValue<bool>("_hasTalkAnimation"))
{
animController.GetValue<Animator>("_animator").SetTrigger("Talking");
}
2020-10-25 13:17:01 +00:00
dialogueTree.GetComponent<InteractVolume>().DisableInteraction();
2020-10-24 15:31:20 +01:00
}
2020-10-24 19:04:50 +01:00
else
2020-10-24 15:31:20 +01:00
{
2020-10-25 15:19:23 +00:00
// Stop talking
2020-11-03 21:18:40 +00:00
QSBPlayerManager.GetPlayer(message.PlayerId).CurrentDialogueID = -1;
2020-10-24 19:04:50 +01:00
animController.SetValue("_inConversation", false);
animController.SetValue("_playerInHeadZone", false);
if (animController.GetValue<bool>("_hasTalkAnimation"))
{
animController.GetValue<Animator>("_animator").SetTrigger("Idle");
}
2020-10-25 13:17:01 +00:00
dialogueTree.GetComponent<InteractVolume>().EnableInteraction();
2020-10-24 15:31:20 +01:00
}
}
}
}
}