2021-10-15 20:06:51 +00:00
using HarmonyLib ;
using OWML.Common ;
2021-12-26 07:03:23 +00:00
using QSB.Animation.NPC.Messages ;
2021-04-29 17:30:45 +00:00
using QSB.Animation.NPC.WorldObjects ;
using QSB.ConversationSync ;
2021-12-23 21:49:04 +00:00
using QSB.Messaging ;
2021-04-26 13:30:21 +00:00
using QSB.Patches ;
using QSB.Player ;
2022-01-13 04:58:38 +00:00
using QSB.TriggerSync.WorldObjects ;
2021-04-26 13:30:21 +00:00
using QSB.Utility ;
using QSB.WorldSync ;
2021-04-29 17:30:45 +00:00
using System.Linq ;
2021-04-26 13:30:21 +00:00
using UnityEngine ;
2021-04-29 17:30:45 +00:00
namespace QSB.Animation.NPC.Patches
2021-04-26 13:30:21 +00:00
{
2021-10-15 20:06:51 +00:00
[HarmonyPatch]
2021-04-26 13:30:21 +00:00
public class CharacterAnimationPatches : QSBPatch
{
public override QSBPatchTypes Type = > QSBPatchTypes . OnClientConnect ;
2021-10-15 20:06:51 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterAnimController), nameof(CharacterAnimController.OnAnimatorIK))]
public static bool AnimatorIKReplacement (
2021-12-27 05:25:26 +00:00
CharacterAnimController __instance )
2021-04-26 13:30:21 +00:00
{
2021-12-04 09:51:27 +00:00
if ( ! WorldObjectManager . AllObjectsReady | | ConversationManager . Instance = = null )
2021-05-02 07:54:00 +00:00
{
2022-01-13 12:13:47 +00:00
return true ;
2021-05-02 07:54:00 +00:00
}
2021-04-26 13:30:21 +00:00
2021-12-27 05:25:26 +00:00
var playerId = ConversationManager . Instance . GetPlayerTalkingToTree ( __instance . _dialogueTree ) ;
2021-04-29 19:59:25 +00:00
var player = QSBPlayerManager . GetPlayer ( playerId ) ;
2022-01-13 12:13:47 +00:00
if ( __instance . playerTrackingZone = = null )
{
return true ;
}
2022-01-13 04:58:38 +00:00
var qsbObj = __instance . playerTrackingZone . GetWorldObject < QSBCharacterTrigger > ( ) ; // OPTIMIZE : maybe cache this somewhere... or assess how slow this is
2021-04-26 13:30:21 +00:00
2021-06-10 22:21:19 +00:00
PlayerInfo playerToUse = null ;
2021-12-27 05:25:26 +00:00
if ( __instance . _inConversation )
2021-04-26 13:30:21 +00:00
{
if ( playerId = = uint . MaxValue )
{
2021-06-23 20:46:52 +00:00
DebugLog . ToConsole ( $"Error - {__instance.name} is in conversation with a null player! Defaulting to active camera." , MessageType . Error ) ;
2021-04-29 19:59:25 +00:00
playerToUse = QSBPlayerManager . LocalPlayer ;
2021-04-26 13:30:21 +00:00
}
else
{
2021-04-29 19:59:25 +00:00
playerToUse = player . CameraBody = = null
? QSBPlayerManager . LocalPlayer
: player ;
2021-04-26 13:30:21 +00:00
}
}
2022-01-13 04:58:38 +00:00
else if ( ! __instance . lookOnlyWhenTalking & & qsbObj . Occupants . Count ! = 0 ) // IDEA : maybe this would be more fun if characters looked between players at random times? :P
2021-04-26 13:30:21 +00:00
{
2022-01-13 04:58:38 +00:00
playerToUse = QSBPlayerManager . GetClosestPlayerToWorldPoint ( qsbObj . Occupants , __instance . transform . position ) ;
2021-04-26 13:30:21 +00:00
}
2021-06-10 22:21:19 +00:00
else if ( QSBPlayerManager . PlayerList . Count ! = 0 )
2021-04-26 13:30:21 +00:00
{
2021-04-29 19:59:25 +00:00
playerToUse = QSBPlayerManager . GetClosestPlayerToWorldPoint ( __instance . transform . position , true ) ;
2021-04-26 13:30:21 +00:00
}
2021-06-19 10:26:05 +00:00
var localPosition = playerToUse ! = null
2021-12-27 05:25:26 +00:00
? __instance . _animator . transform . InverseTransformPoint ( playerToUse . CameraBody . transform . position )
2021-06-10 22:21:19 +00:00
: Vector3 . zero ;
2021-04-26 13:30:21 +00:00
2021-12-27 05:25:26 +00:00
var targetWeight = __instance . headTrackingWeight ;
if ( __instance . lookOnlyWhenTalking )
2021-04-26 13:30:21 +00:00
{
2021-12-27 05:25:26 +00:00
if ( ! __instance . _inConversation
2022-01-13 04:58:38 +00:00
| | qsbObj . Occupants . Count = = 0
| | ! qsbObj . Occupants . Contains ( playerToUse ) )
2021-04-26 13:30:21 +00:00
{
targetWeight * = 0 ;
}
}
else
{
2022-01-13 04:58:38 +00:00
if ( qsbObj . Occupants . Count = = 0
| | ! qsbObj . Occupants . Contains ( playerToUse ) )
2021-04-26 13:30:21 +00:00
{
targetWeight * = 0 ;
}
}
2021-12-27 05:25:26 +00:00
__instance . _currentLookWeight = Mathf . Lerp ( __instance . _currentLookWeight , targetWeight , Time . deltaTime * 2f ) ;
__instance . _currentLookTarget = __instance . lookSpring . Update ( __instance . _currentLookTarget , localPosition , Time . deltaTime ) ;
__instance . _animator . SetLookAtPosition ( __instance . _animator . transform . TransformPoint ( __instance . _currentLookTarget ) ) ;
__instance . _animator . SetLookAtWeight ( __instance . _currentLookWeight ) ;
2021-04-26 13:30:21 +00:00
return false ;
}
2021-10-15 20:06:51 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(FacePlayerWhenTalking), nameof(FacePlayerWhenTalking.OnStartConversation))]
2021-10-28 13:29:53 +00:00
public static bool OnStartConversation ( FacePlayerWhenTalking __instance )
2021-04-29 17:30:45 +00:00
{
2021-10-28 13:29:53 +00:00
var playerId = ConversationManager . Instance . GetPlayerTalkingToTree ( __instance . _dialogueTree ) ;
2021-04-29 17:30:45 +00:00
if ( playerId = = uint . MaxValue )
{
2021-10-28 13:29:53 +00:00
DebugLog . ToConsole ( $"Error - No player talking to {__instance._dialogueTree.name}!" , MessageType . Error ) ;
2021-04-29 17:30:45 +00:00
return false ;
}
2021-06-18 21:38:32 +00:00
2021-04-29 17:30:45 +00:00
var player = QSBPlayerManager . GetPlayer ( playerId ) ;
var distance = player . Body . transform . position - __instance . transform . position ;
var vector2 = distance - Vector3 . Project ( distance , __instance . transform . up ) ;
var angle = Vector3 . Angle ( __instance . transform . forward , vector2 ) * Mathf . Sign ( Vector3 . Dot ( vector2 , __instance . transform . right ) ) ;
var axis = __instance . transform . parent . InverseTransformDirection ( __instance . transform . up ) ;
var lhs = Quaternion . AngleAxis ( angle , axis ) ;
2021-10-28 13:29:53 +00:00
__instance . FaceLocalRotation ( lhs * __instance . transform . localRotation ) ;
2021-04-29 17:30:45 +00:00
return false ;
}
2021-10-15 20:06:51 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.StartConversation))]
public static bool StartConversation ( CharacterDialogueTree __instance )
2021-04-29 17:30:45 +00:00
{
var allNpcAnimControllers = QSBWorldSync . GetWorldObjects < INpcAnimController > ( ) ;
var ownerOfThis = allNpcAnimControllers . FirstOrDefault ( x = > x . GetDialogueTree ( ) = = __instance ) ;
if ( ownerOfThis = = default )
{
return true ;
}
2021-06-18 21:38:32 +00:00
2021-12-26 07:08:11 +00:00
ownerOfThis . SendMessage ( new NpcAnimationMessage ( true ) ) ;
2021-04-29 17:30:45 +00:00
return true ;
}
2021-10-15 20:06:51 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.EndConversation))]
public static bool EndConversation ( CharacterDialogueTree __instance )
2021-04-29 17:30:45 +00:00
{
var allNpcAnimControllers = QSBWorldSync . GetWorldObjects < INpcAnimController > ( ) ;
var ownerOfThis = allNpcAnimControllers . FirstOrDefault ( x = > x . GetDialogueTree ( ) = = __instance ) ;
if ( ownerOfThis = = default )
{
return true ;
}
2021-06-18 21:38:32 +00:00
2021-12-26 07:08:11 +00:00
ownerOfThis . SendMessage ( new NpcAnimationMessage ( false ) ) ;
2021-04-29 17:30:45 +00:00
return true ;
}
2021-04-29 19:59:25 +00:00
2021-10-15 20:06:51 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(KidRockController), nameof(KidRockController.Update))]
2021-10-28 13:29:53 +00:00
public static bool UpdateReplacement ( KidRockController __instance )
2021-04-29 19:59:25 +00:00
{
2021-12-04 09:51:27 +00:00
if ( ! WorldObjectManager . AllObjectsReady )
2021-05-02 07:54:00 +00:00
{
return true ;
}
2021-06-18 21:38:32 +00:00
2021-10-28 13:29:53 +00:00
var qsbObj = QSBWorldSync . GetWorldObjects < QSBCharacterAnimController > ( ) . First ( x = > x . GetDialogueTree ( ) = = __instance . _dialogueTree ) ;
2021-04-29 19:59:25 +00:00
2021-10-28 13:29:53 +00:00
if ( ! __instance . _throwingRock & & ! qsbObj . InConversation ( ) & & Time . time > __instance . _nextThrowTime )
2021-04-29 19:59:25 +00:00
{
2021-10-28 13:29:53 +00:00
__instance . StartRockThrow ( ) ;
2021-04-29 19:59:25 +00:00
}
2021-06-18 21:38:32 +00:00
2021-04-29 19:59:25 +00:00
return false ;
}
2021-04-26 13:30:21 +00:00
}
}